packagesample;importcom.sun.image.codec.jpeg.JPEGCodec;importcom.sun.image.codec.jpeg.JPEGImageEncoder;importjavafx.application.Application;importjavafx.fxml.FXMLLoader;importjavafx.scene.Parent;importjavafx.scene.Scene;importjavafx.scene.control.Label;importjavafx.scene.image.Image;importjavafx.scene.image.ImageView;importjavafx.scene.layout.HBox;importjavafx.scene.layout.Pane;importjavafx.stage.Stage;importjavax.imageio.ImageIO;import java.awt.*;importjava.awt.image.BufferedImage;import java.io.*;importjava.util.HashMap;importjava.util.Map;importjava.util.Random;public class Main extendsApplication {
@Overridepublic void start(Stage primaryStage) throwsException{
Parent root= FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
String code=ImageUtil.outputtofile();
Image image= new Image("ttt.jpeg");
Label label= newLabel(code);
ImageView imageView= newImageView(image);
HBox hBox=newHBox();
Scene scene= new Scene(hBox, 300, 275);
hBox.getChildren().addAll(imageView,label);
primaryStage.setScene(scene);
primaryStage.show();
}public static voidmain(String[] args) {
launch(args);
}
}classImageUtil {private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6','7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};private static final int SIZE = 4;private static final int LINES = 5;private static final int WIDTH = 80;private static final int HEIGHT = 40;private static final int FONT_SIZE = 30;public static MapcreateImage() {
StringBuffer sb= newStringBuffer();
BufferedImage image= newBufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics graphic=image.getGraphics();
graphic.setColor(Color.LIGHT_GRAY);
graphic.fillRect(0, 0, WIDTH, HEIGHT);
Random ran= newRandom();for (int i = 1; i <= SIZE; i++) {int r =ran.nextInt(chars.length);
graphic.setColor(getRandomColor());
graphic.setFont(new Font(null, Font.BOLD +Font.ITALIC, FONT_SIZE));
graphic.drawString(chars[r]+ "", (i - 1) * WIDTH /SIZE,
HEIGHT/ 2);
sb.append(chars[r]);
}for (int i = 1; i <= LINES; i++) {
graphic.setColor(getRandomColor());
graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
}
Map map = new HashMap();
map.put(sb.toString(), image);returnmap;
}public staticColor getRandomColor() {
Random ran= newRandom();
Color color= new Color(ran.nextInt(256), ran.nextInt(256),
ran.nextInt(256));returncolor;
}public staticInputStream getInputStream(BufferedImage image)throwsIOException {
ByteArrayOutputStream bos= newByteArrayOutputStream();
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);byte[] imageBts =bos.toByteArray();
InputStream in= newByteArrayInputStream(imageBts);returnin;
}public staticString outputtofile() {
Map map = new HashMap<>() ;
Map imageMap =createImage();
String code=imageMap.keySet().iterator().next();
BufferedImage image=imageMap.get(code);try{
OutputStream ops= new FileOutputStream("src/ttt.jpeg");
ImageIO.write(image,"jpeg", ops);
ops.flush();
}catch(FileNotFoundException ex){
}catch(IOException ex) {
}returncode;
}
}