您好,登錄后才能下訂單哦!
硬件環境:小米4
Android版本:6.0
咱們先看效果圖:
我把代碼貼出來:
AndroidMainfest.xml文件(需要新增camera權限):
<uses-permission android:name="android.permission.CAMERA" />
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.bihu.qdemopush.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:orientation="horizontal"> <SurfaceView android:id="@+id/surfaceview1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent" /> <SurfaceView android:id="@+id/surfaceview2" android:layout_width="0dp" android:layout_weight="1" android:layout_marginLeft="5dp" android:layout_height="fill_parent" /> </LinearLayout> </LinearLayout>
MainActivity.java文件:主要是對2個surfaceView的顯示渲染
public class MainActivity extends AppCompatActivity { SurfaceView surfaceview1, surfaceview2; SurfaceHolder surfaceholder1, surfaceholder2; String TAG = "MainActivity"; private Camera camera1 = null, camera2; Camera.Parameters parameters; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); surfaceview1 = (SurfaceView) findViewById(R.id.surfaceview1); surfaceview2 = (SurfaceView) findViewById(R.id.surfaceview2); surfaceholder1 = surfaceview1.getHolder(); surfaceholder1.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceholder1.addCallback(new surfaceholderCallbackBack()); surfaceholder2 = surfaceview2.getHolder(); surfaceholder2.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceholder2.addCallback(new surfaceholderCallbackFont()); } @Override protected void onDestroy() { Log.e(TAG, "onDestroy"); super.onDestroy(); } /** * 后置攝像頭回調 */ class surfaceholderCallbackBack implements SurfaceHolder.Callback { @Override public void surfaceCreated(SurfaceHolder holder) { // 獲取camera對象 int cameraCount = Camera.getNumberOfCameras(); if (cameraCount > 0) { camera1 = Camera.open(0); try { // 設置預覽監聽 camera1.setPreviewDisplay(holder); Camera.Parameters parameters = camera1.getParameters(); if (MainActivity.this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { parameters.set("orientation", "portrait"); camera1.setDisplayOrientation(90); parameters.setRotation(90); } else { parameters.set("orientation", "landscape"); camera1.setDisplayOrientation(0); parameters.setRotation(0); } camera1.setParameters(parameters); // 啟動攝像頭預覽 camera1.startPreview(); System.out.println("camera.startpreview"); } catch (IOException e) { e.printStackTrace(); camera1.release(); System.out.println("camera.release"); } } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { camera1.autoFocus(new Camera.AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { initCamera();// 實現相機的參數初始化 camera.cancelAutoFocus();// 只有加上了這一句,才會自動對焦。 } } }); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } // 相機參數的初始化設置 private void initCamera() { parameters = camera1.getParameters(); parameters.setPictureFormat(PixelFormat.JPEG); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 1連續對焦 setDispaly(parameters, camera1); camera1.setParameters(parameters); camera1.startPreview(); camera1.cancelAutoFocus();// 2如果要實現連續的自動對焦,這一句必須加上 } // 控制圖像的正確顯示方向 private void setDispaly(Camera.Parameters parameters, Camera camera) { if (Integer.parseInt(Build.VERSION.SDK) >= 8) { setDisplayOrientation(camera, 90); } else { parameters.setRotation(90); } } // 實現的圖像的正確顯示 private void setDisplayOrientation(Camera camera, int i) { Method downPolymorphic; try { downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[]{int.class}); if (downPolymorphic != null) { downPolymorphic.invoke(camera, new Object[]{i}); } } catch (Exception e) { Log.e("Came_e", "圖像出錯"); } } } class surfaceholderCallbackFont implements SurfaceHolder.Callback { @Override public void surfaceCreated(SurfaceHolder holder) { // 獲取camera對象 int cameraCount = Camera.getNumberOfCameras(); if (cameraCount == 2) { camera2 = Camera.open(1); } try { // 設置預覽監聽 camera2.setPreviewDisplay(holder); Camera.Parameters parameters = camera2.getParameters(); if (MainActivity.this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { parameters.set("orientation", "portrait"); camera2.setDisplayOrientation(90); parameters.setRotation(90); } else { parameters.set("orientation", "landscape"); camera2.setDisplayOrientation(0); parameters.setRotation(0); } camera2.setParameters(parameters); // 啟動攝像頭預覽 camera2.startPreview(); System.out.println("camera.startpreview"); } catch (IOException e) { e.printStackTrace(); camera2.release(); System.out.println("camera.release"); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { camera2.autoFocus(new Camera.AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { parameters = camera2.getParameters(); parameters.setPictureFormat(PixelFormat.JPEG); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 1連續對焦 setDispaly(parameters, camera2); camera2.setParameters(parameters); camera2.startPreview(); camera2.cancelAutoFocus();// 2如果要實現連續的自動對焦,這一句必須加上 camera.cancelAutoFocus();// 只有加上了這一句,才會自動對焦。 } } }); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } // 控制圖像的正確顯示方向 private void setDispaly(Camera.Parameters parameters, Camera camera) { if (Integer.parseInt(Build.VERSION.SDK) >= 8) { setDisplayOrientation(camera, 90); } else { parameters.setRotation(90); } } // 實現的圖像的正確顯示 private void setDisplayOrientation(Camera camera, int i) { Method downPolymorphic; try { downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[]{int.class}); if (downPolymorphic != null) { downPolymorphic.invoke(camera, new Object[]{i}); } } catch (Exception e) { Log.e("Came_e", "圖像出錯"); } } } }
可能出現的問題:
1、沒有權限,需要手動打開,代碼里由于追求簡便,沒有主動申請,只在mainfest中注冊了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。