1. QRCode 및 ZXing QR 코드 생성 및 분석 지원;
2. QRCode는 QR 코드를 생성하고 다음과 같이 그림 추가를 지원합니다.
패키지 공통; import java.awt.color; import java.awt.graphics2d; import java.awt.image; image java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.util.hashmap; import java.util.map; jp.sourceforge.qrcode.qrcodedecoder; import jp.sourceforge.qrcode.exception.decodingfailedexception; import com.google.zxing.barcodeformat; import com.google.zxing.binarizer; import com.google.zxing.binarybitmap; import com.google.zxing.encodehinttype; import com.google.zxing.luminounces; import com.google.zxing.multatiformatrever; import com.gogle.zxed.zx.zxed com.google.zxing.notfoundexception; import com.google.zxing.writexception; import com.google.zxing.common.bitmatrix; import com.google.zxing.common.hybridbinarizer; import com.swetake.util.qrcode; /** * QR 코드 생성 도구 클래스 * @Author Cloud * @Data 2016-12-15 * QRCode */public class qrcodeUtil {// QR 코드 색상 개인 정적 최종 int 블랙 = 0xff000000; // QR 코드 색상 비공개 정적 최종 int white = 0xffffffff; /** * <span style = "font-size : 18px; font-weight : blod;"> zxing 메소드 QR 코드를 생성합니다 </span> * @param text <a href = "javaScript : void ();"; rel = "외부 nofollow"> QR 코드 컨텐츠 </a>* @param width qr 코드 너비* @param height qr 코드 높이 높이* @param outputpath qr 코드 생성 및 저장 경로* @param imagetype qr 코드 생성 형식*/ public static void zxingcodecreate (int width, int height, string) New Hashmap <EncodeHintType, String> (); // 인코딩 된 문자를 설정하십시오. {// 1. QR 코드 생성 BitMatrix Encode = New Multiformatwriter (). Encode (텍스트, Barcodeformat.qr_code, 너비, 높이, His); // 2. QR 코드 너비와 높이를 가져옵니다. int codewidth = encode.getWidth (); int codeheight = encode.getheight (); // 3. 버퍼링 된 스트림 BufferedImage 이미지에 QR 코드를 넣습니다. New BufferedImage (CodeWidth, CodeHeight, BufferedImage.type_int_rgb); for (int i = 0; i <codewidth; i ++) {for (int j = 0; j <codeHeight; j ++) {// 4. QR 코드 컨텐츠를 이미지 이미지에 고정하려는 루프 (I, J, Encode.get (i, j)? black : white); }} 파일 outputImage = 새 파일 (outputPath); // 이미지가 존재하지 않으면 (! outputImage.exists ()) outputImage.createnewfile (); // 5. qr code를 imageio.write (image, imageType, outputImage)에 쓰십시오. } catch (WriteRexception e) {e.printstacktrace (); System.out.println ( "QR 코드 생성 실패"); } catch (ioexception e) {e.printstacktrace (); System.out.println ( "QR 코드 이미지 변경"); }}/** * <span style = "font-syice : 18px; font-weight : blod;"> qr 코드 분석 </span> * @param analyzepath qr code path * @return * @throws ioexception */@suppresswarnings ( "rawtypes", "unchecked"} (strowed) multiformatreader formatreader = new multiformatreader (); 객체 결과 = null; try {file file = new File (AnalyzePath); if (! file.exists ()) {return "QR 코드가 존재하지 않습니다"; } bufferedImage image = imageio.Read (파일); luminancesource source = new luminancesourceutil (이미지); Binarizer binarizer = 새로운 하이브리드 비나 라이저 (소스); BinaryBitMap BinaryBitMap = New BinaryBitMap (Binarizer); 지도 힌트 = new Hashmap (); hints.put (encodehinttype.character_set, "utf-8"); result = formatreader.decode (binarybitmap, hints); } catch (notFoundException e) {e.printstacktrace (); } 반환 결과; }/*** <span style = "font-syice : 18px; font-weight : blod;"> qrcode qr code qr code code qr code content* @param imgpath qr 코드 생성 경로* @param 버전 qr 코드 버전* @param isflag (널로 logo inclate) imgpath, int 버전, 문자열 logopath) {try {qrcode qrcodehandler = new qrcode (); // QR 코드 오류 설명 속도, 선택적 l (7%) m (15%) q (25%) h (30%)를 설정합니다. 오류 결정 속도가 높을수록 정보가 줄어들 수 있지만 QR 코드 QRCodeHandler.setQRCodeERRORCORRECT ( 'M')의 선명도에 대한 요구 사항이 작을 수 있습니다. // n은 숫자를 나타내고, a는 문자 AZ를 나타내고, b는 다른 문자 qrcodehandler.setqrcodeencodemode ( 'b')를 나타냅니다. // 버전 1은 21*21 행렬이며, 버전이 1 증가 할 때마다 QR 코드의 양면은 4로 증가합니다. 버전 7은 45*45의 행렬입니다. 가장 높은 버전은 40이며, 이는 177*177 qrcodehandler.setqrcodeversion (버전)을 가진 매트릭스입니다. // int imgsize = 67 + 12 * (버전 -1)에 따라 크기를 계산합니다. 바이트 [] contentBytes = content.getBytes ( "gb2312"); BufferedImage bufimg = new bufferedImage (imgsize, imgsize, bufferedImage.type_int_rgb); 그래픽 2d gs = bufimg.creategraphics (); gs.setbackground (color.white); gs. clearrect (0, 0, imgsize, imgsize); // 이미지 색상 설정> 검은 색 gs.setColor (color.black); // 설정하지 않도록 오프셋을 설정하십시오. 구문 분석 오류가 발생할 수 있습니다 int pixoff = 2; // output content> qr code if (contentBytes.length> 0 && contentBytes.length <130) {boolean [] [] codeout = qrcodehandler.calqrcode (contentBytes); for (int i = 0; i <codeout.length; i ++) {for (int J = 0; }}}} else {system.err.println ( "qrcode content bytes length =" + contentBytes.length + "는 [0,130]에 있지 않음"); } /* 로고 이미지를 추가 해야하는지 결정* / if (logopath! = null) {file icon = 새 파일 (logopath); if (icon.exists ()) {int width_4 = imgsize / 4; int width_8 = width_4 / 2; int height_4 = imgsize / 4; int height_8 = height_4 / 2; image img = imageio.read (아이콘); gs.DrawImage (img, width_4 + width_8, height_4 + height_8, width_4, height_4, null); gs.Dispose (); bufimg.flush (); } else {System.out.println ( "오류 : 로그인 이미지가 존재하지 않습니다!"); }} gs.Dispose (); bufimg.flush (); // QR 코드 파일 생성 파일 ImgFile = 새 파일 (imgpath); if (! imgfile.exists ()) imgfile.createnewfile (); // 생성 된 이미지에서 이미지를 가져옵니다. String imgtype = imgpath.substring (imgpath.lastindexof ( ".") + 1, imgpath.length ()); // QR 코드 생성 qrcode imageio.write (bufimg, imgtype, imgfile); } catch (예외 e) {e.printstacktrace (); }}/*** <span style = "font-syle : 18px; font-weight : blod;"> qrcode qr 코드 분석 </span>* @param codepath qr code path* @return parsing result*/public static string qrcodeanalyze (String CodePath) {file imagefile = 새 파일); BufferedImage bufimg = null; 문자열 decodedData = null; try {if (! imagefile.exists ()) 반환 "QR 코드가 존재하지 않습니다"; bufimg = imageio.read (imageFile); qrcodedecoder decoder = 새로운 qrcodedecoder (); decodedData = new String (decoder.decode (new imageUtil (bufimg)), "gb2312"); } catch (ioException e) {System.out.println ( "error :" + e.getMessage ()); e.printstacktrace (); } catch (decodingfailedException dfe) {system.out.println ( "error :" + dfe.getMessage ()); dfe.printstacktrace (); } return decodedData; }} 3. 마지막 포스트 테스트 코드 : 패키지 테스트; import java.awt.image.bufferedimage; import java.io.inputStream; import java.net.url; import javax.imageio.imageio; import Common.ImageUtil; import common.qrcodeutil; jp.sourceforge.qrcode.qrcodedecoder 가져 오기; /*** QR 코드 생성 테스트 클래스* @Author Cloud* @Data 2016-11-21* QRCODETEST* /public class QRCODETEST {public static void main (String [] args)은 예외 { /*** QRCODE QR 코드 생성 테스트* qrcodeutil.qrcodecreate ( "http://blog.csdn.net/u014266877", "e : //qrcode.jpg", 15, "e : //icon.png"); *//*** QRCODE QR 코드 분석 테스트* 문자열 qrcodeAnalyze = qrcodeUtil.qrcodeanalyze ( "e : //qrcode.jpg"); *//*** ZXINGCODE QR 코드 생성 테스트* qrcodeUtil.zxingCodeCreate ( "http://blog.csdn.net/u014266877", 300, 300, "e : //zxingcode.jpg", "JPG"); *//*** ZXINGCODE QR 코드 분석* 문자열 zxingAnalyze = qrcodeUtil.zxingCodeAnalyze ( "e : //zxingcode.jpg") .toString (); */ system.out.println ( "성공"); }}