SoFunction
Updated on 2025-03-01

Android custom cameras enable autofocus and manual focus

Android custom cameras implement autofocus and manual focus:

The system camera is not called because the interface presented by different machines that open the camera is not unified and cannot meet the needs.
Therefore, in order to make the program present a unified interface on different machines and be laid out according to needs, this demo was made.

The program implementation code is as follows:

import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;

@SuppressWarnings("deprecation")
public class PhotographActivity extends BaseActivity implements OnClickListener,  {
  private SurfaceView surfaceView;
  private Camera camera;
  private  parameters;
  private Button btn_goback, btn_takephoto;
  private SurfaceHolder surfaceHolder;
  
  @Override
  protected void onDestroy() {
    ();
    if(camera != null){
      ();
      ();
      camera = null;
    }
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.classes_activity_takephoto);
    initView();
  }

  @Override
  protected void onResume() {
    ();
    initCamera();
  }
  private void initView(){
    btn_goback = (Button) findViewById(.btn_goback);
    btn_goback.setOnClickListener(this);
    btn_takephoto = (Button) findViewById(.btn_takephoto);
    btn_takephoto.setOnClickListener(this);

    surfaceView = (SurfaceView) findViewById();
    (true);
    (this);
    (TRIM_MEMORY_BACKGROUND);
    surfaceHolder = ();
    (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    (true);
    (400, 300);
    (this);
  }
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    ();
    ();
    camera = null;

  }

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    try {
      (surfaceHolder);
    } catch (IOException e) {
      ();
    }
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // Implement autofocus    (new AutoFocusCallback() {
      @Override
      public void onAutoFocus(boolean success, Camera camera) {
        if (success) {
          ();// Only by adding this sentence will it be automatically focused          doAutoFocus();
        }
      }
    });
  }

  // Initialization settings of camera parameters  private void initCamera() {
    if (null == camera) {
      camera = ();
    }
    parameters = ();
    ();
    (Parameters.FLASH_MODE_TORCH);
    if (!("KORIDY H30")) {
      (.FOCUS_MODE_CONTINUOUS_PICTURE);// 1 Continuous focus    }else{
      (.FOCUS_MODE_AUTO);
    }
    (parameters);
    setDispaly(camera);
    ();
    ();// 2 If you want to achieve continuous autofocus, this sentence must be added  }

  // Control the correct display direction of the image  private void setDispaly(Camera camera) {
    if (() >= 8) {
      setDisplayOrientation(camera, -90);
    } else {
      (-90);
    }

  }

  // The correct display of the image implemented  private void setDisplayOrientation(Camera camera, int i) {
    Method downPolymorphic;
    try {
      downPolymorphic = ().getMethod("setDisplayOrientation", new Class[] {  });
      if (downPolymorphic != null) {
        (camera, new Object[] { i });
      }
    } catch (Exception e) {
      ("Came_e", "Image error");
    }
  }

  @Override
  public void onClick(View v) {
    switch (()) {
    case :
      doAutoFocus();
      break;
    case .btn_takephoto:
      takePicture();
      break;
    case .btn_goback:
      finish();
      break;
    default:
      break;
    }
  }

  // handle button auto focus
  private void doAutoFocus() {
    parameters = ();
    (.FOCUS_MODE_AUTO);
    (parameters);
    (new AutoFocusCallback() {
      @Override
      public void onAutoFocus(boolean success, Camera camera) {
        if (success) {
          ();// Only by adding this sentence will it be automatically focused.          if (!("KORIDY H30")) {
            parameters = ();
            (.FOCUS_MODE_CONTINUOUS_PICTURE);// 1 Continuous focus            (parameters);
          }else{
            parameters = ();
            (.FOCUS_MODE_AUTO);
            (parameters);
          }
        }
      }
    });
  }

  private void takePicture() {
    (shutterCallback, rawCallback, jpegCallback);
  }

  // define shutterCallback
  ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
      // TODO Do something when the shutter closes.
    }
  };

  PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
      // TODO Do something with the image RAW data.
    }
  };

  // stroe the picture in format jpeg
  PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
      // Save the image JPEG data to the SD card
      FileOutputStream outStream = null;
      try {
        //Modify the image path and name        String tempFilename = (()) + ".jpg";
        File folder = Constants.CACHE_FOLDER;
        if (!()) {
          ();
        }
        String path = Constants.CACHE_FOLDER +  + tempFilename;
        outStream = new FileOutputStream(path);
        (data);
        ();
        ();
        (new BitmapDrawable((data, 0, )));
      } catch (FileNotFoundException e) {
        ("TAG", "File Note Found", e);
      } catch (IOException e) {
        ("TAG", "IO Exception", e);
      }
    }
  };
}

The code for classes_activity_takephoto.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <SurfaceView
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/rlright" />

  <LinearLayout
    android:
    android:layout_width="40dp"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:background="#2b2b2b"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="0dip"
      android:layout_weight="1"
      android:gravity="center" >

      <Button
        android:
        style="@style/PETextButton"
        android:text="return"/>
    </LinearLayout>

    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="0dip"
      android:layout_weight="1"
      android:gravity="center" >

      <Button
        android:
        style="@style/PETextButton"
        android:text="Photograph" />
    </LinearLayout>
  </LinearLayout>

</RelativeLayout>

The corresponding permissions need to be added in the manifest file:

  <uses-permission android:name=""/>
  <uses-feature android:name=""/>
  <uses-feature android:name=""/>

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.