Java: 간단한 QR 코드 생성기 예제 – 무료로 QR 코드 생성
게시 됨: 2020-01-16
스캔: https://crunchify.com
으로 리디렉션됩니다.
QR code
( Quick Response Code
의 약칭)는 일본에서 처음으로 자동차 산업을 위해 설계된 매트릭스 바코드(또는 2차원 바코드) 유형의 상표입니다. 바코드는 항목과 관련된 정보를 기록하는 항목에 부착된 광학 기계 판독 가능 레이블입니다. 처음에 특허를 받았을 때 특허 보유자는 이러한 권리를 행사하지 않기로 결정했습니다. 최근 QR 코드 시스템은 표준 UPC 바코드에 비해 빠른 가독성과 더 큰 저장 용량으로 인해 자동차 산업 외부에서 인기를 얻고 있습니다.
코드는 흰색 배경에 정사각형 격자로 배열된 검은색 모듈(사각형 점)로 구성됩니다. ZXING
은 Android, Java용 클라이언트가 있는 다중 형식 1D/2D 바코드 이미지 처리 라이브러리입니다. Java로 구현된 오픈 소스, 다중 형식 1D/2D 바코드 이미지 처리 라이브러리이며 다른 언어에 대한 포트가 있습니다.
체크아웃 라이브 예시:
우리의 초점은 서버와 통신하지 않고 휴대폰에 내장된 카메라를 사용하여 장치의 바코드를 스캔하고 디코딩하는 것입니다. 그러나 프로젝트는 데스크톱 및 서버에서도 바코드를 인코딩 및 디코딩하는 데 사용할 수 있습니다.
다음은 QR 코드를 생성하는 간단한 Java 코드입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
package crunchify . com . tutorials ; import com . google . zxing . BarcodeFormat ; import com . google . zxing . EncodeHintType ; import com . google . zxing . WriterException ; import com . google . zxing . common . BitMatrix ; import com . google . zxing . qrcode . QRCodeWriter ; import com . google . zxing . qrcode . decoder . ErrorCorrectionLevel ; import javax . imageio . ImageIO ; import java . awt . * ; import java . awt . image . BufferedImage ; import java . io . File ; import java . io . IOException ; import java . util . EnumMap ; import java . util . Map ; /** * @author Crunchify.com * Simple QR Code Generator Example - Create QR codes for free. */ public class CrunchifyQRCodeGenerator { public static void main ( String [ ] args ) { String myCodeText = "https://crunchify.com" ; String filePath = "//cdn.crunchify.com/Users/app/Document/Crunchify.com-QRCode.png" ; int size = 512 ; String crunchifyFileType = "png" ; File crunchifyFile = new File ( filePath ) ; try { Map < EncodeHintType , Object > crunchifyHintType = new EnumMap < EncodeHintType , Object > ( EncodeHintType . class ) ; crunchifyHintType . put ( EncodeHintType . CHARACTER_SET , "UTF-8" ) ; // Now with version 3.4.1 you could change margin (white border size) crunchifyHintType . put ( EncodeHintType . MARGIN , 1 ) ; /* default = 4 */ Object put = crunchifyHintType . put ( EncodeHintType . ERROR_CORRECTION , ErrorCorrectionLevel . H ) ; QRCodeWriter mYQRCodeWriter = new QRCodeWriter ( ) ; // throws com.google.zxing.WriterException BitMatrix crunchifyBitMatrix = mYQRCodeWriter . encode ( myCodeText , BarcodeFormat . QR_CODE , size , size , crunchifyHintType ) ; int CrunchifyWidth = crunchifyBitMatrix . getWidth ( ) ; // The BufferedImage subclass describes an Image with an accessible buffer of crunchifyImage data. BufferedImage crunchifyImage = new BufferedImage ( CrunchifyWidth , CrunchifyWidth , BufferedImage . TYPE_INT_RGB ) ; // Creates a Graphics2D, which can be used to draw into this BufferedImage. crunchifyImage . createGraphics ( ) ; // This Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. // This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java(tm) platform. Graphics2D crunchifyGraphics = ( Graphics2D ) crunchifyImage . getGraphics ( ) ; // setColor() sets this graphics context's current color to the specified color. // All subsequent graphics operations using this graphics context use this specified color. crunchifyGraphics . setColor ( Color . white ) ; // fillRect() fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. crunchifyGraphics . fillRect ( 0 , 0 , CrunchifyWidth , CrunchifyWidth ) ; // TODO: Please change this color as per your need crunchifyGraphics . setColor ( Color . BLUE ) ; for ( int i = 0 ; i < CrunchifyWidth ; i ++ ) { for ( int j = 0 ; j < CrunchifyWidth ; j ++ ) { if ( crunchifyBitMatrix . get ( i , j ) ) { crunchifyGraphics . fillRect ( i , j , 1 , 1 ) ; } } } // A class containing static convenience methods for locating // ImageReaders and ImageWriters, and performing simple encoding and decoding. ImageIO . write ( crunchifyImage , crunchifyFileType , crunchifyFile ) ; System . out . println ( "\nCongratulation.. You have successfully created QR Code.. \n" + "Check your code here: " + filePath ) ; } catch ( WriterException e ) { System . out . println ( "\nSorry.. Something went wrong...\n" ) ; e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } } |
출력: (직접 스캔)

어떤 라이브러리를 다운로드해야 합니까?
Maven 프로젝트가 있는 경우 이 종속성을 pom.xml 파일에 포함합니다.

1 2 3 4 5 6 |
< dependency > < groupId > com . google . zxing < / groupId > < artifactId > core < / artifactId > < version > 3.4.1 < / version > < / dependency > |

또는 수동으로 .jar 파일 다운로드
1 단계:
다운로드 링크.

2 단계
다운로드한 후에는 프로젝트의 클래스 경로에 포함해야 합니다. Eclipse에서 프로젝트 빌드 경로에 .jar 파일을 추가하는 방법.

QR 코드 생성에 문제가 있으면 알려주세요.