Photos taken from an Android camera are completely black

I am making a camera and trying to capture an image. Since the source data is YUV, I convert it to RGB using the function:

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

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

I also found the following method:

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

but the project was disabled.

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

+4
source share
3 answers

Old post, but it talks about a similar problem that I have, so I could also answer the part that I know :)

You are probably mistaken. I suggest you use a JPEG callback to save the image:

 mCamera.takePicture(null, null, callbackJPEG); 

Thus, you will receive JPEG data in a program that you can save to a file without changes:

 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 for the black image, I found that placing a simple Thread.sleep(250) between camera.startPreview() and camera.takePicture() will take care of this particular problem on my Galaxy Nexus. I don’t know why this delay is needed. Even if I add camera.setOneShotPreviewCallback() and call camera.takePicture() from the callback, the image goes black unless I delay the first delay ... Oh, and the delay is not just a “some” delay. This should be a fairly long meaning. For example, 250ms sometimes works, sometimes not on my phone.

+6
source

A full black photograph is the result of an immediate call to mCamera.takePicture() after calling mCamera.startPreview() . Android must get the appropriate time to process its autofocus activity before accepting the actual image. Blackness is the result of intermittent exposure caused by interruption when autofocus occurs.

I recommend calling mCamera.autoFocus() immediately after mCamera.startPreview() .

In the autofocus function callback, call mCamera.takePicture() .

This stream ensures that the image is captured after autofocus is completed and removes blackness or exposure problems from the captured image.

The delay mentioned in Velis answer works for some devices because these devices shut down autofocus. Providing a proper callback flow eliminates this arbitrary delay and will work on every device.

+2
source

I solved this problem using the following argument:

 final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 

When I used TEMPLATE_STILL_CAPTURE instead of TEMPLATE_PREVIEW, which fixed my image as a full black image. This thing works in my case.

+1
source

Source: https://habr.com/ru/post/1398081/


All Articles