从Android相机捕获的照片是完全黑色的 [英] Photos captured from Android camera are completely black
问题描述
我制作相机并尝试拍摄照片。由于原始数据是YUV,我使用函数将其转换为RGB:
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.
我也找到了以下方式:
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
但项目已关闭。
有没有其他有效的方法来保存照片?谢谢!
Are there any other effective ways to save a photo? Thank you!
推荐答案
一个旧帖子,但它说的是一个类似的问题,我知道:)
An old post, but it speaks of a similar problem that I have so I might as well answer the part I know :)
你可能做错了。我建议您使用JPEG回调来存储图像:
You're probably doing it wrong. I suggest you use the JPEG callback to store the image:
mCamera.takePicture(null, null, callbackJPEG);
这样,您就可以将JPEG数据导入到可以存储到文件中的例程中:
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();
}
}
至于黑色图片,在 camera.startPreview()
和相机之间放置一个简单的
负责处理Galaxy Nexus上的特定问题。 Thread.sleep(250)
.takePicture()
我不知道为什么这个延迟是必要的。即使我添加 camera.setOneShotPreviewCallback()
并调用 camera.takePicture()
从回调,图像出来黑色如果我不第一次延迟...
哦,和延迟不只是一些延迟。它必须是一些很长的价值。例如, 250ms
有时会工作,有时不会在我的手机上。
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.
这篇关于从Android相机捕获的照片是完全黑色的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!