Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BitmapFactory.decodeByteArray() is returning NULL

Tags:

I am using the previewCallback from the camera to try and grab images. Here is the code I am using

private Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback()  {         public void onPreviewFrame( byte[] data, Camera Cam ) {                 Log.d("CombineTestActivity", "Preview started");                 Log.d("CombineTestActivity", "Data length = "                          + data.length );                 currentprev = BitmapFactory.decodeByteArray( data, 0,                          data.length );                 if( currentprev == null )                    Log.d("CombineTestActivity", "currentprev is null" );                  Log.d("CombineTestActivity", "Preview Finished" );          } }; 

the length of the data always comes otu the same as 576000.

Also I have tried changing the parameters of the camera so the image comes back as different formats. Here is what it looks like when I do that.

mCamera = Camera.open(); camParam = mCamera.getParameters(); camParam.setPreviewFormat( ImageFormat.RGB_565 ); mCamera.setParameters( camParam );     mCamera.setPreviewCallback( mPrevCallback ); 

However both when I change the preview format and when I leave it as its default of NV21, BitmapFactory.decodeByteArray comes back as null. I have also tried changing the preview format to JPEG type. I even get a debug statement in the ddms, this is what I get

"D/skia (14391): --- SkImageDecoder::Factory returned null"

like image 574
RyoxSinfar Avatar asked Jul 26 '10 19:07

RyoxSinfar


2 Answers

Alright, hopefully this will help.

Scoured the internet looking for a fast solution, and found the perfect thing.

This works as of Android 2.1

Thanks to off3nsiv3 from this page.

// Convert to JPG Size previewSize = camera.getParameters().getPreviewSize();  YuvImage yuvimage=new YuvImage(data, ImageFormat.NV21, previewSize.width, previewSize.height, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvimage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 80, baos); byte[] jdata = baos.toByteArray();  // Convert to Bitmap Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length); 

Just a little modification to off3nsiv3's code and you're set. The FPS is still incredibly high compared to manual decoding.

For the above code, the 80 is the jpeg quality (0 from 100, 100 being best).

like image 98
Qix - MONICA WAS MISTREATED Avatar answered Oct 07 '22 06:10

Qix - MONICA WAS MISTREATED


you can try this and it is working...

mCamera.setOneShotPreviewCallback(new Camera.PreviewCallback() {                 @Override                 public void onPreviewFrame(byte[] data, Camera camera) {                     Camera.Parameters parameters = camera.getParameters();                     int format = parameters.getPreviewFormat();                     //YUV formats require more conversion                     if (format == ImageFormat.NV21 || format == ImageFormat.YUY2 || format == ImageFormat.NV16) {                         int w = parameters.getPreviewSize().width;                         int h = parameters.getPreviewSize().height;                         // Get the YuV image                         YuvImage yuv_image = new YuvImage(data, format, w, h, null);                         // Convert YuV to Jpeg                         Rect rect = new Rect(0, 0, w, h);                         ByteArrayOutputStream output_stream = new ByteArrayOutputStream();                         yuv_image.compressToJpeg(rect, 100, output_stream);                         byte[] byt = output_stream.toByteArray();                         FileOutputStream outStream = null;                         try {                             // Write to SD Card                             File file = createFileInSDCard(FOLDER_PATH, "Image_"+System.currentTimeMillis()+".jpg");                             //Uri uriSavedImage = Uri.fromFile(file);                             outStream = new FileOutputStream(file);                             outStream.write(byt);                             outStream.close();                         } catch (FileNotFoundException e) {                             e.printStackTrace();                         } catch (IOException e) {                             e.printStackTrace();                         } finally {                         }                     }                 } 
like image 27
ROHIT PARMAR Avatar answered Oct 07 '22 05:10

ROHIT PARMAR