Import Java.awt.Color; Import Java.awt.Graphics2D; Import Java.awt.Image; Import Java.awt.geom.AffineTransform; Import java.awt.image.AffineTransformOp; Import Java.awt.image.BufferedImage; Import Java.io.File; Import java.io.IOException; Import java.io.UnsupportedEncodingException; Import Java.util.HashMap; Import Java.util.Map; Import Javax.imageio.ImageIO; Import Com.google.zxing.BarcodeFormat; Import Com.google.zxing.EncodeHintType; Import Com.google.zxing.MultiFormatWriter; Import com.google.zxing.WriterException; Import Com.google.zxing.common.BitMatrix; Import Com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class Matrixtoimagewriter { private static final int image_width = 100; private static final int image_height = 100; private static final int image_half_width = IMAGE_WIDTH/2; private static final int frame_width = 2; private static Multiformatwriter Mutiwriter = new Multiformatwriter (); public static void Encode (String content, int width, int height, String Srcimagepath, String destimagepath) { try { Imageio.write (content, width, height, srcimagepath), Genbarcode "JPG", new File (Destimagepath)); catch (IOException e) { E.printstacktrace (); catch (Writerexception e) { E.printstacktrace (); } } private static BufferedImage Genbarcode (String content, int width, int height, String srcimagepath) throws Writerexception, IOException { BufferedImage scaleimage = scale (Srcimagepath, Image_width, Image_height, True); int[][] Srcpixels = new Int[image_width][image_height]; for (int i = 0; i < scaleimage.getwidth (); i++) { for (int j = 0; J < Scaleimage.getheight (); j + +) { SRCPIXELS[I][J] = Scaleimage.getrgb (i, j); } } Map<encodehinttype, object> hint = new Hashmap<encodehinttype, object> (); Hint.put (Encodehinttype.character_set, "utf-8"); Hint.put (Encodehinttype.error_correction, ErrorCorrectionLevel.H); Generate a two-dimensional code Bitmatrix matrix = mutiwriter.encode (content, Barcodeformat.qr_code, width, height, hint); Two-dimensional matrices into one-dimensional pixel group int HALFW = Matrix.getwidth ()/2; int halfh = Matrix.getheight ()/2; int[] pixels = new int[width * height]; for (int y = 0; y < matrix.getheight (); y++) { for (int x = 0; x < Matrix.getwidth (); + + +) { Top left corner color, adjust color range and color according to your needs if (x > 0 && x < 170 && y > 0 && y < 170) { Color color = new Color (231, 144, 56); int colorint = Color.getrgb (); Pixels[y * width + x] = Matrix.get (x, y)? Colorint : 16777215; } Reading pictures else if (x > Halfw-image_half_width && x < HALFW + Image_half_width && y > Halfh-image_half_width && y < HALFH + image_half_width) { Pixels[y * width + x] = SRCPIXELS[X-HALFW + IMAGE_HALF_WIDTH][Y-HALFH + image_half_width]; else if (x > Halfw-image_half_width-frame_width && x < Halfw-image_half_width + Frame_width && y > Halfh-image_half_width-frame_width && y < HALFH + Image_half_width + frame_width) || (x > HALFW + image_half_width-frame_width && x < HALFW + Image_half_width + frame_width && y > Halfw-image_half_width-frame_width && y < HALFH + Image_half_width + frame_width) || (X > Halfw-image_half_width-frame_width && x < HALFW + Image_half_width + frame_width && y > Halfh-image_half_width-frame_width && y < HALFH -Image_half_width + frame_width) || (X > Halfw-image_half_width-frame_width && x < HALFW + Image_half_width + frame_width && y > halfh + image_half_width-frame_width && y < HALFH + image_half_width + frame_width)) { Pixels[y * width + x] = 0XFFFFFFF; Form a border in the picture four weeks } else { Two-dimensional code color int NUM1 = (int) (50.0-13.0)/Matrix.getheight () * (y + 1)); int num2 = (int) (165-(165.0-72.0)/Matrix.getheight () * (y + 1)); int num3 = (int) (162-(162.0-107.0) /Matrix.getheight () * (y + 1)); Color color = new Color (NUM1, num2, num3); int colorint = Color.getrgb (); Here you can modify the color of two-dimensional code, you can set the color of two-dimensional code and background respectively; Pixels[y * width + x] = Matrix.get (x, y)? Colorint : 16777215; 0x000000:0xffffff } } } BufferedImage image = new BufferedImage (width, height, BUFFEREDIMAGE.TYPE_INT_RGB); Image.getraster (). setDataElements (0, 0, width, height, pixels); return image; } private static BufferedImage scale (String srcimagefile, int height, int width, Boolean hasfiller) throws IOException { Double ratio = 0.0; Scaling ratio File File = new file (srcimagefile); BufferedImage srcimage = imageio.read (file); Image destimage = srcimage.getscaledinstance (width, height, Bufferedimage.scale_smooth); Calculation ratio if (Srcimage.getheight () > height) | | (Srcimage.getwidth () > width)) { if (Srcimage.getheight () > Srcimage.getwidth ()) { Ratio = (new Integer (height)). Doublevalue () /Srcimage.getheight (); } else { Ratio = (new Integer (width)). Doublevalue () /Srcimage.getwidth (); } Affinetransformop op = new Affinetransformop ( Affinetransform.getscaleinstance (ratio, ratio), null); Destimage = Op.filter (srcimage, NULL); } if (Hasfiller) { Filler BufferedImage image = new BufferedImage (width, height, BUFFEREDIMAGE.TYPE_INT_RGB); Graphics2D graphic = Image.creategraphics (); Graphic.setcolor (Color.White); Graphic.fillrect (0, 0, width, height); if (width = = destimage.getwidth (null)) Graphic.drawimage (destimage, 0, (Height-destimage.getheight (NULL))/2, Destimage.getwidth (NULL), destimage.getheight (null), Color.White, NULL); Else Graphic.drawimage (Destimage, (Width-destimage.getwidth (NULL))/2, 0, Destimage.getwidth (NULL), destimage.getheight (null), Color.White, NULL); Graphic.dispose (); Destimage = image; } Return (bufferedimage) destimage; } public static void Main (string[] args) throws Unsupportedencodingexception { Content (not supported in Chinese), wide, long, middle icon path, store path Matrixtoimagewriter.encode ("http://www.baidu.com/", 512, 512, "D:\\logo.png", "d:\\2013-01.jpg"); } } |