Java generates horizontal and vertical image

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.