【问题标题】:Converting text to image in Java在Java中将文本转换为图像
【发布时间】:2014-05-09 15:12:44
【问题描述】:

我正在用 Java 为 Visual Cryptography 项目编写代码。我们想为一个随机令牌(字母数字字符串)创建两个共享图像,这样当两个图像重叠时,令牌就会被显示出来。 现在 - 甚至在视觉加密部分开始之前,我正试图找到一种将这个字母数字令牌转换为图像的方法,但不知道从哪里开始。有什么建议么?谢谢!

【问题讨论】:

    标签: java graphics2d cryptoapi


    【解决方案1】:
    public class TextToGraphicConverter {
    
    
    
      public static void main(String[] args) throws Exception {
            BufferedImage image = new TextToGraphicConverter().convertTextToGraphic("my text", new Font("Arial", Font.PLAIN, 18));
            //write BufferedImage to file
            ImageIO.write(image, "png", new File("path-to-file.png"));
        }
    
        public BufferedImage convertTextToGraphic(String text, Font font) {
    
            BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = img.createGraphics();
    
            g2d.setFont(font);
            FontMetrics fm = g2d.getFontMetrics();
            int width = fm.stringWidth(text);
            int height = fm.getHeight();
            g2d.dispose();
    
            img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    
            g2d = img.createGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
            g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            g2d.setFont(font);
            fm = g2d.getFontMetrics();
            g2d.setColor(Color.BLACK);
            g2d.drawString(text, 0, fm.getAscent());
            g2d.dispose();
            return img;
        }
    }
    

    【讨论】:

    • 这很棒。这就是我想要做的。我可能需要稍微调整一下大小和分辨率,但这是一个很好的起点。谢谢!
    • 我很高兴它很有用。不要忘记将答案设置为正确:)
    【解决方案2】:

    我将首先创建一个BufferedImage 对象,获取其Graphics 上下文,调用Graphics.drawString() 将您的令牌转换为图像数据,然后将BufferedImage 写入磁盘。

    【讨论】:

    • 这在理论上对我来说是有道理的,所以我一定会尝试一下。谢谢!
    【解决方案3】:

    我不会知道太多,但我猜我会这样处理它:

    String string="something";
    Byte[] byteArray=string.getbytes();
    
    //split the byte array into two one for each image. probably want something different than what i did.
    int count=0
    Byte[] bytes1=new Bytes[byteArray.length];
    Byte[] bytes2=new Bytes[byteArray.length];
    for(Byte b:byteArray){
    bytes1[count]=b*0.2;
    
    bytes2[count]=b*0.8;
    count++;
    }
    //init images
    BufferedImage img1 = new BufferedImage(bytes1.length, 1,BufferedImage.TYPE_INT_RGB);
    BufferedImage img2 = new BufferedImage(bytes1.length, 1,BufferedImage.TYPE_INT_RGB);
    
    
    //put bytes into pixels as int
    int count=0;
    for(Byte b: bytes1){
    img1.setRGB(0, count, (int)b);
    count++;}
    
    count=0;
    for(Byte b: bytes2){
    img2.setRGB(0, count, (int)b);
    count++;
    }
    

    然后得到两个图像像素,将 rgb 值解析回字节,然后反转你的分离过程,进行解码。

    【讨论】:

      猜你喜欢
      • 2012-05-12
      • 2016-03-29
      • 2010-11-29
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多