QR 코드는 어디에나 있으므로 스캔 할 수 있으며 선물을 받게됩니다. QR 코드는 지금 매우 인기가 있습니다. QR 코드가 어떻게 생성되는지에 대해 모든 사람이 명확하지 않다고 생각합니다. 이제 편집자는이 기사를 공유하여 QR 코드 생성 방법을 배우는 데 도움이되는 방법을 공유 할 것입니다.
실제로이 기능은 주로 Goggle이 게시 한 JAR을 사용하여 사용됩니다.
1. QR 코드 생성
zxing-core.jar 패키지를 classpath에 추가하십시오.
QR 코드 생성에는 MatrixtoImageWriter 클래스의 도움이 필요합니다. 이 수업은 Google에서 제공합니다. 클래스를 소스 코드에 복사 할 수 있습니다. 여기서는이 클래스의 소스 코드를 붙여 넣고 직접 사용할 수 있습니다.
QR 코드를 직접 생성 할 수있는 코드
public void test1 ()은 예외 {문자열 내용 = "www.baidu.com"을 던집니다. 문자열 path = "d : //"; multiformatwriter multiformatwriter = new multiformatwriter (); // zxing은 Google에서 제공하는 바코드입니다. 지도 힌트 = new Hashmap (); hints.put (encodehinttype.character_set, "utf-8"); bitmatrix bitmatrix = multiformatwriter.encode (content, barcodeformat.qr_code, 400, 400, 힌트); // 이것은 사진 파일의 크기입니다. matrixtoimagewriter.writetofile (bitmatrix, "jpg", file1); System.out.println ( "실행 완료"); }이 코드를 복사 한 후 MatrixtoImageWriter가보고 한 오류가 있음을 알 수 있으므로 찾아야하지만 여기에 코드를 게시하고 직접 사용할 수 있습니다.
import com.google.zxing.common.bitmatrix; import javax.imageio.imageio; import java.io.file; import java.io.outputStream; import java.io.ioexception; java.awt.image.bufferedimage import; 공개 최종 클래스 MatrixToImageWriter {private static final int black = 0xff000000; 개인 정적 최종 int white = 0xffffffff; private matrixtoimagewriter () {} public static bufferedimage tobufferedimage (bitmatrix matrix) {int width = matrix.getWidth (); int height = matrix.getheight (); BufferedImage image = 새로운 BufferedImage (너비, 높이, BufferedImage.type_int_rgb); for (int x = 0; x <width; x ++) {for (int y = 0; y <height; y ++) {image.setrgb (x, y, matrix.get (x, y)? black : white); }} 반환 이미지; } public static void writeTofile (bitmatrix matrix, 문자열 형식, 파일 파일)은 ioexception {bufferedImage image = tobufferedImage (matrix); if (! imageio.write (image, format, file)) {trash new ioException ( "" + format + "형식의 이미지를" + 파일에 쓸 수 없음); }} public static void writeToStream (bitmatrix matrix, String format, outputStream 스트림)은 ioException {bufferedImage image = TobufferedImage (matrix); if (! imageio.write (image, format, stream)) {Throw New IoException ( "형식 이미지를 쓸 수 없음" + 형식); }}}이제 생성 된 QR 코드를보기 위해 D 디스크의 루트 디렉토리를 다운로드 할 수 있습니다.
QR 코드 분석
세대와 마찬가지로 Google에서 제공하는 보조 클래스 (Bufferedimageluminancesource)가 필요합니다. 여기에서는이 클래스의 소스 코드를 붙여 넣고 검색 문제를 저장하여 직접 복사하여 사용할 수 있습니다.
BufferedImageluminanceSource import com.google.zxing.luminancesource; import java.awt.graphics2d; java.awt.geom.affinetransform; java.awt.image.bufferedimage import; 공개 최종 클래스 BufferedImageluminancesource는 Luminancesource {Private Final BufferedImage 이미지; 개인 최종 INT 왼쪽; 개인 최종 INT 탑; public bufferedimageluminancesource (bufferedImage image) {this (image, 0, 0, image.getWidth (), image.getheight ()); } public bufferedimageluminancesource (bufferedImage 이미지, int 왼쪽, int 상단, int 너비, int 높이) {슈퍼 (너비, 높이); int sourceWidth = image.getWidth (); int sourceHeight = image.getheight (); if (왼쪽 + 너비> sourcewidth || top + height> sourceHeight) {새로운 불법 불법 행위 ( "작물 사각형은 이미지 데이터에 맞지 않습니다."); } for (int y = top; y <top+height; y ++) {for (int x = left; x <왼쪽+너비; x ++) {if ((image.getrgb (x, y) & 0xff000000) == 0) {im // = white}}} this.image = new bufferedImage (sourceWidth, sourceHeight, bufferedImage.type_byte_gray); this.image.getGraphics (). DrawImage (image, 0, 0, null); this.left = 왼쪽; this.top = 상단; } @override public byte [] getrow (int y, byte [] row) {if (y <0 || y> = getheight ()) {throw new new OregalArgumentException ( "요청 된 행은 이미지 외부에 있습니다 :" + y); } int width = getWidth (); if (row == null || row.length <width) {row = new Byte [width]; } image.getRaster (). getDataelements (왼쪽, 상단 + y, 너비, 1, 행); 반환 행; } @override public byte [] getmatrix () {int width = getWidth (); int height = getheight (); int 면적 = 너비 * 높이; 바이트 [] 매트릭스 = 새로운 바이트 [영역]; image.getRaster (). getDataelements (왼쪽, 상단, 너비, 높이, 행렬); 리턴 매트릭스; } @override public boolean iscropsupported () {return true; } @override public luminancesource crop (int 왼쪽, int 상단, int 너비, int 높이) {return new bufferedimageluminancesource (이미지, this.left + left, this.top +, 너비, 높이); } @override public boolean isrotatesupported () {return true; } @override public luminancesource rotateCounterclockiswise () {int sourceWidth = image.getWidth (); int sourceHeight = image.getheight (); 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 (이미지, 변환, null); g.dispose (); int width = getWidth (); 새로운 BufferedImageluminanceSource (RotatedImage, Top, SourceWidth- (왼쪽 + 너비), getheight (), 너비)를 반환합니다. }}QR 코드를 구문 분석하는 코드
multiformatreader formatreader = new multiformatreader (); 문자열 filepath = "이미지의 경로"; 파일 = 새 파일 (filepath); BufferedImage image = imageio.read (file) ;; luminancesource source = new bufferedimageluminancesource (이미지); Binarizer binarizer = 새로운 하이브리드 비나 라이저 (소스); BinaryBitMap BinaryBitMap = New BinaryBitMap (Binarizer); 지도 힌트 = new Hashmap (); hints.put (encodehinttype.character_set, "utf-8"); 결과 결과 = formatreader.decode (binarybitMap, 힌트); System.out.println ( "result ="+ result.toString ()); System.out.println ( "resultformat ="+ result.getBarcodeFormat ()); System.out.println ( "resulttext ="+ result.getText ()); tch (예외 e) {e.printstacktrace ();이제이 방법으로 콘솔에서 QR 코드의 내용을 볼 수 있습니다.
위는 Zxing-Core를 통해 QR 코드를 생성하고 분석하는 방법에 대한 관련 지식입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!