I am writing code for a Visual Cryptography project in Java. We want to create two share images for a random token (alphanumeric string), so that when the two images are overlayed the token will be revealed. Now - even before the visual crypto part kicks in, I am trying to figure a way to convert this alphanumeric token into an image, and don't know where to start. Any suggestions? Thanks!
Asked
Active
Viewed 6,511 times
3 Answers
4
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;
}
}

Gabriel Ruiu
- 2,753
- 2
- 19
- 23
-
This is great. This is what I was trying to do. I may have to play around with the size and resolution a bit, but this is a good starting point. Thanks! – Luxuser May 12 '14 at 08:28
-
I'm glad it was useful. Don't forget to set the answer as correct :) – Gabriel Ruiu May 12 '14 at 09:09
1
I would start by creating a BufferedImage object, acquiring its Graphics context, calling Graphics.drawString()
to turn your token into image data, then writing the BufferedImage
to disk.

Dan O
- 6,022
- 2
- 32
- 50
-
That makes sense to me in theory, so I will definitely give that a try. Thanks! – Luxuser May 12 '14 at 08:20
0
I wouldn't know too much but at a guess i would approach it like this:
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++;
}
then get the two images pixels parse the rgb value back into byte and and reverse your separation process, to decode.

Michael Kent
- 383
- 3
- 17