【问题标题】:Android take a picture without preview and without interactionAndroid拍照无预览无交互
【发布时间】:2019-06-28 16:16:32
【问题描述】:

我是安卓新手。

我发现了以前的问题,但是很老,实际上我使用的是 API 23 或更高版本。

我感兴趣的是一种从相机获取图片的方法,无需显示预览,也无需用户进行任何触摸或交互。

我使用 Intent 访问相机应用,但不让我以我需要的方式自动拍照。 这只能让我使用相机应用程序。

       Intent intentTakePic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if(intentTakePic.resolveActivity(getPackageManager()) != null){
                startActivityForResult(intentTakePic, GET_THE_PICTURE);
            }

将来我可能还需要以相同的方式录制音频(无需交互)。

有人对我有建议吗?

【问题讨论】:

  • 这是大学项目,我想用它来拍闪电什么的。
  • 我不知道有什么方法可以在不调用相机应用程序的情况下使用相机,但即使有办法,从 API 24 及更高版本开始,应用程序必须向用户请求访问权限相机和/或照片库。因此,至少在某个时候(可能是应用程序第一次运行时),您需要获得用户的许可。
  • 谢谢你的回答,我知道app需要用户的权限才能访问相机,我想自动化拍摄过程,因为男人的反应时间太长了。

标签: java android android-camera android-camera-intent


【解决方案1】:
【解决方案2】:

您需要使用 CameraAPI 来拍照,而无需打开其他相机应用。 https://developer.android.com/guide/topics/media/camera

你基本上会制作一个相机应用程序。

// in the activity onCreate, but doesn't have to be there

        // needs explicit permission
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[] {Manifest.permission.CAMERA}, 1);
            }
        }

        final Camera camera = Camera.open();
        CameraPreview cameraPreview = new CameraPreview(this, camera);

        // preview is required. But you can just cover it up in the layout.
        FrameLayout previewFL = findViewById(R.id.preview_layout);
        previewFL.addView(cameraPreview);
        camera.startPreview();

        // take picture button
        findViewById(R.id.take_picture_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                camera.takePicture(null, null, new Camera.PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {
                        // path of where you want to save it
                        File pictureFile = new File(getFilesDir() + "/images/pic0");

                        try {
                            FileOutputStream fos = new FileOutputStream(pictureFile);
                            fos.write(data);
                            fos.close();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

CameraPreview 类

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

【讨论】:

  • 好的,谢谢以色列。你用过这个 API 吗?您知道开始使用的教程或示例吗?
  • 我已经添加了创建您自己的相机应用程序的最低限度的方法。请注意,它需要预览才能工作。但是你可以用另一个View 来掩盖它。
  • 是否可以从广播接收器调用?
猜你喜欢
  • 2014-11-03
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多