자바 생 성 을 통 해 QR 코드 상세 설명 읽 기

머리말
시작 하기 전에 QR 코드 생 성 및 읽 기 jar 패 키 지 를 도입 해 야 합 니 다.여 기 는 qrcodejar 를 사용 합 니 다.
자바 프로젝트 를 새로 만 들 고 프로젝트 에 lib 디 렉 터 리 를 추가 합 니 다.두 개의 Jar 패 키 지 를 lib 디 렉 터 리 에 두 고 add as libiary 를 잊 지 마 세 요.


build.gradle 에 설정 추가
compile fileTree(dir:'lib',include:['*.jar'])
(여기 저 는 gradle 을 사용 합 니 다)
준비 작업 이 끝나 면 곧 시작한다.
QR 코드 생 성

public class QRcodeTest {
public static void main(String[] args) throws Exception{
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');//    (  L、M、H    )
qrcode.setQrcodeEncodeMode('B');//N    ,A  a-Z,B      
qrcode.setQrcodeVersion(10);//  
//            
String qrData ="http://www.baidu.com";
//          
int width = 67+12*(10-1);
int height = 67+12*(10-1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//  
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);//       
//      ,       ,       。
int pixoff = 2;
byte[] d = qrData.getBytes("gb2312");
if(d.length > 0 && d.length <120){
boolean[][] s = qrcode.calQrcode(d);
for(int i=0;i<s.length;i++){
for(int j=0;j<s.length;j++){
if(s[j][i]){
gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
//        (       )
ImageIO.write(bufferedImage, "png", new File("E:/code/qrcodebd.png"));
}
}
이렇게 하면 QR 코드 그림 을 만 들 고 QR 코드 를 스 캔 하면 바 이 두 첫 페이지 로 넘 어 갈 수 있다.
QR 코드 사진

public class MyQrCodeImage implements QRCodeImage {
BufferedImage bufferedImage;
public MyQrCodeImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}
public int getHeight() {
return bufferedImage.getHeight();
}
public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}
public int getWidth() {
return bufferedImage.getWidth();
}

}
QR 코드 읽 기

public class ReadQrCode {
public static void main(String[] args) throws IOException {
File file = new File("E:/code/qrcodebd.png");
BufferedImage bufferedImage = ImageIO.read(file);
Qrcode qrcode = new Qrcode();
QRCodeDecoder codeDecoder = new QRCodeDecoder();
String result = new String(codeDecoder.decode(new MyQrCodeImage(bufferedImage)),"gb2312");
System.out.println(result);
}
}
QR 코드 를 읽 는 내용 은http://www.baidu.com
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기