【问题标题】:Showing front and rear camera with zoom显示带变焦的前后摄像头
【发布时间】:2019-01-31 01:06:46
【问题描述】:

我希望在我自己的活动中显示前置或后置摄像头输入。另外,我希望用户控制两个摄像头的变焦。

我应该如何使用 Camera 对象调用,它不是已弃用吗?另外,如何让我的视图显示来自摄像机的实时数据?

【问题讨论】:

  • 你想要原生相机还是任何相机都适合你?
  • 我只想让我的应用程序正常工作。真的不在乎我是否必须使用本机或任何相机类。
  • android.hardware.camera2 是新的,尽管看看这个库-github.com/natario1/CameraView

标签: java android camera


【解决方案1】:

你应该试试the CameraKit library。 它易于使用并具有许多内置功能,例如:

  • 图像和视频捕获。
  • 自动系统权限处理。
  • 自动预览缩放和更多

【讨论】:

    【解决方案2】:

    首先你需要检查设备是否支持使用多摄像头 那么你可以使用下面的代码 这是 MainActivity.class

    import android.content.Context;
    import android.hardware.Camera;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.widget.FrameLayout;
    
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Create an instance of Camera
        Camera mBackCamera = getCameraInstance(0);
        // Create back camera Preview view and set it as the content of our activity.
        BackCamera mBackCam = new BackCamera(this, mBackCamera);
        FrameLayout backPreview = (FrameLayout) findViewById(R.id.back_camera_preview);
        backPreview.addView(mBackCam);
    
        Camera mFrontCamera = getCameraInstance(1);
        FrontCamera mFrontCam = new FrontCamera(this, mFrontCamera);
        FrameLayout frontPreview = (FrameLayout) findViewById(R.id.front_camera_preview);
        frontPreview.addView(mFrontCam);
    }
    public static Camera getCameraInstance(int cameraId){
        Camera c = null;
        try {
            c = Camera.open(cameraId); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
            Log.e("TAG","Camera " + cameraId + " not available! " + e.toString() );
        }
        return c; // returns null if camera is unavailable
    }
    }
    
    class FrontCamera extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    
    public static String TAG = "FrontCamera";
    
    public FrontCamera(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 : " + e.getMessage());
        }
    }
    }
    class BackCamera extends SurfaceView implements SurfaceHolder.Callback {
        private SurfaceHolder mHolder;
        private Camera mCamera;
    
        public static String TAG = "BackCamera";
    
        public BackCamera(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 : " + 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 : " + e.getMessage());
            }
        }
    }
    

    这里是xml

     <android.support.constraint.ConstraintLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="com.mooi.myapplication.MainActivity">
    <FrameLayout
        android:id="@+id/back_camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        />
    
    <FrameLayout
        android:id="@+id/front_camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 您的代码不起作用。使用您的代码,我从后置或前置摄像头看不到任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多