Androidは静かに写真を撮る機能を実現します。


本論文の例では、Androidが静粛撮影機能を実現するための具体的なコードを共有しています。
1.申請権限(6.0以降は動的に申請する)

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2.カメラツール類Cameraa Previewを作成する:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
 private SurfaceHolder mHolder;
 private Camera mCamera;

 public CameraPreview(Context context, Camera camera) {
  super(context);
  //   Camera  
  mCamera = camera;
  //  SurfaceHolder  
  mHolder = getHolder();
  //    ,  Surface         
  mHolder.addCallback(this);
  // deprecated setting, but required on Android versions prior to 3.0
  mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  try {
   //      
   mCamera.setDisplayOrientation(90);
   //          SurfaceView  
   mCamera.setPreviewDisplay(holder);
   //      
   mCamera.startPreview();
  } catch (IOException e) {
//   Log.d(TAG, "Error setting camera preview: " + e.getMessage());
  }

 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  if (holder.getSurface() == null) {
   return;
  }
  //      
  mCamera.stopPreview();
  //        
  try {
   mCamera.setPreviewDisplay(mHolder);
  } catch (IOException e) {
   e.printStackTrace();
  }
  mCamera.startPreview();
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {

 }
}
3.xmlファイルを作成します。

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <FrameLayout
  android:id="@+id/camera_frame"
  android:layout_width="match_parent"
  android:layout_height="500dp">
 </FrameLayout>

</LinearLayout>
4.メインクラスは静かに写真を撮ることを実現する:

public class CameraActivity extends BaseActivity{

 private FrameLayout cameraFrame;
 private Camera mCamera;
 private TextView cameraTv;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_camera);


  cameraFrame = (FrameLayout) findViewById(R.id.camera_frame);
  cameraTv = (TextView) findViewById(R.id.camera_tv);

  int numberOfCameras = Camera.getNumberOfCameras();//        
  //       
  for (int cameraId = 0; cameraId < numberOfCameras; cameraId++) {
   Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
   Camera.getCameraInfo(cameraId, cameraInfo);
   if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {//     
    mCamera = Camera.open(cameraId);//     
   }
  }

  CameraPreview mPreview = new CameraPreview(this, mCamera);
  cameraFrame.addView(mPreview);
  new Thread(new Runnable() {
   @Override
   public void run() {
    try {
     Thread.sleep(1000); //   1      ,   
     //        
     Camera.Parameters parameters = mCamera.getParameters();
     //     
     parameters.setPictureFormat(ImageFormat.JPEG);
     //        
     parameters.setPreviewSize(800, 400);
     //      ,    
     parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
     //     ,    
     mCamera.autoFocus(new Camera.AutoFocusCallback() {
      @Override
      public void onAutoFocus(boolean success, Camera camera) {
       if (success) {
        //    
        mCamera.takePicture(null, null, mPictureCallback);
       }
      }
     });
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }).start();

  });
 }

 @Override
 protected void onStart() {
  super.onStart();

 }

 //          
 Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
  @Override
  public void onPictureTaken(byte[] data, Camera camera) {
   FileOutputStream fos = null;
   String mFilePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "tt005.png";
   //  
   File tempFile = new File(mFilePath);
   try {
    //
    fos = new FileOutputStream(tempFile);
    fos.write(data);

   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   } finally {
    //          
//    mCamera.startPreview();
//    if (fos != null) {
//     try {
//      fos.close();
//     } catch (IOException e) {
//      e.printStackTrace();
//     }
//    }
   }

  }
 };
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。