Java Implementation example of merging two pictures

Source: Internet
Author: User
Tags flush

Recently we need to do a two-dimensional code generation and embed two-dimensional code in an existing background template. Has not written these aspects of things, suddenly write is really a bit confused, especially for the Java Picture API is not very familiar with, so reference a lot of netizens to share, quickly made, Now share the

The code is as follows Copy Code

BufferedImage image= bim;//Two-dimensional code
BufferedImage bg= imageio.read (path);//Get Beijing pictures
Graphics2D G=bg.creategraphics ();
int width=image.getwidth (NULL) > Bg.getwidth () * 5/10? (Bg.getwidth () * 5/10): Image.getwidth (NULL);
int height=image.getheight (NULL) > Bg.getheight () *5/10? (Bg.getheight () * 5/10): Image.getwidth (NULL);
G.drawimage (Image, (Bg.getwidth ()-width)/2, (Bg.getheight ()-height)/2,width,height,null);
G.dispose ();
Bg.flush ();
Image.flush ();
Imageio.write (BG, "PNG", file);

On the generation of two-dimensional code in the article Java generation with logo of the colorful two-dimensional code interested can take a look at

The code is as follows:

The code is as follows Copy Code

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");

}
}

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.