2016-04-22 9 views
5

Ho cercato di elaborare un'immagine non appena la foto è stata scattata, ad esempio nel callback onPictureTaken(). Come è stato a mio avviso, dovrei convertire l'array di byte in una matrice OpenCV, ma l'intera app si blocca quando provo a farlo. Fondamentalmente tutto ciò che ho fatto è stato questo:Come elaborare un'immagine con OpenCV onPictureTaken?

@Override 
public void onPictureTaken(byte[] bytes, Camera camera) { 
    Log.w(TAG, "picture taken!"); 

    if (bytes != null) { 
     Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
     Mat matImage = new Mat(); 

     // This is where my app freezes. 
     Utils.bitmapToMat(image, matImage); 

     Log.w(TAG, matImage.dump()); 
    } 

    mCamera.startPreview(); 
    mCamera.setPreviewCallback(this); 
} 

Qualcuno sa perché si blocca e come risolverlo?

Nota: Ho usato il tutorial 3 di OpenCV4Android come base.

Update 1: Ho anche cercato di parste i byte (senza successo) come segue:

Mat mat = Imgcodecs.imdecode(
    new MatOfByte(bytes), 
    Imgcodecs.CV_LOAD_IMAGE_UNCHANGED 
); 

Aggiornamento 2: Presumibilmente questo dovrebbe funzionare, non ha fatto per me però.

Mat mat = new Mat(1, bytes.length, CvType.CV_8UC3); 
mat.put(0, 0, bytes); 

Né questa variante:

Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC1); 
mat.put(0, 0, bytes); 

Update 3: Questo non ha funzionato neanche per me:

Mat mat = new MatOfByte(bytes); 

risposta

0

Ho un po 'aiuto di un mio collega . Egli è riuscito a risolvere il problema nel modo seguente:

BitmapFactory.Options opts = new BitmapFactory.Options(); // This was missing. 
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts); 

Mat mat = new Mat(); 
Utils.bitmapToMat(bitmap, mat); 

// Note: when the matrix is to large mat.dump() might also freeze your app. 
Log.w(TAG, mat.size()); 

Speriamo che questo ti aiuto a tutti voi che sono anche alle prese con questo.