温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

仿抖音短视频源码开启双目摄像头实时预览

发布时间:2020-08-11 16:11:55 来源:ITPUB博客 阅读:170 作者:zhibo系统开发 栏目:编程语言
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="返回" />
    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_weight="5" />
    <SurfaceView
        android:id="@+id/surfaceView2"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_weight="5" />
</LinearLayout>

紧接着在对应的类中实现SurfaceHolder.Callback接口,并重写surfaceCreated、surfaceChanged、surfaceDestroyed方法。

public class CameraTestActivity extends Activity implements  SurfaceHolder.Callback{
    public Button mButton;
    private Camera mCamera1,mCamera2;
    private SurfaceView surfaceView1,surfaceView2;
    private SurfaceHolder mSurfaceHolder,mSurfaceHolder2;
    private int cameraId = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_camera);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        initView();
        initView2();
        initButton();
    }
    //视图初始化(摄像头1)
    private void initView() {
        surfaceView1 = findViewById(R.id.surfaceView1);//获得SurfaceView的实例
        mSurfaceHolder = surfaceView1.getHolder();//获得SurfaceView的Holder
        mSurfaceHolder.addCallback(this);//设置Holder的回调
    }
    //视图初始化(摄像头2)
    private void initView2(){
        surfaceView2 = findViewById(R.id.surfaceView2);//获得SurfaceView的实例
        mSurfaceHolder2 = surfaceView2.getHolder();//获得SurfaceView的Holder
        mSurfaceHolder2.addCallback(this);//设置Holder的回调
    }
    //打开照相机1
    public void CameraOpen() {
        try
        {
            //打开摄像机1
            mCamera1 = Camera.open(cameraId);
            mCamera1.setDisplayOrientation(270);
            //绑定Surface并开启预览
            mCamera1.setPreviewDisplay(mSurfaceHolder);
            mCamera1.startPreview();
        } catch (IOException e) {
            mCamera1.release();
            mCamera1 = null;
            Toast.makeText(CameraTestActivity.this, "surface created failed", Toast.LENGTH_SHORT).show();
        }
    }
    //打开照相机2
    public void CameraOpen2() {
        try
        {
            //打开摄像机2
            mCamera2 = Camera.open(cameraId - 1);
            mCamera2.setDisplayOrientation(270);
            //绑定Surface并开启预览
            mCamera2.setPreviewDisplay(mSurfaceHolder2);
            mCamera2.startPreview();
        } catch (IOException e) {
            mCamera2.release();
            mCamera2 = null;
            Toast.makeText(CameraTestActivity.this, "surface created failed", Toast.LENGTH_SHORT).show();
        }
    }
    //回调初始化
    private void initButton() {
        //返回上个界面的按钮
        mButton = findViewById(R.id.button2);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Intent intent = new Intent(CameraTestActivity.this,MainActivity.class);
               startActivity(intent);
            }
        });
    }
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        //检查权限
        if (ContextCompat.checkSelfPermission(CameraTestActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        } else {
            CameraOpen();
            CameraOpen2();
        }
    }
    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        Camera.Parameters parameters = mCamera1.getParameters();
        mCamera1.setParameters(parameters);
        mCamera1.startPreview();
        Camera.Parameters parameters2 = mCamera2.getParameters();
        mCamera2.setParameters(parameters2);
        mCamera2.startPreview();
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        mCamera1.stopPreview();
        mCamera1.release();
        mCamera1 = null;
        mCamera2.stopPreview();
        mCamera2.release();
        mCamera2 = null;
    }
}

记得别忘了在AndroidMainfest.xml中开启权限了

<uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI