Android(相机)- 如何将 stopPreview() 与 onPictureTaken() 同步? [英] Android (Camera) - How to synchronize stopPreview() with onPictureTaken()?

查看:12
本文介绍了Android(相机)- 如何将 stopPreview() 与 onPictureTaken() 同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,客户端使用相机拍照.在人们点击我的点击"按钮之前,图像的预览正在使用 SurfaceView 显示在平板电脑中.当用户点击点击按钮时,会调用 onPictureTaken 方法,在该方法中,我会保存图像并调用 camera.stopPreview() 方法(以便用户可以看到拍摄的照片).

I have an app in which the client uses the camera to take a picture. The preview of the image is being shown in the tablet using a SurfaceView, before the person hits my "click" button. When the person hits the click button, the method onPictureTaken is called, and, in that method, I save the image and also call the camera.stopPreview() method (so the user can see the picture that was taken).

但是有一个问题...如果用户在拍摄图片的那一刻在平板电脑上移动,在调用 stopPreview 方法后实际显示的静止图片不对应于我进入了 onPictureTaken 方法的字节数组.那里有几毫秒的延迟,当用户在拍摄照片之前在平板电脑周围移动时,差异会变得突出(我知道 99% 的人在拍照时不会移动平板电脑,但我的客户实际上注意到了这个问题并希望修复它......).我已经尝试将保存操作移动到一个单独的线程,如下所示,这样 onPictureTaken 方法可以尽可能快地执行.然而,它根本没有任何效果......

There is an issue, however... If the user is moving around the tablet at the moment that the picture is taken, the still picture actually shown after the stopPreview method is called DOES NOT correspond to the one that I get in the byte array of the onPictureTaken method. There is a delay of some miliseconds there in which make that difference to stand out when the user is moving around the tablet just before the picture is taken (I know that 99% of the people will not move the tablet around while taken the picture, but my client actually noticed this issue and want it fixed...). I have tried to move the save operation to a separete thread, as shown below, so the onPictureTaken method can execute as fast as possible. Still, it had no effect at all...

private PictureCallback pictureCallback = new PictureCallback() {

    public void onPictureTaken(byte[] data, Camera camera) {

        camera.stopPreview();
        reference = data;

        new PictureCallbackHeavy().execute();
    }
};

我也尝试在调用 takePicture 方法之前调用 camera.stopPreview()(而不是在 onPictureTaken() 方法内).但结果是一样的.

I have also trield to call camera.stopPreview() just BEFORE I call the takePicture method (and not inside the onPictureTaken() method). But the result is the same.

我可以做些什么来同步 stopPreview 方法,这样我就可以准确地显示拍摄的图像并且在 onPictureTaken() 回调的字节数组中?

What can I do to sync the stopPreview method so I can show EXACTLY the image that was taken and that is in the byte array of the onPictureTaken() callback?

提前谢谢你!!=)

推荐答案

不幸的是,你不能仅仅通过调用 stopPreview() 来获得合理的良好预览图像,因为在拍摄图片的那一刻和调用 onPictureTaken() 的那一刻之间可能会经过相当长的一段时间,因为它是这样工作的:

Unfortunately you can't acquire a reasonable good preview image just by calling stopPreview() because between the moment the picture is taken and the moment onPictureTaken() is called there can pass quite some time because it works like this:

  • 相机实际拍摄照片(这就是您要预览的内容)
  • onShutter() 被调用
  • 调用原始图像数据的 onPictureTaken()(在某些设备上)
  • 调用缩放预览图像的onPictureTaken()(在某些设备上)
  • onPictureTaken() 用于最终压缩的图像数据被调用(我们这里说的那个)

因此,您必须将 onPictureTaken() 回调中的 byte[] 数据 转换为位图,并将该位图映射到您应该放置在 SurfaceView 上方以显示静止预览图像的 ImageView.代码可能看起来像这样:

So you have to convert the byte[] data in your onPictureTaken() callback into a Bitmap and map that Bitmap onto an ImageView that you should position above your SurfaceView to show the still preview image. The code will probably look something like this:

public void onPictureTaken(byte[] data, Camera camera) {
    camera.stopPreview();
    final Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length);
    surfaceView.setVisibility(SurfaceView.GONE);
    imageView.setVisibility(ImageView.VISIBLE);
    imageView.setImageBitmap(image);
    reference = data;
    new PictureCallbackHeavy().execute();
}

这篇关于Android(相机)- 如何将 stopPreview() 与 onPictureTaken() 同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆