QR 코드의 Java 생성을위한 많은 개발 JAR 패키지가 있습니다. 예를 들어 Zxing은 Google에서 개발했습니다. 여기서 Zxing의 개발 패키지를 사용하여 구현합니다. 많은 프로젝트에서 QR 코드를 동적으로 생성하여 사용자에게 제공하여 더 많은 사람들이 QR 코드를 통해 응용 프로그램을 잘 경험할 수 있도록해야합니다.
코드는 아래에 게시되었으며 테스트되었으며 코드를 직접 복사 할 수 있습니다.
마지막으로 결과를 달성했습니다.
Java는 QR 코드를 생성합니다
코드는 다음과 같습니다.
import java.awt.color; import java.awt.graphics2d; import java.awt.image; import java.awt.geom.affinetransform; import java.awt.image.affinetransformop; import java.awt.image.bufferedimage; import java.io.file; java.io.unsupportedencodingException; import java.util.hashmap; import java.util.map; import javax.imageio.imageio; 가져 오기 com.google.zxing.barcodeformat; import com.google.zxing.encodehinttype; import com.google.zxing.multiformatwriter; import com.google.zxing.writexception; import com.google.zxing.common.bitmatrix; import com.google.zxxing.decoder.decoder.decoder.decoder. 공개 클래스 matrixtoImageWriter {private static final int image_width = 100; 비공개 정적 최종 int image_height = 100; 비공개 정적 최종 int image_half_width = image_width / 2; 비공개 정적 최종 int frame_width = 2; 개인 정적 multiformatwriter mutiwriter = new multiformatwriter (); public static void encode (문자열 내용, int 너비, int 높이, 문자열 srcimagepath, string destimagepath) {try {imageio.write (genbarcode (내용, 너비, 높이, srcimagepath), "jpg", 새 파일 (destimagepath)); } catch (ioexception e) {e.printstacktrace (); } catch (WriteRexception e) {e.printstacktrace (); }} private static bufferedimage genbarcode (문자열 내용, int 너비, int 높이, 문자열 srcimagepath) writeRexception, ioexception {bufferedImage scaleimage = scale (srcimagepath, image_width, image_height, true); int [] [] srcpixels = new int [image_width] [image_height]; for (int i = 0; i <scaleimage.getWidth (); i ++) {for (int j = 0; }} map <EncodeHintType, Object> hint = new Hashmap <EncodeHintType, Object> (); hint.put (encodehinttype.character_set, "utf-8"); hint.put (encodehinttype.error_correction, errorcorrectionlevel.h); // QR 코드 생성 BitMatrix matrix = mutiwriter.encode (content, barcodeformat.qr_code, 너비, 높이, 힌트); // 2 차원 행렬을 1 차원 픽셀 배열로 변환 int halfw = matrix.getWidth () / 2; int halfh = matrix.getheight () / 2; int [] pixels = new int [너비 * 높이]; for (int y = 0; y <matrix.getheight (); y ++) {for (int x = 0; x <matrix.getWidth (); x ++) {// 왼쪽 상단 코너의 색상, 필요에 따라 색상 범위와 색상을 조정합니다 (x> 0 && x <170 && y <170) {New Color (231, 144); int colorint = color.getrgb (); 픽셀 [y * width + x] = matrix.get (x, y)? Colorint : 16777215; } // if (x> halfw -image_half_width && x <Halfw + image_half_width && y> alfh- im image_half_width]; } else if ((x> halfw -image_half_width -frame_width && hallw -im im frame_width && y <Halfh -Image_half_width + frame_width) || > HalfH + image_half_width -frame_width && y <Halfh + image_half_width + frame_width)) {pixels [y * width + x] = 0xffffffffff; // 그림 주위에 테두리를 형성} else {// qr 코드 색상 int num1 = (int) (50- (50.0-13.0) / matrix.getheight () * (y + 1)); int num2 = (int) (165- (165.0-72.0) / matrix.getheight () * (y + 1)); int num3 = (int) (162- (162.0-107.0) / matrix.getheight () * (y + 1)); 색상 = 새로운 색상 (Num1, Num2, Num3); int colorint = color.getrgb (); // 여기서 QR 코드 색상을 수정할 수 있으며 각각 QR 코드와 배경색을 공식화 할 수 있습니다. 픽셀 [y * width + x] = matrix.get (x, y)? Colorint : 16777215; // 0x00000 : 0xffffff}}} bufferedImage image = new bufferedImage (너비, 높이, bufferedImage.type_int_rgb); image.getRaster (). setDataelements (0, 0, 너비, 높이, 픽셀); 리턴 이미지; } private static bufferedImage scale (String srcimagefile, int 높이, int 너비, 부울 하세 필러)는 ioexception {double ratio = 0.0; // scale file file = 새 파일 (srcimageFile); BufferedImage srcimage = imageio.Read (파일); image destimage = srcimage.getScaledInstance (너비, 높이, bufferedImage.scale_smooth); // if ((srcimage.getheight ()> height) || (srcimage.getwidth ()> width)) {if (srcimage.getheight ()> srcimage.getWidth ()) {ratio = (new Inter (Height)). } else {ratio = (새 정수 (width)). doubleValue () / srcimage.getWidth (); } affinetransformop op = new affinetransformop (affinetransform.getScaleInstance (비율, 비율), null); destimage = op.filter (srcimage, null); } if (hasfiller) {// bufferedImage image = new bufferedImage (너비, 높이, bufferedImage.type_int_rgb); Graphics2d Graphic = image.creategraphics (); Graphic.setColor (Color.white); graphic.fillRect (0, 0, 너비, 높이); if (width == destimage.getWidth (null)) graphic.DrawImage (destimage, 0, (높이 -Destimage.getheight (null)) / 2, destimage.getwidth (null), destimage.getheight (null), color.white, null); else Graphic.DrawImage (destimage, (width -destimage.getWidth (null)) / 2, 0, destimage.getWidth (null), destimage.getheight (null), color.white, null); Graphic.Dispose (); destimage = 이미지; } return (bufferedImage) destimage; } public static void main (string [] args)은 unsupportedencodingexception {// 컨텐츠, 내용 (중국어로 지원되지 않음), 폭, 길이, 중간 아이콘 경로, 스토리지 경로, 스토리지 경로, 스토리지 경로, 스토리지 경로, 512, 512, "d : //logo.png", "D : //2013-01.jpg"); }}당신은 주제를 참조 할 수 있습니다 : Java QR 학습을위한 코드
위의 내용은이 기사에 관한 것이므로 자신의 QR 코드를 설계하는 데 도움이됩니다.