SoFunction
Updated on 2025-03-11

Android implements silent photography function

This article shares the specific code for Android to implement silent photography function for your reference. The specific content is as follows

1. Application permission (change application after 6.0)

<uses-permission android:name="" />
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name=".READ_EXTERNAL_STORAGE"/>

2. Create the camera tool class CameraPreview:

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

 public CameraPreview(Context context, Camera camera) {
  super(context);
  //Initialize the Camera object  mCamera = camera;
  //Get the SurfaceHolder object  mHolder = getHolder();
  //Add a callback to get the three declaration cycle methods of Surface  (this);
  // deprecated setting, but required on Android versions prior to 3.0
  (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  try {
   //Set preview direction   (90);
   //Show this preview effect on SurfaceView   (holder);
   //Open the preview effect   ();
  } catch (IOException e) {
//   (TAG, "Error setting camera preview: " + ());
  }

 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  if (() == null) {
   return;
  }
  //Stop the preview effect  ();
  //Reset the preview effect  try {
   (mHolder);
  } catch (IOException e) {
   ();
  }
  ();
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {

 }
}

3. Create an xml file (If you want the user to take photos without any sense, you can set the view width and height to 0.1dp or hide it under another page):

<LinearLayout
 xmlns:andro
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <FrameLayout
  android:
  android:layout_width="match_parent"
  android:layout_height="500dp">
 </FrameLayout>

</LinearLayout>

4. The main class implements silent photography:

public class CameraActivity extends BaseActivity{

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


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

  int numberOfCameras = ();// Get the number of cameras  //Travel the camera information  for (int cameraId = 0; cameraId &lt; numberOfCameras; cameraId++) {
    cameraInfo = new ();
   (cameraId, cameraInfo);
   if ( == .CAMERA_FACING_FRONT) {//Front camera    mCamera = (cameraId);//Open the camera   }
  }

  CameraPreview mPreview = new CameraPreview(this, mCamera);
  (mPreview);
  new Thread(new Runnable() {
   @Override
   public void run() {
    try {
     (1000); // Set it to take photos automatically after 1 second, and can be adjusted     //Get the camera parameters      parameters = ();
     //Picture format     ();
     //What is the size of the preview     (800, 400);
     //Set the focus mode, autofocus     (.FOCUS_MODE_AUTO);
     //After the focus is successful, take photos automatically     (new () {
      @Override
      public void onAutoFocus(boolean success, Camera camera) {
       if (success) {
        //Get photos        (null, null, mPictureCallback);
       }
      }
     });
    } catch (InterruptedException e) {
     ();
    }
   }
  }).start();

  });
 }

 @Override
 protected void onStart() {
  ();

 }

 //Get the interface callback in the photo  mPictureCallback = new () {
  @Override
  public void onPictureTaken(byte[] data, Camera camera) {
   FileOutputStream fos = null;
   String mFilePath = ().getPath() +  + "";
   //document   File tempFile = new File(mFilePath);
   try {
    //
    fos = new FileOutputStream(tempFile);
    (data);

   } catch (FileNotFoundException e) {
    ();
   } catch (IOException e) {
    ();
   } finally {
    //Achieve the effect of shooting multiple shots in succession//    ();
//    if (fos != null) {
//     try {
//      ();
//     } catch (IOException e) {
//      ();
//     }
//    }
   }

  }
 };
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.