2017-03-16 3 views
2

아래 코드는 QR 코드를 성공적으로 생성합니다.ZXing ("Zebra Crossing") API를 사용하여 Java에서 QR 코드 생성

QR 코드의 높이와 너비는 입력 qrCodeData에 따라 달라집니다.

폭스 예의 경우 qrCodeData = Hello World! 그러면 생성 된 QR 코드의 높이와 너비가 낮습니다. qrCodeData = 휴대 전화는 사용자가 전화 서비스 지역 내에서 이동하는 동안 무선 주파수 링크를 통해 전화를 걸고받을 수있는 휴대 전화입니다. 무선 주파수 링크는 PSTN (Public Switched Telephone Network)에 대한 액세스를 제공하는 이동 통신 사업자의 스위칭 시스템에 대한 연결을 설정 한 다음 생성 된 QR 코드의 높이와 너비가 더 큽니다.

첨부 된 샘플 QR 코드. for less datafor large data

나는 동일한 높이와 QR 코드를 생성하고 qrCodeData에서 주어진 데이터에 관계없이 폭 싶다. 어떤 사람들은 저에게 조언합니다. 미리 감사드립니다.

package com.javapapers.java; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.HashMap; 
import java.util.Map; 

import javax.imageio.ImageIO; 

import com.google.zxing.BarcodeFormat; 
import com.google.zxing.BinaryBitmap; 
import com.google.zxing.EncodeHintType; 
import com.google.zxing.MultiFormatReader; 
import com.google.zxing.MultiFormatWriter; 
import com.google.zxing.NotFoundException; 
import com.google.zxing.Result; 
import com.google.zxing.WriterException; 
import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 
import com.google.zxing.client.j2se.MatrixToImageWriter; 
import com.google.zxing.common.BitMatrix; 
import com.google.zxing.common.HybridBinarizer; 
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 

public class QRCode { 

public static void main(String[] args) throws WriterException, IOException, 
     NotFoundException { 
String qrCodeData = "Hello World!"; 
String filePath = "QRCode.png"; 
String charset = "UTF-8"; // or "ISO-8859-1" 
Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new                  HashMap<EncodeHintType, ErrorCorrectionLevel>(); 
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); 

    createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200); 
    System.out.println("QR Code image created successfully!"); 

    System.out.println("Data read from QR Code: " 
      + readQRCode(filePath, charset, hintMap)); 

} 

public static void createQRCode(String qrCodeData, String filePath, 
     String charset, Map hintMap, int qrCodeheight, int qrCodewidth) 
     throws WriterException, IOException { 
    BitMatrix matrix = new MultiFormatWriter().encode(
      new String(qrCodeData.getBytes(charset), charset), 
      BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap); 
    MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath 
      .lastIndexOf('.') + 1), new File(filePath)); 
} 

public static String readQRCode(String filePath, String charset, Map hintMap) 
     throws FileNotFoundException, IOException, NotFoundException { 
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
      new BufferedImageLuminanceSource(
        ImageIO.read(new FileInputStream(filePath))))); 
    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap, 
      hintMap); 
    return qrCodeResult.getText(); 
} 
} 
+0

ZXing는 암호화 할 수 없습니다. DP에서 고정 된 크기의'ImageView' (QR 코드를 표시)에'warp_content'를 사용하지 않으시겠습니까? 생성 된 비트 맵은 커지지 만 컨테이너에 맞게 축소됩니다. 그러나, 독자가 여전히 축소 된 QR 코드를 읽을 수 있는지 확인하십시오 ;-) – mithrop

+0

답장 mithrop 주셔서 감사합니다. – Parthiban

답변

1

화상이 (더 정확하게 할 수 있습니다 : 이미지의 픽셀 부품), 을 다른 크기가 주위에 흰색 여백이 있기 때문에.

Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>(); 
hintMap.put(EncodeHintType.MARGIN, 0); 
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); 

createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200); 

이 여백의 QR code quiet zone 소위가, 따라서 4 의 폭이 기본적으로 이처럼 main 방법을 수정 0
margin 설정할 수 그것을 제거하기 그러면 결과 이미지에 여백이없고 픽셀 부품의 크기가 같습니다. 충분한 공간이없는 경우

enter image description here enter image description here

+0

토마스에게 감사드립니다. 이제 데이터와 상관없이 동일한 높이와 너비의 QR을 얻을 수 있습니다. – Parthiban

+0

@Parthiban 환영합니다. 이 방법으로 문제가 해결되면 대답을 수락하는 것이 좋습니다. –