Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Photos captured from Android camera are completely black

I make a camera and try to capture a picture. Since the original data is YUV, I turn it into RGB using function:

static public void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp,int width, int height)

However, the photo saved is completely black, there is no content in it.

I also found the following way:

mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

but the project was shut down.

Are there any other effective ways to save a photo? Thank you!

like image 636
user1132443 Avatar asked Dec 27 '22 05:12

user1132443


1 Answers

An old post, but it speaks of a similar problem that I have so I might as well answer the part I know :)

You're probably doing it wrong. I suggest you use the JPEG callback to store the image:

mCamera.takePicture(null, null, callbackJPEG);

This way you will get JPEG data into the routine which you can store into a file unmodified:

final Camera.PictureCallback mCall = new Camera.PictureCallback()
{
  @Override
  public void onPictureTaken(byte[] data, Camera camera)
  {
    //Needs <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    File sdCard = Environment.getExternalStorageDirectory();
    File file = new File(sdCard, "pic.jpg");
    fil = new FileOutputStream(file);
    fil.write(data);
    fil.close();          
  }
}

As far as the black picture goes, I have found that placing a simple Thread.sleep(250) between camera.startPreview() and camera.takePicture() takes care of that particular problem on my Galaxy Nexus. I have no idea why this delay is necessary. Even if I add camera.setOneShotPreviewCallback() and call camera.takePicture() from the callback, the image comes out black if I don't first delay... Oh, and the delay is not just "some" delay. It has to be some pretty long value. For example, 250ms sometimes works, sometimes not on my phone.

like image 79
velis Avatar answered Jan 15 '23 17:01

velis