lmw
2023-06-16 03972ad1d3ce6ffe0be0395c0a4d5dcb4474031f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.luck.picture.lib;
 
 
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.core.CameraX;
import androidx.camera.view.CameraView;
 
import com.luck.picture.lib.camera.CustomCameraView;
import com.luck.picture.lib.camera.listener.CameraListener;
import com.luck.picture.lib.camera.view.CaptureLayout;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.tools.ToastUtils;
 
import java.io.File;
import java.lang.ref.WeakReference;
 
/**
 * @author:luck
 * @date:2020-01-04 14:05
 * @describe:自定义拍照和录音
 */
public class PictureCustomCameraActivity extends PictureSelectorCameraEmptyActivity {
 
 
    private CustomCameraView mCameraView;
 
    @Override
    public boolean isImmersive() {
        return false;
    }
 
    @Override
    public int getResourceId() {
        return R.layout.picture_custom_camera;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }
        super.onCreate(savedInstanceState);
    }
 
    @Override
    protected void initWidgets() {
        super.initWidgets();
        mCameraView = findViewById(R.id.camera_view);
        mCameraView.setPictureSelectionConfig(config);
        // 绑定生命周期
        mCameraView.setBindToLifecycle(new WeakReference<>(this).get());
        // 视频最大拍摄时长
        if (config.recordVideoSecond > 0) {
            mCameraView.setRecordVideoMaxTime(config.recordVideoSecond);
        }
        // 视频最小拍摄时长
        if (config.recordVideoMinSecond > 0) {
            mCameraView.setRecordVideoMinTime(config.recordVideoMinSecond);
        }
        // 获取CameraView
        CameraView cameraView = mCameraView.getCameraView();
        if (cameraView != null && config.isCameraAroundState) {
            cameraView.toggleCamera();
        }
        // 获取录制按钮
        CaptureLayout captureLayout = mCameraView.getCaptureLayout();
        if (captureLayout != null) {
            captureLayout.setButtonFeatures(config.buttonFeatures);
        }
        // 拍照预览
        mCameraView.setImageCallbackListener((file, imageView) -> {
            if (config != null && config.imageEngine != null && file != null) {
                config.imageEngine.loadImage(getContext(), file.getAbsolutePath(), imageView);
            }
        });
        // 设置拍照或拍视频回调监听
        mCameraView.setCameraListener(new CameraListener() {
            @Override
            public void onPictureSuccess(@NonNull File file) {
                if (config.camera) {
                    Intent intent = new Intent();
                    intent.putExtra(PictureConfig.EXTRA_MEDIA_PATH, file.getAbsolutePath());
                    requestCamera(intent);
                } else {
                    setResult(RESULT_OK);
                    onBackPressed();
                }
            }
 
            @Override
            public void onRecordSuccess(@NonNull File file) {
                if (config.camera) {
                    Intent intent = new Intent();
                    intent.putExtra(PictureConfig.EXTRA_MEDIA_PATH, file.getAbsolutePath());
                    requestCamera(intent);
                } else {
                    setResult(RESULT_OK);
                    onBackPressed();
                }
            }
 
            @Override
            public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
                ToastUtils.s(getContext(), message);
                onBackPressed();
            }
        });
 
        //左边按钮点击事件
        mCameraView.setOnClickListener(() -> onBackPressed());
    }
 
    @Override
    public void onBackPressed() {
        closeActivity();
    }
 
    @SuppressLint("RestrictedApi")
    @Override
    protected void onDestroy() {
        CameraX.unbindAll();
        super.onDestroy();
    }
}