doum
6 天以前 2b287056e2f59518888d05a1bbc7e5a55fbd84d5
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package com.example.datalibrary.gatecamera;
 
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.TextureView;
import android.view.ViewGroup;
import android.view.ViewParent;
 
import com.example.datalibrary.callback.CameraDataCallback;
import com.example.datalibrary.gl.view.GLFaceSurfaceView;
import com.example.datalibrary.gl.view.GlMantleSurfacView;
 
import java.io.IOException;
import java.util.List;
 
/**
 * Time: 2019/1/24
 * Author: v_chaixiaogang
 * Description:
 */
public class CameraPreviewManager implements SurfaceHolder.Callback , TextureView.SurfaceTextureListener {
 
    private static final String TAG = "camera_preview";
 
 
    GLFaceSurfaceView surfaceView;
    boolean mPreviewed = false;
 
    public static final int CAMERA_FACING_BACK = 0;
 
    public static final int CAMERA_FACING_FRONT = 1;
 
    public static final int CAMERA_USB = 2;
 
    /**
     * 当前相机的ID。
     */
    private int cameraFacing = CAMERA_FACING_BACK;
 
    private int previewWidth;
    private int previewHeight;
 
    private int videoWidth;
    private int videoHeight;
 
    private int surfaceWidth;
    private int surfaceHeight;
 
    private Camera mCamera;
 
    private int displayOrientation = 0;
 
    private int videoDirection;
 
    private GlMantleSurfacView mTextureView;
    private SurfaceTexture mSurfaceTexture;
    private int mirrorVideoRGB;
    public void setmCameraDataCallback(CameraDataCallback mCameraDataCallback) {
        this.mCameraDataCallback = mCameraDataCallback;
    }
 
    public void destroy() {
 
    }
 
    private CameraDataCallback mCameraDataCallback;
    private static volatile CameraPreviewManager instance = null;
 
    private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
 
    static {
 
        ORIENTATIONS.append(Surface.ROTATION_0, 0);
        ORIENTATIONS.append(Surface.ROTATION_90, 90);
        ORIENTATIONS.append(Surface.ROTATION_180, 180);
        ORIENTATIONS.append(Surface.ROTATION_270, 270);
    }
 
    public static CameraPreviewManager getInstance() {
        synchronized (CameraPreviewManager.class) {
            if (instance == null) {
                instance = new CameraPreviewManager();
            }
        }
        return instance;
    }
 
    public int getCameraFacing() {
        return cameraFacing;
    }
 
    public void setCameraFacing(int cameraFacing) {
        this.cameraFacing = cameraFacing;
    }
 
    public int getDisplayOrientation() {
        return displayOrientation;
    }
 
    public void setDisplayOrientation(int displayOrientation) {
        this.displayOrientation = displayOrientation;
    }
 
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
 
    }
 
    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, final int width, final int height) {
        if (surfaceView != null){
            surfaceWidth = width;
            surfaceHeight = height;
            surfaceView.surfaceChanged(surfaceHolder);
 
            openCamera();
        }
    }
    /**
     * 开启预览
     * @param textureView
     */
    public void startPreview(GlMantleSurfacView textureView, int videoDirection ,
                             int width, int height) {
        Log.e(TAG, "开启预览模式");
        mTextureView = textureView;
        if (textureView.getGlFaceSurfaceView() != null){
            surfaceView = textureView.getGlFaceSurfaceView();
            this.previewWidth = width;
            this.previewHeight = height;
            this.videoDirection = videoDirection;
        }else {
            this.previewWidth = width;
            this.previewHeight = height;
            this.videoDirection = videoDirection;
            this.mirrorVideoRGB = textureView.getMirrorRGB();
            mSurfaceTexture = mTextureView.getTextureView().getSurfaceTexture();
            mTextureView.getTextureView().setSurfaceTextureListener(this);
        }
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        if (surfaceView != null){
            //System.out.println("==内存==>surfaceDestroyed");
            surfaceView.surfaceDestroyed();
        }
    }
 
    /**
     * 关闭预览
     */
    public void stopPreview() {
        if (mCamera != null) {
            try {
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();
                mPreviewed = false;
                mCamera.release();
                mCamera = null;
            } catch (Exception e) {
                Log.e("qing", "camera destory error");
                e.printStackTrace();
 
            }
        }
    }
    public static interface ErrInfoCallBack{
        void err(String errInfo);
    }
 
    public int[] initCamera(ErrInfoCallBack callBack){
        try {
            if (mCamera == null) {
                mCamera = Camera.open(cameraFacing);
                Log.e(TAG, "initCamera---open camera");
            }
            // 摄像头图像预览角度
            int cameraRotation = videoDirection;
            mCamera.setDisplayOrientation(cameraRotation);
            if (mTextureView != null){
                if (cameraRotation == 90 || cameraRotation == 270) {
                    if (mirrorVideoRGB == 1) {
                        mTextureView.setRotationY(180);
                    } else {
                        mTextureView.setRotationY(0);
                    }
                    // 旋转90度或者270,需要调整宽高
                    mTextureView.setPreviewSize(previewHeight, previewWidth);
                } else {
                    if (mirrorVideoRGB == 1) {
                        mTextureView.setRotationY(180);
                    } else {
                        mTextureView.setRotationY(0);
                    }
                    mTextureView.setPreviewSize(previewWidth, previewHeight);
                }
            }
            Camera.Parameters params = mCamera.getParameters();
            List<Camera.Size> sizeList = params.getSupportedPreviewSizes(); // 获取所有支持的camera尺寸
            final Camera.Size optionSize = getOptimalPreviewSize(sizeList, previewWidth,
                    previewHeight); // 获取一个最为适配的camera.size
            if (optionSize.width == previewWidth && optionSize.height == previewHeight) {
                videoWidth = previewWidth;
                videoHeight = previewHeight;
            } else {
                videoWidth = optionSize.width;
                videoHeight = optionSize.height;
            }
            params.setPreviewSize(videoWidth, videoHeight);
 
            mCamera.setParameters(params);
            if (mTextureView != null){
                try {
                    mCamera.setPreviewCallback(new Camera.PreviewCallback() {
                        @Override
                        public void onPreviewFrame(byte[] bytes, Camera camera) {
                            if (mCameraDataCallback != null) {
                                mCameraDataCallback.onGetCameraData(bytes, camera,
                                        videoWidth, videoHeight);
                            }
                        }
                    });
                    mCamera.setPreviewTexture(mSurfaceTexture);
 
                    mCamera.startPreview();
 
                } catch (Exception e) {
                    e.printStackTrace();
                    if(callBack!=null){
                        callBack.err("开启相机报错 (2)==>"+e.getMessage());
                    }
                    Log.e(TAG, e.getMessage());
                }
            }
            if (surfaceView != null){
                surfaceView.getHolder().addCallback(this);
                surfaceView.setWillNotDraw(false);
            }
 
        }catch (Exception e){
            e.printStackTrace();
            if(callBack!=null){
                callBack.err("开启相机报错 (1)==>"+e.getMessage());
            }
        }
        return new int[]{videoWidth , videoHeight};
    }
    /**
     * 开启摄像头
     */
    public void openCamera() {
 
        try {
            try {
                mCamera.setPreviewTexture(surfaceView.getFramebuffer());
                mCamera.setPreviewCallback(new Camera.PreviewCallback() {
                    @Override
                    public void onPreviewFrame(byte[] bytes, Camera camera) {
                        if (mCameraDataCallback != null) {
                            mCameraDataCallback.onGetCameraData(bytes, camera,
                                    videoWidth, videoHeight);
                        }
                    }
                });
                mCamera.startPreview();
 
                surfaceView.initFrame(surfaceWidth , surfaceHeight);
                if (videoDirection == 90 || videoDirection == 270){
                    surfaceView.setSize(videoHeight , videoWidth);
                }else {
                    surfaceView.setSize(videoWidth , videoHeight);
 
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, e.getMessage());
            }
        } catch (RuntimeException e) {
            Log.e(TAG, e.getMessage());
        }
    }
 
 
    private int getCameraDisplayOrientation(int degrees, int cameraId) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(cameraId, info);
        int rotation = 0;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            rotation = (info.orientation + degrees) % 360;
            rotation = (360 - rotation) % 360;
        } else { // back-facing
            rotation = (info.orientation - degrees + 360) % 360;
        }
        return rotation;
    }
 
 
    /**
     * 解决预览变形问题
     *
     * @param sizes
     * @param w
     * @param h
     * @return
     */
    private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
        final double aspectTolerance = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null) {
            return null;
        }
        Camera.Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;
 
        int targetHeight = h;
 
        // Try to find an size match aspect ratio and size
        for (Camera.Size size : sizes) {
 
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > aspectTolerance) {
                continue;
            }
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
 
        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Camera.Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }
 
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture texture, int i, int i1) {
        try {
            if (mCamera != null && !mPreviewed) {
                mSurfaceTexture = texture;
                mCamera.setPreviewTexture(mSurfaceTexture);
                mCamera.startPreview();
                mPreviewed = true;
            }
        } catch (Exception exception) {
            Log.e("chaixiaogang", "IOException caused by setPreviewDisplay()", exception);
        }
    }
 
    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
        Log.e(TAG, "--surfaceTexture--TextureSizeChanged");
    }
 
    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        if (mCamera != null) {
            mCamera.stopPreview();
            mPreviewed = false;
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
        mCameraDataCallback = null;
        //System.out.println("==内存==>onSurfaceTextureDestroyed");
        if(mSurfaceTexture!=null) {
            mSurfaceTexture.release();
            mSurfaceTexture = null;
        }
        if (surfaceView != null){
            surfaceView.surfaceDestroyed();
            surfaceView = null;
        }
        if(mTextureView!=null) {
            //System.out.println("==内存==>mTextureView");
            mTextureView.getTextureView().setSurfaceTextureListener(null);
            mTextureView.destroy();
            mTextureView = null;
        }
        return true;
    }
 
    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        Log.e(TAG, "--surfaceTexture--Updated");
    }
}