0

So I'm trying to use face detection with a camera, so I need to convert the byte array supplied to the preview callback, into a bitmap, using this code:

Camera.PreviewCallback previewCallback=new Camera.PreviewCallback()
{
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) 
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
        if(mBitmap==null) faceDetected=false;
        else              faceDetected=(findFace(mBitmap)!=null);            
    }
};

Unfortunately, mBitmap is always null, and options outHeight and outWidth are always -1 - which indicates a decode error. Naturally, there are no diagnostics, so it's impossible to fix.

user3660664
  • 237
  • 3
  • 8
  • `BitmapFactory` may not handle the image format used by camera preview frames (e.g., YUV). – CommonsWare May 02 '15 at 10:52
  • This is possibly true, but not very helpful ;-) Do you know this for sure, in which case do you know how to solve the problem? – user3660664 May 02 '15 at 13:42
  • http://stackoverflow.com/questions/20298699/onpreviewframe-data-image-to-imageview and http://stackoverflow.com/questions/5212531/android-byte-to-image-in-camera-onpreviewframe turn up with a Google search on `android onpreviewframe imageview`. I'd start there. – CommonsWare May 02 '15 at 13:55
  • Thank you. One of the links you sent links to other code which converts to BMP via YUV. Utterly ridiculous, but it works. ;-) I'm still gobsmacked that decodeByteArray doesn't work with the default camera preview format. – user3660664 May 03 '15 at 07:41

0 Answers0