Android Realizes Silent Photographing Function

  • 2021-12-04 19:44:39
  • OfStack

In this paper, we share the specific code of Android to realize silent photo function for your reference. The specific contents are as follows

1. Apply for permission (apply dynamically after 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. Create the camera tool class CameraPreview:


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

 public CameraPreview(Context context, Camera camera) {
  super(context);
  // Initialization Camera Object 
  mCamera = camera;
  // Get SurfaceHolder Object 
  mHolder = getHolder();
  // Add a callback to get Surface Adj. 3 Declare cycle method 
  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 {
   // Set the preview direction 
   mCamera.setDisplayOrientation(90);
   // Show this preview effect in the SurfaceView Above 
   mCamera.setPreviewDisplay(holder);
   // Turn on preview effect 
   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;
  }
  // Stop preview effects 
  mCamera.stopPreview();
  // Reset the preview effect 
  try {
   mCamera.setPreviewDisplay(mHolder);
  } catch (IOException e) {
   e.printStackTrace();
  }
  mCamera.startPreview();
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {

 }
}

3. Create an xml file (for users to take photos without feeling it, you can change the view width to 0.1 dp or hide it under another page):


<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. The main class realizes silent photo taking:


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();//  Get the number of cameras 
  // Traverse the camera information 
  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) {// Front camera 
    mCamera = Camera.open(cameraId);// Turn on the camera 
   }
  }

  CameraPreview mPreview = new CameraPreview(this, mCamera);
  cameraFrame.addView(mPreview);
  new Thread(new Runnable() {
   @Override
   public void run() {
    try {
     Thread.sleep(1000); //  Settings 1 Automatic photo after seconds, adjustable 
     // Get the parameters of the camera 
     Camera.Parameters parameters = mCamera.getParameters();
     // Format of pictures 
     parameters.setPictureFormat(ImageFormat.JPEG);
     // What is the size of the preview 
     parameters.setPreviewSize(800, 400);
     // Set the focus mode and auto-focus 
     parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
     // After focusing successfully, take pictures automatically 
     mCamera.autoFocus(new Camera.AutoFocusCallback() {
      @Override
      public void onAutoFocus(boolean success, Camera camera) {
       if (success) {
        // Get photos 
        mCamera.takePicture(null, null, mPictureCallback);
       }
      }
     });
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }).start();

  });
 }

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

 }

 // Get interface callbacks in photos 
 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";
   // Documents 
   File tempFile = new File(mFilePath);
   try {
    //
    fos = new FileOutputStream(tempFile);
    fos.write(data);

   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   } finally {
    // Achieve the effect of taking multiple pictures continuously 
//    mCamera.startPreview();
//    if (fos != null) {
//     try {
//      fos.close();
//     } catch (IOException e) {
//      e.printStackTrace();
//     }
//    }
   }

  }
 };
}

Related articles: