2013-02-13 5 views
4

OpenCV와 Zxing을 사용하고 있으며 2 차원 코드 스캐닝을 추가하고 싶습니다. 나는 보낼 수있는 몇 가지 유형의 이미지가 있습니다. 아마도 Bitmat가 가장 좋습니다 (다른 옵션은 OpenCV Mat입니다). 이 같은 변환 할 수 있도록 예전처럼ZXing 비트 맵을 바이너리 비트 맵으로 변환

는 같습니다 : 더 이상 입력으로 비트 맵을지지 않습니다처럼

Bitmap frame = //this is the frame coming in 

LuminanceSource source = new RGBLuminanceSource(frame); 
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 

//then I can use reader.decode(bitmap) to decode the BinaryBitmap 

그러나, RGBLuminaceSource 보인다. 그렇다면 어떻게 입력 이미지를 BinaryBitmap으로 변환 할 수 있습니까 ???

편집 : 나는 약간의 진전을했지만, 나는 아직도 문제에 봉착 생각 때문에

좋아. 그러나 나는 내가 지금 arrayIndexOutOfBounds

public void zxing(){ 
    Bitmap bMap = Bitmap.createBitmap(frame.width(), frame.height(), Bitmap.Config.ARGB_8888); 
    Utils.matToBitmap(frame, bMap); 
    byte[] array = BitmapToArray(bMap); 
    LuminanceSource source = new PlanarYUVLuminanceSource(array, bMap.getWidth(), bMap.getHeight(), 0, 0, bMap.getWidth(), bMap.getHeight(), false); 

    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 
    Reader reader = new DataMatrixReader(); 
    String sResult = ""; 
    try { 
     Result result = reader.decode(bitmap); 
     sResult = result.getText(); 
     Log.i("Result", sResult); 
     } 
    catch (NotFoundException e) { 
      Log.d(TAG, "Code Not Found"); 
      e.printStackTrace(); 
    } 
} 

public byte[] BitmapToArray(Bitmap bmp){ 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream); 
    byte[] byteArray = stream.toByteArray(); 
    return byteArray; 
} 

내가

02-14 10:19:27.469: E/AndroidRuntime(29736): java.lang.ArrayIndexOutOfBoundsException: length=33341; index=34560 02-14 10:19:27.469: E/AndroidRuntime(29736): at 
com.google.zxing.common.HybridBinarizer.calculateBlackPoints(HybridBinarizer.java:199) 

내가 바이트의 크기를 [로그인 한 오류를 얻을

을 얻고, 내가 올바른 형식으로 비트 맵 변환 코드를 생각] , 그리고 위에 표시된 길이입니다. 나는 zxing이 더 큰 것으로 기대하는 이유를 파악할 수 없다.

답변

7

알았어. Sean Owen이 말했듯이, PlanarYUVLuminaceSource는 OpenCV가 사용하지 않는 기본 안드로이드 카메라 형식 일뿐입니다. 그래서 짧게, 당신이 그것을 어떻게 할 것입니다 :

//(note, mTwod is the CV Mat that contains my datamatrix code) 

Bitmap bMap = Bitmap.createBitmap(mTwod.width(), mTwod.height(), Bitmap.Config.ARGB_8888); 
Utils.matToBitmap(mTwod, bMap); 
int[] intArray = new int[bMap.getWidth()*bMap.getHeight()]; 
//copy pixel data from the Bitmap into the 'intArray' array 
bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight()); 

LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(),intArray); 

BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 
Reader reader = new DataMatrixReader();  
//....doing the actually reading 
Result result = reader.decode(bitmap); 

그래서 그게 전혀 어렵지 않습니다. 안드로이드 비트 맵을 정수형 배열로 변환하고 그 조각을 케이크 조각으로 변환해야했습니다.

2

Bitmap은 안드로이드 클래스이다. 카메라의 Android 기본 이미지 형식은 평면 YUV 형식입니다. 그렇기 때문에 PlanarYUVLuminanceSource 만 필요하며 Android 용으로 제공됩니다. RGBLuminanceSource을 이식해야합니다.

잘못된 종류의 데이터를 완전히 클래스에 넣습니다. YUV 평면 형식의 픽셀을 기대합니다. JPEG 파일의 압축 바이트를 전달 중입니다.

+0

설명해 주셔서 감사합니다. 비트 맵을 int []로 변환 한 다음 RGBLuminanceSource 생성자를 사용하여 작업하고 있다고 생각합니다. – Jameo

6
public static String readQRImage(Bitmap bMap) { 
    String contents = null; 

    int[] intArray = new int[bMap.getWidth()*bMap.getHeight()]; 
    //copy pixel data from the Bitmap into the 'intArray' array 
    bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight()); 

    LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray); 
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 

    Reader reader = new MultiFormatReader();// use this otherwise ChecksumException 
    try { 
     Result result = reader.decode(bitmap); 
     contents = result.getText(); 
     //byte[] rawBytes = result.getRawBytes(); 
     //BarcodeFormat format = result.getBarcodeFormat(); 
     //ResultPoint[] points = result.getResultPoints(); 
    } catch (NotFoundException e) { e.printStackTrace(); } 
    catch (ChecksumException e) { e.printStackTrace(); } 
    catch (FormatException e) { e.printStackTrace(); } 
    return contents; 
} 
관련 문제