Java generates two-dimensional code

Source: Internet
Author: User

Package Test;


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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Package Test;


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;

/**
* QR Code Tool class
*
*/
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 { Br>hashtable<encodehinttype, object> hints = new Hashtable<encodehinttype, object> ();
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;
}

/**
* Insert Logo
*
* @param source
* Two-dimensional code image
* @param Imgpath
* Logo image Address
* @param needcompress
* Whether to compress
* @throws Exception
*/
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 ();
}

/**
* Generate two-dimensional code (embedded logo)
*
* @param content
* Content
* @param imgpath
* Logo address
* @param destpath
* Store directory
* @param needcompress
* Whether to compress logo
* @throws Exception
*/
public static File encode (String conte NT, String Imgpath, String DestPath,
Boolean needcompress) throws Exception {
BufferedImage image = Qrcodeutil.crea Teimage (content, Imgpath,
needcompress);
Mkdirs (DestPath);
String file = new Random (). Nextint (99999999) + ". jpg";
File T = new file (destpath+ "/" +file);

Imageio.write (image, Format_name, T);
return t;
}

/**
* When the folder does not exist, Mkdirs will automatically create a multi-level directory, different from mkdir. (MkDir throws an exception if the parent directory does not exist)
* @author Lanyuan
* Email: [Email protected]
* @date 2013-12-11 10:16:36
* @param destpath Storage Directory
*/
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 ();
}
}

/**
* Generate two-dimensional code (embedded logo)
*
* @param content
* Content
* @param Imgpath
* Logo Address
* @param destpath
* Storage Address
* @throws Exception
*/
public static void Encode (string content, String Imgpath, String destpath)
Throws Exception {
Qrcodeutil.encode (content, Imgpath, DestPath, false);
}

/**
* Generate two-dimensional code
*
* @param content
* Content
* @param destpath
* Storage Address
* @param needcompress
* Whether to compress logo
* @throws Exception
*/
public static void Encode (string content, String destpath,
Boolean needcompress) throws Exception {
Qrcodeutil.encode (content, NULL, destpath, needcompress);
}

/**
* Generate two-dimensional code
*
* @param content
* Content
* @param destpath
* Storage Address
* @throws Exception
*/
public static void Encode (string content, String destpath) throws Exception {
Qrcodeutil.encode (content, NULL, DestPath, FALSE);
}

/**
* Generate two-dimensional code (embedded logo)
*
* @param content
* Content
* @param Imgpath
* Logo Address
* @param output
* Output stream
* @param needcompress
* Whether to compress logo
* @throws Exception
*/
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);
}

/**
* Generate two-dimensional code
*
* @param content
* Content
* @param output
* Output stream
* @throws Exception
*/
public static void Encode (String content, outputstream output)
Throws Exception {
Qrcodeutil.encode (content, NULL, output, false);
}

/**
* Parse QR code
*
* @param file
* QR code image
* @return
* @throws Exception
*/
public static Stri Ng 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<decodehinttype, object> hints = new Hashtable<decodehinttype, object> ();
Hints.put (Decodehinttype.character_set, CHARSET);
result = new Multiformatreader (). Decode (bitmap, hints);
String resultstr = Result.gettext ();
return resultstr;
}

/**
* Parse QR code
*
* @param path
* QR code image address
* @return
* @throws Exception
*/
public static St Ring decode (String path) throws Exception {
return Qrcodeutil.decode (New File (path));
}
/**
*
* @param content
* @param logo address


@throws Exception
* Adjust method. Returns the generated picture path File p = qrcodeutil.encode (text, "D://33.png", "c:/a/", true);

Incoming CAPTCHA content middle avatar picture Generation Success Captcha path true execution
*/
public static void Main (string[] args) throws Exception {
String Text = "www.eeeem11111";
File p = qrcodeutil.encode (text, "D://123.png", "c:/a/", true);
SYSTEM.OUT.PRINTLN (P);
}
}

Java generates two-dimensional code

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.