有意图地捕捉图像,无需再次单击相机按钮

发布于 2024-12-02 18:58:37 字数 215 浏览 3 评论 0原文

我想通过 Intent

IntentcameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)

捕获图像,但问题是使用此代码相机启动,用户必须单击相机按钮来捕获图像,但是什么我想要的是相机应该启动并拍照,而无需与用户进行任何进一步的交互

我想使用 INTENT 来做到这一点

I want to capture images through Intent

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)

but the problem is that with this code the camera is started and the user has to click on the camera button to capture the image , but what i want is that the camera sholud start and take picture without any furthur interaction with the user

I want to do this using INTENT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叫思念不要吵 2024-12-09 18:58:37

我就是这样做的:
声明 Camera 和 SurfaceHolder 的实例。

创建一个CallBackPicture对象,并实现PictureTaken上的方法(当你想拍照时启动的方法)

                 mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
                 Camera.PictureCallback mCall = new Camera.PictureCallback()  
                 {     
                     @Override  
                     public void onPictureTaken(byte[] data, Camera camera)  
                    {
                                             //DO YOUR STUFF
                    }
                };  

               // Open the instance of camera
               mCamera = Camera.open();
               try {
               // Call the preview (not sure if it is working without this call
               mCamera.setPreviewDisplay(mSurfaceHolder);
               mCamera.startPreview();
               } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
               }

               if(mCamera == null) Log.i(TAG, "mCamera is null");
               // Will call the onPictureTaken implemented above
               // Look at the documentation : public final void takePicture 

               mCamera.takePicture(null, null, mCall);  
               mCamera.stopPreview();
               mCamera.release();

通过修改这个,你应该能够做你想做的事情。
不要忘记也修改 Manifest.XML,但我认为您已经完成了!

编辑:有时 stoppreview() 和 release() 会出现一些问题。
所以,我所做的事情是:

if (mCamera != null) {
    mCamera.release();
    mCamera = null;
}
mCamera = Camera.open();
etc...

That is the way I did it :
Declare an instance of Camera, and SurfaceHolder.

Create an Object CallBackPicture, and implements the method on PictureTaken (method launched when you want to take a picture)

                 mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
                 Camera.PictureCallback mCall = new Camera.PictureCallback()  
                 {     
                     @Override  
                     public void onPictureTaken(byte[] data, Camera camera)  
                    {
                                             //DO YOUR STUFF
                    }
                };  

               // Open the instance of camera
               mCamera = Camera.open();
               try {
               // Call the preview (not sure if it is working without this call
               mCamera.setPreviewDisplay(mSurfaceHolder);
               mCamera.startPreview();
               } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
               }

               if(mCamera == null) Log.i(TAG, "mCamera is null");
               // Will call the onPictureTaken implemented above
               // Look at the documentation : public final void takePicture 

               mCamera.takePicture(null, null, mCall);  
               mCamera.stopPreview();
               mCamera.release();

By modifying this, you should be able to do what you want..
Don't forget to modify the Manifest.XML too, but I think you've already done it!

EDIT : sometimes there is some problem with stoppreview() and release()..
So, the thing i've done is :

if (mCamera != null) {
    mCamera.release();
    mCamera = null;
}
mCamera = Camera.open();
etc...
何必那么矫情 2024-12-09 18:58:37

这是不可能的。只有两个选项:

  1. 通过 Intent 调用相机应用程序。然后,用户以正常方式使用相机应用程序 - 即准备好后按下按钮。

  2. 使用 Camera 类 - 这个与通过 Intent 运行相机应用程序相比,工作量要大得多。但它给了你完全的控制权。

This can't be done. There are only two options:

  1. Invoking Camera app via Intent. The user then uses Camera app in normal way - i.e. presses the button when ready.

  2. Use Camera class - this is much more work compared to running Camera app via Intent. But it gives you full control.

吃颗糖壮壮胆 2024-12-09 18:58:37

当您调用相机意图时,您基本上“运行”相机应用程序(或在此意图上注册的其他应用程序),因此基本上,您无法控制它的工作方式。
您可以使用相机 API...请查看此处

When you call the camera intent you basically "run" the camera app (or other app that registered on this intent), so basically, you can't control of how it works.
You can use the Camera API...take a look here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文