SoFunction
Updated on 2025-04-09

Android quickly realizes the function of taking photos without preview

This example shares the specific code for Android to implement the preview and photography function for your reference. The specific content is as follows

Implementation ideas:

Set the width and height of the preview SurfaceView to a value that is invisible to the naked eye, such as 0.1dp, and the others are the standard steps for customizing the camera!

Of course, there are so many articles on custom cameras on the Internet, so it is impossible for me to do the old things. Here I recommend a very useful third-party library. Maybe many people have already learned about it and used it. Students who have not used it can try it and test it out for yourself.

Without further ado, go firstSource code address 

Here is a brief introduction to the usage:

1. Add library dependencies in gradle:compile ':camerakit:0.13.1'

2. Layout file code:

<LinearLayout
   android:
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">
 
   <Button
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="test"
    android:layout_gravity="center_horizontal"/>
 
   <
    android:
    android:layout_width="0.1dp"
    android:layout_height="0.1dp"
    android:adjustViewBounds="true"
    camerakit:ckFacing="front" />
 
</LinearLayout>

camerakit:ckFacing="front" means to use the front camera. Please refer to the official documentation for other attributes.

Note: The width and height cannot be set to 0, otherwise you cannot take pictures.

3. Java code

public class MainActivity extends BaseActivity {
 @BindView(.btn_test)
 Button btnTest;
 @BindView()
 CameraView cameraView;
 @BindView(.ll_content)
 LinearLayout llContent;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_main);
  (this);
  initView();
 } 
 
 @Override
 protected void onResume() {
  ();
  ();
 }
 
 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  ();
  ();
 }
 
 private void initView() {
  //Set callback  (new CameraKitEventListener() {
   @Override
   public void onEvent(CameraKitEvent cameraKitEvent) {
 
   }
 
   @Override
   public void onError(CameraKitError cameraKitError) {
 
   }
 
   @Override
   public void onImage(CameraKitImage cameraKitImage) {
    ImageView imageView = new ImageView();
    (());
    (imageView);
   }
 
   @Override
   public void onVideo(CameraKitVideo cameraKitVideo) {
 
   }
  });
 }
 
 @OnClick(.btn_test)
 public void onViewClicked() {
  //Photograph  ();
 }
 
}

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.