2016-03-30 154 views
0

我想使用網絡攝像頭從一張紙上讀取QR碼,但它一直不合格 - 它不會打印。我一直堅持這個問題好幾天了。從網絡攝像頭讀取QR碼

這裏是我的代碼:

公共無效imageUpdated(形象畫像) {

LuminanceSource ls = new BufferedImageLuminanceSource((BufferedImage)image); 
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(ls)); 
    QRCodeReader qrReader = new QRCodeReader(); 
    try { 
     Result result = qrReader.decode(bitmap); 
     System.out.println("QR Code data is: "+result.getText()); 
    } catch (NotFoundException e) { 
     // TODO Auto-generated catch block 
     System.out.println("--------"); 
    } catch (ChecksumException e) { 
     // TODO Auto-generated catch block 
     System.out.println("--------"); 
    } catch (FormatException e) { 
     // TODO Auto-generated catch block 
     System.out.println("--------"); 
    } 
    qrReader.reset(); 
} 

這段代碼看上去很奇怪的,你的人?任何幫助表示讚賞,謝謝:)

+0

這取決於圖像質量。您可以嘗試在解碼方法中添加指令'try harder':Map hints = new TreeMap <>(); \t \t hints.put(DecodeHintType.TRY_HARDER,null);結果結果= qrReader.decode(位圖,提示);' – LibertyPaul

+0

它仍然不起作用..但是,謝謝你的回答:) – Jacob

+0

你能提供你想要解碼的圖像的例子嗎? – LibertyPaul

回答

0

我發現的方法,我用我的框架(墊目標)到BufferedImage的轉換沒有工作。這張照片全是黑色的。在StackOverflow上找到這個代碼對我有效:

public Image toBufferedImage(Mat m){ 
    int type = BufferedImage.TYPE_BYTE_GRAY; 
    if (m.channels() > 1) { 
     type = BufferedImage.TYPE_3BYTE_BGR; 
    } 
    int bufferSize = m.channels()*m.cols()*m.rows(); 
    byte [] b = new byte[bufferSize]; 
    m.get(0,0,b); // get all the pixels 
    BufferedImage image = new BufferedImage(m.cols(),m.rows(), type); 
    final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 
    System.arraycopy(b, 0, targetPixels, 0, b.length); 
    return image; 
}