QR 코드를 생성하기 위해 Java 디자인을 사용하여 단계별로 가르쳐줍니다.
사물 인터넷 시대에는 QR 코드가 매우 중요합니다. 이제 당신이 무엇이든 QR 코드 로고를 받아야합니다. 구식이 아닌 것에 대한 두려움은 QR 코드를 사용하여 식별 할 수 없습니다. 언젠가 출생 증명서 나 가구 등록 책은 QR 코드로 식별됩니다. 오늘 나는 변덕에 있었고 다른 사람들이 블로그에 QR 코드를 추가 한 것을 보았고, 또한 테스트를하고 싶었습니다.
주로 두 가지 점을 달성하는 데 사용됩니다.
1. 텍스트로 QR 코드를 생성합니다.
2. QR 코드 중간에 이미지를 추가하십시오.
1. 준비 작업.
QR QR Code 3.0 코어 패키지 및 JPG 사진을 준비하십시오.
QR QR 코드 패키지를 다운로드하십시오.
먼저 zxing.jar 패키지를 다운로드해야합니다. 여기에서 3.0의 핵심 패키지를 사용하고 있습니다
다운로드 주소 : 이제 github : https://github.com/zxing/zxing/wiki/getting-started-developing으로 이동했습니다.
물론 Maven 저장소에서 Jar 패키지를 다운로드 할 수도 있습니다 : http://central.maven.org/maven2/com/google/zxing/core/
2. 프로그래밍
1. Eclipse를 시작하고 새 Java 프로젝트를 만들고 프로젝트 이름을 찾으십시오 (이 예에서는 qrcodesoft의 이름). 다음을 클릭하십시오 :
2. zxing.jar 패키지를 가져옵니다. 여기에서 3.0의 핵심 패키지를 사용하고 있습니다. "외부 JAR (X) 추가 ..."를 클릭하십시오.
3. 두 가지 새로운 클래스, 즉 :
Bufferedimageluminancesource.java
qrcodeutil.java
주요 코드는 다음과 같습니다. bufferedimageluminancesource.java 및 qrcodeutil.java는 테스트 된 주요 방법이 qrcodeutil.java에 있습니다.
Bufferedimageluminancesource.java 프로그램 코드 :
package com.yihaomen.barcode; import java.awt.graphics2d; import java.awt.geom.affinetransform; import java.awt.image.bufferedimage; import com.google.zxing.luminances; public class bufferedimageluminances extrends luminances {private buflerce extrents; 최종 int top; public bufferedimageluminancesource (bufferedImage image) {this (im = image.getheight (); if (왼쪽 + width> sourceWidth || top + height> sourceHeight) { "새 new height exception ("crop height; y <top; y <top + height; y x = left; x + width; x + +) {if (image. 0xff000000) == 0) {image.setrgb (x, y, 0xffffff); // = white}}} this.image = new bufferedImage (sourceWidth, sourceHeight, bufferedImage.type_byte_gray); this.image.getGraphics (). drawImage (image, 0, 0, null); this.left = 왼쪽; this.top = top;} 공개 바이트 (get y, gen y) > = getheight ()) {새로운 불법 불법 행정 덱스크 ( "요청 된 행은 이미지 외부에 있습니다 :" + y);} int width = getWidth = getWidth (); if (row == null || row.length <width) {row = new Byte [width];} image.getraster (). getmatrix () {int width = getWidth (); int height = getheight (); int area = width * height; byte [] matrix = new byte [zrea]; image.getRaster (). getDataelements (왼쪽, 상단, 높이, 높이, 매트릭스;} public boolean isropsupported () int top, int width, int height) {return new bufferedimageluminances (return new, this.left + left, this.top + 상단, 너비, 높이);} public boolean isrotatesupported () {return true;} public luminances rotatecounterclockisly () {int sourcewidth = incorswidth (); image.getheight (); affinetransform transform = new Affinetransform (0.0, -1.0, 1.0, 0.0, sourceWidth); bufferedImage rotatedImage = new bufferedImage (sourceHeight, SourceWidth, BufferedImage.Type_byte_gray); graphimage (g.cratedimage); null); g.dispose (); int width = getWidth (); 새로운 BufferedImageluminanceSource를 반환합니다 (RotatedImage, Top, SourceWidth- (왼쪽 + 너비), getheight (), width);}}qrcodeutil.java 프로그램 코드 :
package com.yhaomen.barcode; import java.awt.basicstroke; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.shape; import java.awt.geom.roundrectangle 2d; import java.awt 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.binarytybitmap; import com.zxogle.decodehodehintpe; 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.hybridbinaroger; com.google.zxing.qrcode.decoder.errorcorrectionlevel;/*** qr 코드 도구 클래스*/public class qrcodeutil {private static final string charset = "utf-8"; private static final string format_name = "jpg"; // qr wid wid static static final qrcode_size = // // 60; // 로고 높이 프라이빗 정적 최종 int 높이 = 60; 개인 정적 버퍼링 식품 CreateImage (문자열 내용, 문자열 imgpath, 부울 Needcompress) 예외 {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, hrcode_size, qrcode_size, qrcode_size). bitmatrix.getWidth (); int height = bitmatrix.getheight (); bufferedImage image = new bufferedImage (너비, 높이, bufferedImage.type_int_rgb); for (int x = 0; x <width; x ++) {for (int y = 0; y <height; y ++) {im Needcompress); return image;}/** * 로고 삽입 로고 * * @param 소스 * QR 코드 사진 * @param imgpath * 로고 사진 주소 * @param needcompress * @throws 예외 */private static void insertimage (bufferedImage source, string imgpath, boolean needcompress {img path (!)); if (!)); {System.err.println ( ""+imgpath+"이 파일은 존재하지 않습니다!"); return; return; im (높이> 높이) {높이 = 높이;} 이미지 이미지 = src.getScaledInstance (너비, 높이, 이미지 .scale_smooth); bufferedImage tag = new bufferedImage (너비, 높이, bufferedimage.type_int_rgb); Graphics G = tag.getGraphics (); G.DrawImage (0, 0, null); // 감소 된 그래프 G.dispose (); src = image;} // logographics2d Graph = source.creategraphics (); int x = (qrcode_size -width)/ 2; int y = (qrcode_size -height)/ 2; graph.drawimage (src, x, y, width, a); Y, 너비, 폭 void encode (문자열 내용, String imgpath, String DestPath, Boolean Needcompress) 예외 {bufferedimage image = qrcodeutil.createimage (content, imgpath, needcompress); mkdirs (destpath); String file = new Random (). NextInt (9999999)+". file (destPath+"/"+file));}/*** 폴더가 존재하지 않으면 mkdir는 MKDIR과 다른 다층 디렉토리를 자동으로 생성합니다. (부모 디렉토리가 존재하지 않으면 예외가 발생합니다) * @author lanyuan * 이메일 : [email protected] * @date 2013-12-11 10:16:36 AM * @param destpath 스토어 디렉토리 */public static void mkdirs (strest destpath) {file pile (destpite) (destpit); // 폴더가 존재하지 않으면 mkdirs는 MKDIR과 다른 다층 디렉토리를 자동으로 생성합니다. (mkdir는 부모 디렉토리가 존재하지 않으면 예외를 던집니다. 컨텐츠, String ImgPath, String DestPath) 예외 {qrcodeutil.encode (content, imgpath, destpath, false);}/** * qr 코드 생성 * * @param 컨텐츠 * @param destpath * 스토리지 주소 * @param needcompress * @throws 예외 */public void encode (string rest content, boolean regelcompress) {qrcodeutil.encode (내용, null, destpath, needcompress);}/*** qr 코드 생성** @param content* content* @param destpath* @throws 예외*/public static void encode (문자열 내용, Strows) {qrcodeutil.encode (컨텐츠, null, destpath); 로고) * * @param content * 컨텐츠 * @param imgpath * 로고 주소 * @param 출력 * 출력 스트림 * @param needcompress * 로고 * @throws 예외 */public static void encode (문자열 컨텐츠, 문자열 imgpath, boolean needcompress) 예외 {buffered image = qrcodeutil, createimage, createimage. Needcompress); imageio.write (image, format_name, output);}/*** QR 코드 생성** @param content* content* @param 출력* 출력 스트림* @throws 예외*/public static void encode (문자열 컨텐츠, 출력 스트림 출력) 예외 {qrcodeutil.encode (content, output, antal, rals, rall). @Param 파일 * QR 코드 이미지 * @return * @throws Exception */public static string decode (파일 파일) 예외 {bufferedImage image; image = image = image); if (image == null) {return null;} bufferedimageluminounces source = new buteredimageluminource (binatherbitmap map); HybridBinarizer (source)); 결과 결과; hashtable <decodehinttype, object> hints = new hashtable, object> (); hints.put (decodehinttype.character_set, charset); result = new multiformatreader (). decode (bitmap, hints); string resulttr.gettet (resulttt); * QR 코드를 구문 분석 * * @Param Path * QR 코드 이미지 주소 * @return * @throws Exception */public static String decode (문자열 경로) 예외 {return qrcodeUtil.decode (새 파일 (path));} public static void main (Strows) 예외 {문자열 text = "http://www.yihaomen.com"; qrcodeutil.encode (텍스트, "c : /me.jpg", "c :/barcode", true);}}로고없이 QR 코드를 생성합니다
프로그램 코드는 다음과 같습니다.
public static void main (String [] args)은 예외 {문자열 text = "http://www.dans88.com.cn"; qrcodeutil.encode (텍스트, "", "d :/myworkDoc", true);}이 테스트 방법을 실행하면 생성 된 QR 코드에는 로고가 없으며 스타일은 다음과 같습니다.
관심이 있으시면 휴대 전화로 스캔 할 수 있습니다.
로고로 QR 코드를 생성하십시오
로고 자신의 아바타 또는 좋아하는 이미지를 사용하고 다음 코드를 사용할 수 있습니다. 프로그램 코드는 다음과 같습니다.
public static void main (String [] args)은 예외 {문자열 text = "http://www.dans88.com.cn"; qrcodeutil.encode (텍스트, "d :/myworkdoc/my180.jpg", "d :/myworkdoc", true); }유일한 차이점은 로고의 주소가 이전 기준으로 지정되고 C 디스크의 이미지 파일이 여기에서 사용된다는 것입니다.
휴대폰으로 스캔하면 나타나려는 텍스트가 나타날 수 있습니다. 자신의 웹 사이트를 입력하려면 클릭하십시오.
위의 것은 Java 디자인을 사용하여 QR 코드를 생성하는 것입니다. 관심있는 친구들이 그것을 언급 할 수 있습니다. 이 사이트를 지원 해주셔서 감사합니다!