Java generates two-dimensional code

Source: Internet
Author: User

Two classes, one jar package (Core-3.0.0.jar)

One:

Package Com.java.qrCode;

Import Java.awt.Graphics2D;
Import Java.awt.geom.AffineTransform;
Import Java.awt.image.BufferedImage;

Import Com.google.zxing.LuminanceSource;

public class Bufferedimageluminancesource extends Luminancesource {
Private final bufferedimage image;
private final int left;
private final int top;

Public Bufferedimageluminancesource (bufferedimage image) {
This (image, 0, 0, image.getwidth (), Image.getheight ());
}

Public Bufferedimageluminancesource (bufferedimage image, int left,
int top, int width, int height) {
Super (width, height);

int sourcewidth = Image.getwidth ();
int sourceheight = Image.getheight ();
if (left + width > Sourcewidth | | top + height > sourceheight) {
throw New IllegalArgumentException (
"Crop rectangle does not fit within image data.");
}

for (int y = top; y < top + height; y++) {
for (int x = left, x < left + width; + +) {
if ((Image.getrgb (x, y) & 0xff000000) = = 0) {
Image.setrgb (x, y, 0xFFFFFFFF); = White
}
}
}

This.image = new BufferedImage (Sourcewidth, Sourceheight,
Bufferedimage.type_byte_gray);
This.image.getGraphics (). DrawImage (image, 0, 0, NULL);
This.left = left;
This.top = top;
}


Public byte[] GetRow (int y, byte[] row) {
if (Y < 0 | | y >= getheight ()) {
throw New IllegalArgumentException (
"Requested row is outside the image:" + y);
}
int width = getwidth ();
if (row = = NULL | | Row.length < width) {
row = new Byte[width];
}
Image.getraster (). getDataElements (left, top + y, width, 1, row);
return row;
}


Public byte[] Getmatrix () {
int width = getwidth ();
int height = getheight ();
int area = width * height;
byte[] Matrix = new Byte[area];
Image.getraster (). getDataElements (left, top, width, height, matrix);
return matrix;
}


public Boolean iscropsupported () {
return true;
}


Public luminancesource crop (int left, int top, int width, int height) {
return new Bufferedimageluminancesource (image, This.left + left,
This.top + top, width, height);
}


public Boolean isrotatesupported () {
return true;
}


Public Luminancesource rotatecounterclockwise () {
int sourcewidth = Image.getwidth ();
int sourceheight = Image.getheight ();
AffineTransform transform = new AffineTransform (0.0,-1.0, 1.0,
0.0, 0.0, sourcewidth);
BufferedImage rotatedimage = new BufferedImage (Sourceheight,
Sourcewidth, Bufferedimage.type_byte_gray);
Graphics2D g = rotatedimage.creategraphics ();
G.drawimage (image, transform, null);
G.dispose ();
int width = getwidth ();
return new Bufferedimageluminancesource (Rotatedimage, Top,
Sourcewidth-(left + width), getheight (), width);
}
}

Two:

Package Com.java.qrCode;

Import Java.awt.BasicStroke;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.Shape;
Import Java.awt.geom.RoundRectangle2D;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.OutputStream;
Import java.util.Hashtable;
Import Java.util.Random;

Import Javax.imageio.ImageIO;

Import Com.google.zxing.BarcodeFormat;
Import Com.google.zxing.BinaryBitmap;
Import Com.google.zxing.DecodeHintType;
Import Com.google.zxing.EncodeHintType;
Import Com.google.zxing.MultiFormatReader;
Import Com.google.zxing.MultiFormatWriter;
Import Com.google.zxing.Result;
Import Com.google.zxing.common.BitMatrix;
Import Com.google.zxing.common.HybridBinarizer;
Import Com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;


public class Qrcodeutil {

Private static final String CHARSET = "Utf-8";
private static final String Format_name = "JPG";
//Two D code size
private static final int qrcode_size = +;
//logo width
private static final int width =;
/ /logo height
private static final int height =;

private static BufferedImage CreateImage (string content, String Imgpath,
Boolean needcompress) throws Exception {
Hashtable hints = new Hashtable ();
Hints.put (Encodehinttype.error_correction, ErrorCorrectionLevel.H);
Hints.put (Encodehinttype.character_set, CHARSET);
Hints.put (Encodehinttype.margin, 1);
Bitmatrix Bitmatrix = new Multiformatwriter (). Encode (content,
Barcodeformat.qr_code, Qrcode_size, qrcode_size, hints);
int width = bitmatrix.getwidth ();
int height = bitmatrix.getheight ();
BufferedImage image = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
for (int x = 0; x < width; + +) {
for (int y = 0; y < height; y++) {
Image.setrgb (x, Y, Bitmatrix.get (x, y)? 0xff000000
: 0xFFFFFFFF);
}
}
if (Imgpath = = NULL | | "". Equals (Imgpath)) {
return image;
}
Insert Picture
Qrcodeutil.insertimage (image, Imgpath, needcompress);
return image;
}


private static void Insertimage (BufferedImage source, String Imgpath,
Boolean needcompress) throws Exception {
File File = new file (Imgpath);
if (!file.exists ()) {
System.err.println ("+imgpath+" The file does not exist!) ");
Return
}
Image src = imageio.read (new File (Imgpath));
int width = src.getwidth (null);
int height = src.getheight (null);
if (needcompress) {//Compress logo
if (Width > width) {
width = width;
}
if (height > height) {
Height = height;
}
Image image = Src.getscaledinstance (width, height,
Image.scale_smooth);
BufferedImage tag = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics g = tag.getgraphics ();
G.drawimage (image, 0, 0, NULL); Draw a zoomed-out diagram
G.dispose ();
src = image;
}
Insert Logo
Graphics2D graph = source.creategraphics ();
int x = (qrcode_size-width)/2;
int y = (qrcode_size-height)/2;
Graph.drawimage (src, x, y, width, height, null);
Shape shape = new Roundrectangle2d.float (x, y, Width, width, 6, 6);
Graph.setstroke (New Basicstroke (3f));
Graph.draw (Shape);
Graph.dispose ();
}


public static string encode (string content, String Imgpath, String destpath,
Boolean needcompress) throws Exception {
BufferedImage image = Qrcodeutil.createimage (content, Imgpath,
needcompress);
Mkdirs (DestPath);
String file = new Random (). Nextint (99999999) + ". jpg";
Imageio.write (image, Format_name, new File (destpath+ "/" +file));
return file;
}


public static void Mkdirs (String destpath) {
File file =new file (destpath);
When a folder does not exist, Mkdirs automatically creates a multi-tiered directory, distinguished from mkdir. (MkDir throws an exception if the parent directory does not exist)
if (!file.exists () &&!file.isdirectory ()) {
File.mkdirs ();
}
}


public static void Encode (string content, String Imgpath, String destpath)
Throws Exception {
Qrcodeutil.encode (content, Imgpath, DestPath, false);
}


public static void Encode (string content, String destpath,
Boolean needcompress) throws Exception {
Qrcodeutil.encode (content, NULL, destpath, needcompress);
}


public static void Encode (string content, String destpath) throws Exception {
Qrcodeutil.encode (content, NULL, DestPath, FALSE);
}


public static void Encode (string content, String Imgpath,
OutputStream output, Boolean needcompress) throws Exception {
BufferedImage image = Qrcodeutil.createimage (content, Imgpath,
needcompress);
Imageio.write (image, format_name, output);
}


public static void Encode (String content, outputstream output)
Throws Exception {
Qrcodeutil.encode (content, NULL, output, false);
}


public static String decode (file file) throws Exception {
BufferedImage image;
Image = Imageio.read (file);
if (image = = null) {
return null;
}
Bufferedimageluminancesource Source = new Bufferedimageluminancesource (
image);
Binarybitmap bitmap = new Binarybitmap (new Hybridbinarizer (source));
result result;
Hashtable hints = new Hashtable ();
Hints.put (Decodehinttype.character_set, CHARSET);
result = new Multiformatreader (). Decode (bitmap, hints);
String resultstr = Result.gettext ();
return resultstr;
}


public static string decode (string path) throws Exception {
Return Qrcodeutil.decode (new File);
}


public static void Main (string[] args) throws Exception {
String Text = "H001";

Create a QR code with logo
String Imagepath=qrcodeutil.encode (text, "D:/myworkdoc/logo.jpg", "D:/myworkdoc", true);
System.out.println (ImagePath);
Create a logo-less
Qrcodeutil.encode (Text, "", "D:/myworkdoc", true);
}

}

Three: with logo and without logo

Java generates two-dimensional code

Related Article

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.