Java 显示带变焦的前后摄像头

Java 显示带变焦的前后摄像头,java,android,camera,Java,Android,Camera,我希望在自己的活动中显示前后摄像头输入。另外,我希望用户控制两个摄像头上的缩放 我应该如何使用摄影机对象调用,不是不推荐吗?另外,如何让我的视图显示来自摄像机的实时数据 首先,您需要检查设备是否支持使用多摄像头 然后您可以使用下面的代码 这是主活动类 import android.content.Context; import android.hardware.Camera; import android.os.Bundle; import android.support.v7.app.AppC

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


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

首先,您需要检查设备是否支持使用多摄像头 然后您可以使用下面的代码 这是主活动类

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>

首先,您需要检查设备是否支持使用多摄像头 然后您可以使用下面的代码 这是主活动类

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>

您应该试试。 它易于使用,并具有许多内置功能,如:

  • 图像和视频捕获
  • 自动系统权限处理
  • 自动预览缩放和更多功能
    • 你应该试试。 它易于使用,并具有许多内置功能,如:

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

      您的代码不起作用。使用您的代码,我无法从我的后摄像头或前摄像头中看到任何内容。您的代码无效。使用您的代码,我无法从我的后摄像头或前摄像头中看到任何内容。您希望本机摄像头还是任何摄像头都能为您工作?我只希望我的应用程序能够工作。我真的不在乎我是否必须使用本机或任何摄像头类。android.hardware.camera2是新的一个,但请查看此库-你想要本机摄像头还是任何摄像头都可以为你工作?我只想让我的应用程序工作。我真的不在乎我是否必须使用本机或任何camera类。android.hardware.camera2是新的,不过请查看此库-