<?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>
緊接著在對應的類中實現(xiàn)SurfaceHolder.Callback接口,并重寫surfaceCreated、surfaceChanged、surfaceDestroyed方法。
創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站建設、網(wǎng)站重做改版、雙灤網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5建站、購物商城網(wǎng)站建設、集團公司官網(wǎng)建設、外貿(mào)網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為雙灤等各大城市提供網(wǎng)站開發(fā)制作服務。
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的回調(diào) } //視圖初始化(攝像頭2) private void initView2(){ surfaceView2 = findViewById(R.id.surfaceView2);//獲得SurfaceView的實例 mSurfaceHolder2 = surfaceView2.getHolder();//獲得SurfaceView的Holder mSurfaceHolder2.addCallback(this);//設置Holder的回調(diào) } //打開照相機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(); } } //回調(diào)初始化 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" />
當前名稱:仿抖音短視頻源碼開啟雙目攝像頭實時預覽
網(wǎng)站URL:http://www.2m8n56k.cn/article14/jcgsde.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站導航、外貿(mào)建站、建站公司、營銷型網(wǎng)站建設、外貿(mào)網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)