Android 拍摄照片并将其存储为字节数组
我这里有一段简单的代码,但由于某种原因它没有存储数据。我确信这是愚蠢的事情,但我似乎无法弄清楚。任何帮助将不胜感激。
private byte[] picture;
public void takePicture(){
Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.set("camera-id",2);
camera.setParameters(parameters);
parameters.set("gps-timestamp", "1233744883");
camera.setParameters(parameters);
Log.i("method", "in takePicture()");
camera.takePicture(null, rawCallback, null);
camera.release();
}
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Do something with the image RAW data.
Log.i("rawcallback", "in rawcallback");
picture = data;
}
};
当我运行代码时,回调中的日志没有被调用,“图片”也没有保存“数据”。有什么想法吗?我正在尝试使用前置摄像头拍照,我是否需要为此做一些特别的事情,但我没有这样做?
我也有这些权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
I have a simple piece of code here, but for some reason it is not storing the data. I'm sure it's something stupid, but I can't seem to figure it out. Any help would be greatly appreciated.
private byte[] picture;
public void takePicture(){
Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.set("camera-id",2);
camera.setParameters(parameters);
parameters.set("gps-timestamp", "1233744883");
camera.setParameters(parameters);
Log.i("method", "in takePicture()");
camera.takePicture(null, rawCallback, null);
camera.release();
}
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Do something with the image RAW data.
Log.i("rawcallback", "in rawcallback");
picture = data;
}
};
When I run the code the Log in the call back is not being called, nor is "picture" saving "data". Any thoughts? I'm trying to take a picture with the front facing camera, could I need something special for that, which I'm not doing?
I also have these permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Camera.takePicture()
中的最后一个参数对应于 JPEG 回调。因此,您应该在camera.setParameters()调用之前添加parameters.setPictureFormat(PixelFormat.JPEG);
行。或者使用camera.takePicture(null, rawCallback, null); 来代替。您还应该确保 AndroidManifest.xml 中有以下几行:
来自我的评论:
用于访问字体相机的 API 仅出现在 Android 2.3(API 版本 9)中。在早期版本中,您应该使用供应商特定的技术。看看这个工作解决方案< /a> 适用于 Galaxy S:
希望对您有所帮助。如果没有,请尝试使用上面的
setPreviewSize
参数。The last parameter in
Camera.takePicture()
corresponds to JPEG callback. So you should add theparameters.setPictureFormat(PixelFormat.JPEG);
line before camera.setParameters() call. Or usecamera.takePicture(null, rawCallback, null);
instead.You should also ensure you have the following lines in AndroidManifest.xml:
From my comment:
The API for accessing font facing cameras only appeared in Android 2.3 (API version 9). In the earlier version you should use vendor specific techniques. Take a look at this working solution for Galaxy S:
I hope it will help you. If not, try to play with
setPreviewSize
parameters above.我在 JPEG 回调中遇到了类似的问题。拍照后并不总是被调用。
这可能是因为您过早释放相机。
在释放相机或退出应用程序之前,您应该等待所有回调都返回。
Camera 类不是线程安全的。回调将在调用
open(int)
的事件线程上调用。 (请参阅 doc)我设法通过使用
setJpegQuality()
函数降低 JPEG 质量来更频繁地调用它,以减少需要计算的数据。但更好的方法应该是等待回调返回。根据 Android代码文档,还需要向Camera传递一个SurfaceHolder,并在拍照前启动预览。
出于我的目的,我想在用户屏幕上不进行任何预览的情况下拍摄一张照片。
我使用了本地 SurfaceView,但您可以使用另一个。
我建议类似:
I got a similar problem with the JPEG callback. It was not always called after taking a picture.
This might be because you release the Camera too soon.
You should wait that all your callbacks are returned before releasing your Camera or exiting your app.
The Camera class is not thread-safe. Callbacks will be invoked on the event thread
open(int)
was called from. (see the doc)I managed to get it called more often by reducing the JPEG quality with the
setJpegQuality()
function to get less data to compute. But a nicer way should be to wait until the callbacks are returned.According to Android code documentation, you also need to pass a SurfaceHolder to the Camera and start a preview before taking a picture.
For my purpose, I wanted to take a picture without any preview on the user screen.
I used a local SurfaceView, but you can use another one.
I suggest something like :