2012-02-12 64 views
0

我正在使用Android Zxing库来解码QR代码,该代码必须从相机预览帧中实时提取。问题是我必须使用OpenCV来执行QR码检测,而不要求用户捕获图像。任何人都可以告诉我: 如何使用相机的框架? 如何使用OpenCV对这些帧进行QR检测,而不捕获图像? 什么算法用于QR码检测? 此外,如果有人能告诉我什么函数,使用的库以及一些示例代码可以帮助我,我将不胜感激。如何使用OpenCV在Android中检测并从预览帧中提取QR码?

更新:这就是我现在所做的: 使用预览框,其解码为Byte数组,然后把它传递给RGBLuminance
公共无效surfaceCreated(SurfaceHolder持有者){

// The Surface has been created, acquire the camera and tell it where 
    // to draw. 

    camera = Camera.open(); 
    try { 
     camera.setPreviewDisplay(holder); 

     camera.setPreviewCallback(new PreviewCallback() { 

      public void onPreviewFrame(byte[] data, Camera arg1) { 
       boolean shouldCall = (System.currentTimeMillis() - lastTime) > 1000; 
       if (shouldCall) { 
        lastTime = System.currentTimeMillis(); 
        //slow work 


       Camera.Parameters parameters = camera.getParameters(); 
       Size size = parameters.getPreviewSize(); 

       Bitmap bMap1 = BitmapFactory.decodeByteArray(data, 0, data.length); 
       TextView textv = (TextView) findViewById(R.id.mytext); 
       LuminanceSource source = new RGBLuminanceSource(bMap1); 

       BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 
       Reader reader = new MultiFormatReader(); 

       try { 
        Result result = reader.decode(bitmap); 
        text = result.getText(); 
         byte[] rawBytes = result.getRawBytes(); 
         if (rawBytes!= null) 
          camera.stopPreview(); 
         BarcodeFormat format = result.getBarcodeFormat(); 
         ResultPoint[] points = result.getResultPoints(); 
         ParsedResult result2 = parseResult(result); 

         textv.setText(text); 



       } catch (NotFoundException e) { 
        camera.startPreview(); 
        e.printStackTrace(); 
       } catch (ChecksumException e) { 
        text = "Checksum Error"; 
        camera.stopPreview(); 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (FormatException e) { 
        text = "Format Error"; 
        camera.stopPreview(); 
        // TODO Auto-generated catch block 
      e.printStackTrace(); 


       } 

       lastTime = System.currentTimeMillis(); 
       } 



      } 
     }); 
    } catch (IOException e) { 
     camera.startPreview(); 
    } 
} 

但这是行不通的。有人可以告诉我我做错了什么吗?另外,我在其他代码片段中使用了相同的解码实例,我只是简单地拍摄并解码它。但每次图片不包含QR时,该应用程序都会以关闭的方式崩溃。我该怎么做?有人请帮助

回答

0

不确定,如果您已经使用了zxing,那么您已经拥有了解码Android相机帧QR码的代码,句号。你还需要什么 - 为什么要打开OpenCV?

+0

它必须被检测到。我无法使用图像文件。正如我所提到的,我必须使用相机预览帧来检测代码。这就是OpenCV进来的地方。 – 2012-02-13 07:47:04

+0

是的,正如我所说的,库不必解码图像文件。实际上,它包括从相机预览框(不是照片)中检测条形码的代码。如果因为某些原因必须使用OpenCV,那么确定,但如果您不这样做,那么您已经拥有完整的解决方案。 – 2012-02-13 09:01:12