Using BitmapFactory to decode the image byte buffer

All your problems with Audio, Video and Images.

Using BitmapFactory to decode the image byte buffer

Postby underminedsk » Thu Apr 29, 2010 6:08 am

Hey,

I'm using Camera.PreviewCallback to get access to the frame byte buffer, but I'm having some trouble using bitmap factory to decode the array into a jpg. Right now, my BitmapFactory returns null.

note the preview buffer has the following options for pixel format:

PixelFormat.JPEG
PixelFormat.YCbCr_420_SP (default)
PixelFormat.RGB_565

Here's what i am doing:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void onPreviewFrame(byte[] data, Camera camera) {
  3.  
  4.         ...
  5.  
  6.         CameraUtilities.saveImageToDisk(data);
  7.  
  8.  
  9.  
  10.         }
  11.  
  12.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


and the CameraUtilities method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public static String saveImageToDisk(byte[] imageData) {
  3.  
  4.                
  5.  
  6.                 if (imageData == null) {
  7.  
  8.                         Log.e(TAG, "Error: imageData is null - can not save to disk.");
  9.  
  10.                         return null;
  11.  
  12.                 }
  13.  
  14.                
  15.  
  16.                 Log.v(TAG, "Decoding image data...");
  17.  
  18.                 Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
  19.  
  20.                 if (myImage == null) {
  21.  
  22.                         Log.e(TAG, "BitmapFactory did not sucessfully decode the byte array...exiting.");
  23.  
  24.                         return null;
  25.  
  26.                 }
  27.  
  28.                
  29.  
  30.                 Log.v(TAG, "Creating a new image file...");
  31.  
  32.                 File sdImageMainDirectory = new File(SD_DIRECTORY);
  33.  
  34.                
  35.  
  36.                 Log.v(TAG, "Saving image to disk..."); 
  37.  
  38.                 String imagePath = sdImageMainDirectory.toString() +"/" + IMAGE_NAME + ".jpg";
  39.  
  40.                 try {
  41.  
  42.                         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(imagePath), 8000);
  43.  
  44.                         myImage.compress(CompressFormat.JPEG, STORAGE_QUALITY, bos); // important! compress the image down before sending
  45.  
  46.                         bos.flush();
  47.  
  48.                         bos.close();
  49.  
  50.                 } catch (Exception e) {
  51.  
  52.                         Log.e(TAG, "Error - did not sucessfully save the image to disk");
  53.  
  54.                         e.printStackTrace();
  55.  
  56.                         return null;
  57.  
  58.                 }
  59.  
  60.                
  61.  
  62.                 return imagePath;
  63.  
  64.         }
  65.  
  66.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


anyone know more than me about decoding byte arrays?

Thanks
underminedsk
Developer
Developer
 
Posts: 26
Joined: Tue Mar 02, 2010 3:36 am

Top

Postby underminedsk » Thu Apr 29, 2010 5:02 pm

Update: I think I need to use the following method in BitmapFactory:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. myImage = BitmapFactory.decodeStream(new ByteArrayInputStream(imageData));
  3.  
  4.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


but it's still not working :cry:
underminedsk
Developer
Developer
 
Posts: 26
Joined: Tue Mar 02, 2010 3:36 am

Re: Using BitmapFactory to decode the image byte buffer

Postby tanmay » Sun Jan 16, 2011 11:15 pm

I am having the same problem. Would appreciate some help. The decode method from BitmapFactory returns null when I try to decode the byte array.
tanmay
Freshman
Freshman
 
Posts: 3
Joined: Tue Jan 19, 2010 4:40 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests