Android quickly realizes the function of taking pictures without preview
- 2021-12-04 19:44:48
- OfStack
In this paper, we share the specific code of Android to realize the photo function without preview for your reference. The specific contents are as follows
Implementation ideas:
Set the width and height of the preview SurfaceView to a value invisible to the naked eye, such as 0.1 dp, and others are the standard steps for customizing the camera!
Of course, there are many articles about self-defined cameras on the Internet, so it is impossible for me to do the same thing again. Here I recommend a very easy-to-use third-party library, which may have been known and used by many people. Students who have not used it can try it and test it well.
Needless to say, first on the source address
Here is a brief introduction to the following usage:
1. Add library dependencies to gradle: compile 'com. wonderkiln: camerakit: 0.13. 1'
2. Layout file code:
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Test "
android:layout_gravity="center_horizontal"/>
<com.wonderkiln.camerakit.CameraView
android:id="@+id/camera"
android:layout_width="0.1dp"
android:layout_height="0.1dp"
android:adjustViewBounds="true"
camerakit:ckFacing="front" />
</LinearLayout>
camerakit: ckFacing= "front" means using front camera. Please refer to official documents 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(R.id.btn_test)
Button btnTest;
@BindView(R.id.camera)
CameraView cameraView;
@BindView(R.id.ll_content)
LinearLayout llContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
}
@Override
protected void onResume() {
super.onResume();
cameraView.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
cameraView.stop();
super.onPause();
}
private void initView() {
// Set callback
cameraView.addCameraKitListener(new CameraKitEventListener() {
@Override
public void onEvent(CameraKitEvent cameraKitEvent) {
}
@Override
public void onError(CameraKitError cameraKitError) {
}
@Override
public void onImage(CameraKitImage cameraKitImage) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageBitmap(cameraKitImage.getBitmap());
llContent.addView(imageView);
}
@Override
public void onVideo(CameraKitVideo cameraKitVideo) {
}
});
}
@OnClick(R.id.btn_test)
public void onViewClicked() {
// Photographing
cameraView.captureImage();
}
}