2015-04-22 14 views
7

Tôi đang sử dụng thư viện ZXing ứng dụng để tạo mã QR. Tôi muốn tạo mã QR phù hợp với chiều rộng của màn hình (có thể là một số phần đệm nhỏ).Android: Mã QR được tạo bằng Zxing có lề (không vừa với khu vực)

Nếu tôi đặt chiều rộng của màn hình là kích thước chiều rộng của mã QR, tôi nhận được mã QR nhỏ hơn. Nhìn vào ảnh chụp màn hình (độ phân giải 320x240). Tôi muốn mã QR để phù hợp với khu vực màu đen. Tại sao mã QR màu đỏ quá nhỏ?

Làm cách nào để kéo dài vùng màu đen?

From the app

Mã của tôi:

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x; 

Bitmap bm = encodeAsBitmap(mGeneratedURL, BarcodeFormat.QR_CODE, width, width); 
qrcodeImage.setImageBitmap(bm); 

Tạo QR code:

private Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException { 
    String contentsToEncode = contents; 
    if (contentsToEncode == null) { 
     return null; 
    } 
    Map<EncodeHintType, Object> hints = null; 
    String encoding = guessAppropriateEncoding(contentsToEncode); 
    if (encoding != null) { 
     hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); 
     //hints.put(EncodeHintType.CHARACTER_SET, encoding); 
     hints.put(EncodeHintType.MARGIN, 0); /* default = 4 */ 
    } 
    MultiFormatWriter writer = new MultiFormatWriter(); 
    BitMatrix result; 
    try { 
     result = writer.encode(contentsToEncode, format, img_width, img_height, hints); 
    } catch (IllegalArgumentException iae) { 
     // Unsupported format 
     return null; 
    } 

    int width = result.getWidth(); 
    int height = result.getHeight(); 
    int[] pixels = new int[width * height]; 
    for (int y = 0; y < height; y++) { 
     int offset = y * width; 
     for (int x = 0; x < width; x++) { 
      pixels[offset + x] = result.get(x, y) ? RED : Color.BLACK; 
     } 
    } 

    Bitmap bitmap = Bitmap.createBitmap(width, height, 
      Bitmap.Config.ARGB_8888); 
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height); 
    return bitmap; 
} 

Trả lời

8

tôi thấy một vấn đề!

dòng này:

String encoding = guessAppropriateEncoding(contentsToEncode); 

trả về NULL

Vì vậy, Nó doesn't thiết

EncodeHintType.MARGIN. 

Tháo codition và nó cần được ok.

//if (encoding != null) { 
    hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); 
    //hints.put(EncodeHintType.CHARACTER_SET, encoding); 
    hints.put(EncodeHintType.MARGIN, 0); /* default = 4 */ 
//} 
+1

Chỉ cần lưu ý rằng lý do này tồn tại. Khu vực lề (được gọi là vùng yên tĩnh) được đặt trên hình ảnh theo thiết kế để cung cấp kết quả tốt nhất có thể khi quét. Việc giảm hoặc loại bỏ vùng yên tĩnh có thể khiến một số máy quét gặp khó khăn hơn trong việc quét mã nhanh chóng và chính xác trong điều kiện tối ưu như ánh sáng yếu, tuy nhiên trong hầu hết các trường hợp, nó sẽ không tạo ra sự khác biệt. – BrentM

Các vấn đề liên quan