简体   繁体   English

Android:从我的布局中拍摄照片而无需启动相机意图

[英]Android : Take a photo from with in my layout without starting a camera intent

I want to take a photo from my camera, however I don't want to start a new intent to capture a photo but I want the camera to open inside my layout and then click a button to take the photo, like in the bellow picture. 我想用相机拍摄照片,但是我不想开始捕捉照片的新意图,但是我希望相机在版面中打开,然后单击按钮以拍摄照片,就像下面的照片一样。

在此处输入图片说明

So is it possible to do it because i didn't find any ressources about this, also I prefer that the token photo to be cropped? 因此有可能这样做是因为我没有找到任何资源,我也希望裁剪令牌照片?

Google demo of Camera2 Api. Google的Camera2 Api演示。 It will help you. 它会帮助你。 Link : https://github.com/googlesamples/android-Camera2Basic 链接: https : //github.com/googlesamples/android-Camera2Basic

You need to get the camera runtime permission. 您需要获得相机运行时权限。 You can then access the camera API directly. 然后,您可以直接访问相机API。

This SurfaceView for example will show the preview image in your layout structure (from the official documentation ): 例如,此SurfaceView将在布局结构中显示预览图像(来自官方文档 ):

/** A basic Camera preview class */
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) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    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){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

You get the Camera instance with this code: 您使用以下代码获取Camera实例:

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

And you display it in your Activity like this: 然后将其显示在Activity如下所示:

public class CameraActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }
}

See the official documentation . 请参阅官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM