package com.zzq;import java.awt.BasicStroke;import java.awt.Color;import Java.awt.font;import java.awt.graphics;import java.awt.graphics2d;import java.awt.image;import java.awt.Shape;import java.awt.font.FontRenderContext;import java.awt.geom.AffineTransform; import java.awt.geom.rectangle2d;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.client.j2se.bufferedimageluminancesource;import com.google.zxing.common.bitmatrix;import com.google.zxing.common.hybridbinarizer;import com.google.zxing.qrcode.decoder.errorcorrectionlevel;/** * QR Code tool * Add your own google zxing Bag * */public class qrcodeutil { private static final String CHARSET = "Utf-8"; private static final string format_name = "JPG"; // two-dimensional code size private static final int qrcode_ Size = 300; // logo width private static final int WIDTH = 60; // logo Height private static final int height = 60; private static bufferedimage createimage (string content, string imgpath, boolean needcompress) throws exception { 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,&NBSP;&NBSP;&NBSP;&NBSP;BUFFEREDIMAGE.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setrgb (X, y, bitMatrix.get (x, y) &NBSP;?&NBSP;0XFF000000&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;:&NBSP;0XFFFFFFFF); } } if (imgPath == null | | ". Equals (Imgpath) { return image; } // Insert Picture qrcodeutil.insertimage (image, imgpath, needcompress); return image; } /** * inserting logo * * @param source * qr code images * @param Imgpath * logo Photo Address * @param needCompress * whether compression * @throws exception */ private static void insertimage (bufferedimage source, string imgpath, boolean needcompress) throws&nbsP 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) { // compression 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,&NBSP;HEIGHT,&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BUFFEREDIMAGE.TYPE_INT_RGB); graphics g = tag.getgraphics (); g.drawimage (image, 0, 0, null); // drawing a reduced figure 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, &NBSP;6); graph.setstroke (New basicstroke (3f)); graph.draw (Shape); Graph.dispose (); } /** * generate text images * @param str * @param font * @param outFile * @throws Exception * @author the _monster March 12, 2015 * @version 1.0 */ public static void Createimageforname (String str,font font,file outfile) throws exception{ // Gets the font's style applied to the entire rectangle on the str rectangle2d r=font.getstringbounds (str, new FontRenderContext (Affinetransform.getscaleinstance (1, 1), false,false)); int unitheight= (int) Math.floor (r.getheight ());//Gets the height of a single character //gets the entire str with the width of the font style here with rounded after +1 to ensure that the width is absolutely able to hold this string as the width of the picture int width= (int) Math.Round (R.getwidth ()) +1; int height=unitheight+ 3;//the height of a single character +3 to ensure that the height can hold the string as the height of the picture //create a picture bufferedimage image=new bufferedimage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_BGR); graphics g=image.getgraphics (); g.setcolor (color.white); &nBsp; g.fillrect (0, 0, width, height);//First fill the entire picture with white, which is the background g.setcolor ( Color.Black);//Change to Black g.setfont (font);//Set Brush font g.drawstring (str, 0, font.getsize ());//Draw the string g.dispose (); imageio.write (image, "PNG", outfile);//output PNG image } /** * generate two-dimensional code (embedded logo) * * @param content * content * @param imgPath * logo address * @param destPath * Storage directory * @param needcompress * whether to compress logo * @throws &NBSP;EXCEPTION&NBSP;&NBsp;*/ public static void 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); } /** * When a folder does not exist, Mkdirs automatically creates a multi-tier directory, which differs from mkdir. (MkDir throws an exception if the parent directory does not exist) * @author lanyuan * Email: [email protected] * @date 2013-12-11 Morning 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, which differs 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 QR code &NBSP;&NBSP;*&NBsp; * @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 QR 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); } /** * generates 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); } / ** * generating QR codes * * @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); } /** * parsing two-dimensional code * * @param file * qr code images * @return * @throws exception */ 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 <decodehinttype, object> hints = new hashtable<decodehinttype, object > ( hints.put); result = new (Decodehinttype.character_set, charset); multiformatreader (). Decode (Bitmap, hints); string resultstr = result.gettext (); return resultstr; } /** * parsing QR code * * @param path * QR code image address * @return * @throws exception */ public static string decode (String Path) throws exception { return qrcodeutil.decode (New File (path)); } /** * Test * @param args * @throws exception * @author the_monster March 17, 2015 * @version 1.0 */ Public static void main (String[] args) throws exception { string text = "Zhangzhiqin"; //the content here is the format of the business card's QR code must be in this format String content = "Mecard:n: Laureatetel:00000000000;tel:13304012214; email:[email protected]; url:http://zzqnobody.blog.51cto.com; ADR: Shanghai; "; //first create the text picture createimageforname (Text,new font ("Arial", font.bold,18), New file ("c:/ A.jpg ") encode (content, " e:/a.jpg ", " C:/barcode ", true); SYSTEM.OUT.PRINTLN ("---success"); }}
This article is from the "The_monster" blog, make sure to keep this source http://zzqnobody.blog.51cto.com/7059797/1621599
Java version logo business card Two-dimensional code generation tool class