Introduction to the

Camera module library, custom camera, through a simple call can realize the function of taking pictures, picture cutting, video and video capture; Image compression, reduce image volume; Custom camera can avoid the large size of photos or videos caused by using system camera; Processing of access to built-in camera and SD card; Github link below, help star support ~ github link

Realization function: – photo – picture cutting – video – video capture

Import the library in your project

Add the following to your project build.gradle:

allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }Copy the code

Add dependencies to build.gradle in module:

Dependencies {compile 'com.github. Autume :syd-camera:v1.0.0'}Copy the code

use

Taking pictures

For details, see CameraTestActivity in demo

Start the camera

Refer to the following methods to enter the picture quality, photo minimum width configuration, camera preview interface minimum width configuration, directly jump to the photo interface for taking pictures; – picQuality: image quality 0~100, default 80 – picWidth: photo minimum width, default 800 – previewWidth: camera preview interface minimum width, default 1280 – pictureSize: Limit the size of a photo, in KB. If the photo is not saved, it will be compressed only according to the image quality. The actual compressed size will be slightly larger than this value

Intent intent = new Intent(CameraTestActivity.this, SydCameraActivity.class); intent.putExtra(CameraParaUtil.picQuality, 70); Intent. PutExtra (cameraparautil.picWidth, 1536); Configuration/photo/minimum width, height according to the proportion of screen automatically configure intent. PutExtra (CameraParaUtil previewWidth, 1280); Minimum width / / camera preview interface configuration, height according to the proportion of screen automatically configure startActivityForResult (intent, CameraParaUtil. CameraRequestCode);Copy the code

Receive photo return

Photos get to store photos in return path can according to need to deal with pictures, after access path: picturePath = data. GetStringExtra (CameraParaUtil. PicturePath);

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Log.i(TAG, "onActivityResult resultCode:" + resultCode + ",requestCode: " + requestCode); If (resultCode == activity.result_canceled){log. I (TAG, "Canceled! ); return; } if (resultCode ! = activity.result_ok){log.w (TAG, "Failed to take photo!" ); return; } if (requestCode == CameraParaUtil.cameraRequestCode) { String picturePath; picturePath = data.getStringExtra(CameraParaUtil.picturePath); img_photo.setImageBitmap(BitmapFactory.decodeFile(picturePath)); Log.d(TAG, "onActivityResult picturePath: " + picturePath); }}Copy the code

Image cropping

Start the cutting

Refer to the following way to pass in the picture quality, to be cropped picture path, directly jump to the cropping interface for picture cropping; – cropTitle: indicates the title of the clipping interface. – cropDestPicPath: indicates the name of the folder where the picture is to be clipped. – cropSrcPicPath: indicates the path of the source file to be clipped

private void startCrop(String path) { Intent intent = new Intent(CameraTestActivity.this, IcomwellCropActivity.class); intent.putExtra(CropParaUtil.cropQuality, 70); Intent. PutExtra (cropparautil.croptitle, "Add cover "); intent.putExtra(CropParaUtil.cropSrcPicPath, path); startActivityForResult(intent, CropParaUtil.REQUEST_CODE_FROM_CUTTING); }Copy the code

Receive clipping return

Cut back for stored image path after cutting in according to the demand for image processing, path gain: data. GetStringExtra (CropParaUtil. CropDestPicPath);

     switch (requestCode) {
            case CropParaUtil.REQUEST_CODE_FROM_CUTTING:
                String cropDestPicPath;
                cropDestPicPath = data.getStringExtra(CropParaUtil.cropDestPicPath);
                img_photo.setImageBitmap(BitmapFactory.decodeFile(cropDestPicPath));
                Log.d(TAG, "onActivityResult cropDestPicPath: " + cropDestPicPath);
                break;
            default:
                break;
        }
Copy the code

Custom picture cropping UI

If you want to customize the clipping interface U, see SydCropActivity to customize the layout

Video recording

Refer to the following method to input relevant parameters, directly jump to the video interface for recording; – picQuality: video quality 0~100, default 80 – picWidth: video minimum width, default 800 – previewWidth: camera preview interface minimum width, default 1280 – pictureSize: – picDuration: automatic capture duration (unit: second), 3600 seconds by default, disables automatic capture duration if the value is less than or equal to 0. Video segmentation interval, in seconds (default: 1800 seconds) Photos and videos are located in the sydPhoto and sydVideo folders respectively

private void startVideo() { Intent intent = new Intent(CameraTestActivity.this, SydVideoActivity.class); intent.putExtra(CameraParaUtil.picQuality, 70); Intent. PutExtra (cameraparautil.picWidth, 1536); Configuration/photo/minimum width, height according to the proportion of screen automatically configure intent. PutExtra (CameraParaUtil previewWidth, 1280); StartActivityForResult (Intent, cameraparautil.request_code_from_video); }Copy the code

Special cases are compatible

Samsung A8 has a file storage problem without permission. The processing method is to save the photo or cropped image in Bitmap and return the Bitmap directly

Public static Bitmap croppedBitmap public static Bitmap croppedBitmap; Public static Bitmap pictureBitmap; public static Bitmap pictureBitmap; /** * Compatible Samsung A8 has no permission to store files. * After the phone is restarted, files can be stored normally. * @param resultCode */ private void onResultExceptionHandle(int resultCode) { Pictures failed after check CameraParaUtil. PictureBitmap have data if (the resultCode = = CameraParaUtil. REQUEST_CODE_FROM_CAMERA_FAIL && CameraParaUtil.pictureBitmap ! = null) {if (invoke.ispicNeedDeal) {// Use pictureBitmap to crop startCropForBitmap(); } else {/ / don't need to cut uploadPicture (CameraParaUtil. PictureBitmap); }} // Clipping failure processing, for Samsung A8 cannot store file processing, Failed after check CropParaUtil. CroppedBitmap whether there is data if (the resultCode = = CropParaUtil. REQUEST_CODE_FROM_CUTTING_FAIL && CropParaUtil.croppedBitmap ! = null) { uploadPicture(CropParaUtil.croppedBitmap); }}Copy the code