Some time ago, I saw a very popular two-way picture in space, that is, I could watch it, or I could watch it. I tried to use a program to generate it myself.
In general, the font is scaled and then combined in one piece. The overall operation is as follows:
Generate two images with corresponding information to zoom in and out the images and overlay them.
The following code: image operation ImageUtil:
Package honest. imageio; import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. image. bufferedImage; import java. io. file; import java. io. IOException; import javax. imageio. imageIO;/*** image operation class ** @ author Li Yue Dong **/public class ImageUtil {private BufferedImage image; private int width; // image width private int height; // Image height public ImageUtil (int width, int height) {this. width = width; t His. height = height; image = new BufferedImage (width, height, BufferedImage. TYPE_INT_ARGB );} /*** create an image containing a specified color string ** @ param message * string * @ param fontSize * font size * @ param color * font color * @ return image */public bufferedImage drawString (String message, int fontSize, Color color) {Graphics g = image. getGraphics (); g. setColor (color); Font f = new Font ("", Font. BOLD, fontSize); g. setFont (f); int len = mess Age. length (); g. drawString (message, (width-fontSize * len)/2, (height + (int) (fontSize/1.5)/2); g. dispose (); return image ;} /*** scale the image ** @ param scaleW * horizontal scale * @ param scaleY * vertical scale * @ return */public BufferedImage scale (double scaleW, double scaleH) {width = (int) (width * scaleW); height = (int) (height * scaleH); BufferedImage newImage = new BufferedImage (width, height, image. getType ()); Graphics g = newImage. getGraphics (); g. drawImage (image, 0, 0, width, height, null); g. dispose (); image = newImage; return image;}/*** rotate 90 degrees ** @ return corresponding image */public BufferedImage rotate () {BufferedImage dest = new BufferedImage (height, width, BufferedImage. TYPE_INT_ARGB); for (int I = 0; I <width; I ++) for (int j = 0; j
GenerateImage:
Package honest. imageio; import java. awt. color;/*** generate an image ** @ author Honest **/public class GenerateImage {public static void main (String [] args) {String outPath = "/home/honest/Desktop/out.png"; ImageUtil imageUtil1 = new ImageUtil (2000,250); imageUtil1.drawString ("This is just a test program", 200, Color. BLUE); imageUtil1.scale (0.125, 1); ImageUtil imageUtil2 = new ImageUtil (2000,250); imageUtil2.drawString ("this is just another program", 200, Color. RED); imageUtil2.scale (0.125, 1); imageUtil2.rotate (); imageUtil2.mergeImage (imageUtil1.getImage (); imageUtil2.save (outPath );}}
The comments in the Code have been clearly written. Please leave a message.