소개하다
goole의 zxing 패키지는 주로 사용됩니다. 다음은 샘플 코드로, 모든 사람이 이해하고 배우는 데 매우 편리합니다. 이 코드는 예비 프레임 워크에 속하며 기능이 있으며 실제 사용에 따라 개선되고 최적화되어야합니다.
1 단계 : Maven import zxing
<pectionency> <groupId> com.google.zxing </groupid> <artifactid> core </artifactid> <bersion> 3.2.1 </version> </fectionency>
2 단계 : QR 코드 생성 시작 :
개인 정적 최종 int black = 0xff000000; private static final int white = 0xffffffff;/*** zxing 패키지* @return*/public static bufferedimage tobufferedimage (bitmatrix magrix) {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); }} return image;}/*** QR 코드를 생성하고 파일에 쓰기* @param 컨텐츠 QR 코드의 내용을 스캔* @param 형식 이미지 형식 jpg* @param 파일 예외*/public static void writeTofile (문자열 내용, 문자열 형식, 파일 작성자 {multiformatatwriter = new MultiforatWriter (). @suppresswarnings ( "rawtypes") map hints = new Hashmap (); // 중국어가 달린 힌트를 방지하기 위해 UTF-8을 설정합니다. // QR 코드 주위에 흰색 영역의 크기를 설정합니다. // QR 코드 hints.put (encodehinttype.error_correction, errorcorrectionlevel.h)의 결함 공차 설정; // QR 코드 BitMatrix bitmatrix = multiformatwriter.encode (content, barcodeformat.qr_code, 너비, 높이, 힌트); BufferedImage image = TobufferedImage (bitmatrix); if (! imageio.write (image, format, file)) {trash new ioException ( "" + format + "형식의 이미지를" + 파일에 쓸 수 없음); }}3 단계 : QR 코드 사진에 텍스트를 작성하십시오.
/*** QR 코드 이미지에 텍스트 추가* @param presstext text* @param qrfile qr 코드 파일* @param fontstyle* @param fontsize*/public static void xttext (String presstext, 파일 qrfile, int fontstyle, color color, int fontsize) 예외 {presstext (presttextextes) "UTF-8"); image src = imageio.read (qrfile); int imagew = src.getWidth (null); int imageh = src.getheight (null); BufferedImage Image = New BufferedImage (ImageW, ImageH, BufferedImage.type_int_rgb); 그래픽 g = image.creategraphics (); g.DrawImage (src, 0, 0, imagew, imageh, null); // 브러시의 색상을 설정합니다. G.SetColor (색); // 글꼴을 설정하여 글꼴을 설정합니다. FontMetrics Metrics = G.GetFontMetrics (Font); // 그림의 텍스트 좌표는 중간에 설정되어 int startx = (width -metrics.stringWidth (pressText)) /2; int Starty = 높이/2; G. 세트 폰트 (글꼴); G.DrawString (pressText, startX, Starty); g.dispose (); fileoutputStream out = 새 FileOutputStream (qrfile); imageio.write (image, "jpeg", out); out.close (); System.out.println ( "이미지 프레스 성공");}4 단계 : 기본 방법에서 테스트 중간에 텍스트가있는 QR 코드가 생성됩니다.
public static void main (string [] args)은 예외 {file qrcfile = 새 파일 ( "/users/deweixu/", "google.jpg"); WriteTofile ( "www.google.com.hk", "jpg", qrcfile); PressText ( "Google", QRCFile, 5, Color.red, 32);}요약
위는이 기사의 전체 내용입니다. 이 기사의 내용이 귀하의 연구 나 업무에 도움이되기를 바랍니다. 궁금한 점이 있으면 의사 소통을 위해 메시지를 남길 수 있습니다.