JCameraView.java
21.9 KB
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package com.cjt2325.cameralibrary;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.VideoView;
import com.cjt2325.cameralibrary.listener.CaptureListener;
import com.cjt2325.cameralibrary.listener.ClickListener;
import com.cjt2325.cameralibrary.listener.ErrorListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
import com.cjt2325.cameralibrary.listener.TypeListener;
import com.cjt2325.cameralibrary.state.CameraMachine;
import com.cjt2325.cameralibrary.util.FileUtil;
import com.cjt2325.cameralibrary.util.LogUtil;
import com.cjt2325.cameralibrary.util.ScreenUtils;
import com.cjt2325.cameralibrary.view.CameraView;
/**
* =====================================
* 作 者: 陈嘉桐
* 版 本:1.0.4
* 创建日期:2017/4/25
* 描 述:
* =====================================
*/
public class JCameraView extends FrameLayout implements CameraInterface.CameraOpenOverCallback, SurfaceHolder
.Callback, CameraView {
// private static final String TAG = "JCameraView";
//Camera状态机
private CameraMachine machine;
//闪关灯状态
private static final int TYPE_FLASH_AUTO = 0x021;
private static final int TYPE_FLASH_ON = 0x022;
private static final int TYPE_FLASH_OFF = 0x023;
private int type_flash = TYPE_FLASH_OFF;
//拍照浏览时候的类型
public static final int TYPE_PICTURE = 0x001;
public static final int TYPE_VIDEO = 0x002;
public static final int TYPE_SHORT = 0x003;
public static final int TYPE_DEFAULT = 0x004;
//录制视频比特率
public static final int MEDIA_QUALITY_HIGH = 20 * 100000;
public static final int MEDIA_QUALITY_MIDDLE = 16 * 100000;
public static final int MEDIA_QUALITY_LOW = 12 * 100000;
public static final int MEDIA_QUALITY_POOR = 8 * 100000;
public static final int MEDIA_QUALITY_FUNNY = 4 * 100000;
public static final int MEDIA_QUALITY_DESPAIR = 2 * 100000;
public static final int MEDIA_QUALITY_SORRY = 1 * 80000;
public static final int BUTTON_STATE_ONLY_CAPTURE = 0x101; //只能拍照
public static final int BUTTON_STATE_ONLY_RECORDER = 0x102; //只能录像
public static final int BUTTON_STATE_BOTH = 0x103; //两者都可以
//回调监听
private JCameraListener jCameraLisenter;
private ClickListener leftClickListener;
private ClickListener rightClickListener;
private Context mContext;
private VideoView mVideoView;
private ImageView mPhoto;
private ImageView mSwitchCamera;
private ImageView mFlashLamp;
private CaptureLayout mCaptureLayout;
private FoucsView mFoucsView;
private MediaPlayer mMediaPlayer;
private int layout_width;
private int layout_Height;
private float screenProp = 0f;
private Bitmap captureBitmap; //捕获的图片
private Bitmap firstFrame; //第一帧图片
private String videoUrl; //视频URL
//切换摄像头按钮的参数
private int iconSize = 0; //图标大小
private int iconMargin = 0; //右上边距
private int iconSrc = 0; //图标资源
private int iconLeft = 0; //左图标
private int iconRight = 0; //右图标
private int duration = 0; //录制时间
//缩放梯度
private int zoomGradient = 0;
private boolean firstTouch = true;
private float firstTouchLength = 0;
public JCameraView(Context context) {
this(context, null);
}
public JCameraView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public JCameraView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
//get AttributeSet
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.JCameraView, defStyleAttr, 0);
iconSize = a.getDimensionPixelSize(R.styleable.JCameraView_iconSize, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 35, getResources().getDisplayMetrics()));
iconMargin = a.getDimensionPixelSize(R.styleable.JCameraView_iconMargin, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics()));
iconSrc = a.getResourceId(R.styleable.JCameraView_iconSrc, R.drawable.ic_camera);
iconLeft = a.getResourceId(R.styleable.JCameraView_iconLeft, 0);
iconRight = a.getResourceId(R.styleable.JCameraView_iconRight, 0);
duration = a.getInteger(R.styleable.JCameraView_duration_max, 10 * 1000); //没设置默认为10s
a.recycle();
initData();
initView();
}
public void setDuration(int duration) {
this.duration = duration;
if (mCaptureLayout != null)
mCaptureLayout.setDuration(duration);
}
private void initData() {
layout_width = ScreenUtils.getScreenWidth(mContext);
layout_Height = ScreenUtils.getScreenHeight(mContext);
//缩放梯度
zoomGradient = (int) (layout_width / 16f);
LogUtil.i("zoom = " + zoomGradient);
machine = new CameraMachine(getContext(), this, this);
}
private void initView() {
setWillNotDraw(false);
View view = LayoutInflater.from(mContext).inflate(R.layout.camera_view, this);
mVideoView = (VideoView) view.findViewById(R.id.video_preview);
mPhoto = (ImageView) view.findViewById(R.id.image_photo);
mSwitchCamera = (ImageView) view.findViewById(R.id.image_switch);
mSwitchCamera.setImageResource(iconSrc);
mFlashLamp = (ImageView) view.findViewById(R.id.image_flash);
setFlashRes();
mFlashLamp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
type_flash++;
if (type_flash > 0x023)
type_flash = TYPE_FLASH_AUTO;
setFlashRes();
}
});
mCaptureLayout = (CaptureLayout) view.findViewById(R.id.capture_layout);
mCaptureLayout.setDuration(duration);
mCaptureLayout.setIconSrc(iconLeft, iconRight);
mFoucsView = (FoucsView) view.findViewById(R.id.fouce_view);
mVideoView.getHolder().addCallback(this);
//切换摄像头
mSwitchCamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
machine.swtich(mVideoView.getHolder(), screenProp);
}
});
//拍照 录像
mCaptureLayout.setCaptureLisenter(new CaptureListener() {
@Override
public void takePictures() {
mSwitchCamera.setVisibility(INVISIBLE);
mFlashLamp.setVisibility(INVISIBLE);
machine.capture();
}
@Override
public void recordStart() {
mSwitchCamera.setVisibility(INVISIBLE);
mFlashLamp.setVisibility(INVISIBLE);
machine.record(mVideoView.getHolder().getSurface(), screenProp);
mCaptureLayout.setTip("点击结束录制");
mCaptureLayout.showTip();
}
@Override
public void recordShort(final long time) {
mCaptureLayout.setTextWithAnimation("录制时间过短");
mSwitchCamera.setVisibility(VISIBLE);
mFlashLamp.setVisibility(VISIBLE);
postDelayed(new Runnable() {
@Override
public void run() {
machine.stopRecord(true, time);
}
}, 1500 - time);
mCaptureLayout.setTip("点击开始录制");
}
@Override
public void recordEnd(long time) {
machine.stopRecord(false, time);
mCaptureLayout.setTip("录制完成");
}
@Override
public void recordZoom(float zoom) {
LogUtil.i("recordZoom");
machine.zoom(zoom, CameraInterface.TYPE_RECORDER);
}
@Override
public void recordError() {
if (errorLisenter != null) {
errorLisenter.AudioPermissionError();
}
}
});
//确认 取消
mCaptureLayout.setTypeLisenter(new TypeListener() {
@Override
public void cancel() {
machine.cancle(mVideoView.getHolder(), screenProp);
mCaptureLayout.setTip("点击开始录制");
}
@Override
public void confirm() {
machine.confirm();
}
});
//退出
// mCaptureLayout.setReturnLisenter(new ReturnListener() {
// @Override
// public void onReturn() {
// if (jCameraLisenter != null) {
// jCameraLisenter.quit();
// }
// }
// });
mCaptureLayout.setLeftClickListener(new ClickListener() {
@Override
public void onClick() {
if (leftClickListener != null) {
leftClickListener.onClick();
}
}
});
mCaptureLayout.setRightClickListener(new ClickListener() {
@Override
public void onClick() {
if (rightClickListener != null) {
rightClickListener.onClick();
}
}
});
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
float widthSize = mVideoView.getMeasuredWidth();
float heightSize = mVideoView.getMeasuredHeight();
if (screenProp == 0) {
screenProp = heightSize / widthSize;
}
}
@Override
public void cameraHasOpened() {
CameraInterface.getInstance().doStartPreview(mVideoView.getHolder(), screenProp);
}
//生命周期onResume
public void onResume() {
LogUtil.i("JCameraView onResume");
resetState(TYPE_DEFAULT); //重置状态
CameraInterface.getInstance().registerSensorManager(mContext);
CameraInterface.getInstance().setSwitchView(mSwitchCamera, mFlashLamp);
machine.start(mVideoView.getHolder(), screenProp);
}
//生命周期onPause
public void onPause() {
LogUtil.i("JCameraView onPause");
stopVideo();
resetState(TYPE_PICTURE);
CameraInterface.getInstance().isPreview(false);
CameraInterface.getInstance().unregisterSensorManager(mContext);
}
//SurfaceView生命周期
@Override
public void surfaceCreated(SurfaceHolder holder) {
LogUtil.i("JCameraView SurfaceCreated");
new Thread() {
@Override
public void run() {
CameraInterface.getInstance().doOpenCamera(JCameraView.this);
}
}.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
LogUtil.i("JCameraView SurfaceDestroyed");
CameraInterface.getInstance().doDestroyCamera();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (event.getPointerCount() == 1) {
//显示对焦指示器
setFocusViewWidthAnimation(event.getX(), event.getY());
}
if (event.getPointerCount() == 2) {
Log.i("CJT", "ACTION_DOWN = " + 2);
}
break;
case MotionEvent.ACTION_MOVE:
if (event.getPointerCount() == 1) {
firstTouch = true;
}
if (event.getPointerCount() == 2) {
//第一个点
float point_1_X = event.getX(0);
float point_1_Y = event.getY(0);
//第二个点
float point_2_X = event.getX(1);
float point_2_Y = event.getY(1);
float result = (float) Math.sqrt(Math.pow(point_1_X - point_2_X, 2) + Math.pow(point_1_Y -
point_2_Y, 2));
if (firstTouch) {
firstTouchLength = result;
firstTouch = false;
}
if ((int) (result - firstTouchLength) / zoomGradient != 0) {
firstTouch = true;
machine.zoom(result - firstTouchLength, CameraInterface.TYPE_CAPTURE);
}
// Log.i("CJT", "result = " + (result - firstTouchLength));
}
break;
case MotionEvent.ACTION_UP:
firstTouch = true;
break;
}
return true;
}
//对焦框指示器动画
private void setFocusViewWidthAnimation(float x, float y) {
machine.foucs(x, y, new CameraInterface.FocusCallback() {
@Override
public void focusSuccess() {
mFoucsView.setVisibility(INVISIBLE);
}
});
}
private void updateVideoViewSize(float videoWidth, float videoHeight) {
if (videoWidth > videoHeight) {
LayoutParams videoViewParam;
int height = (int) ((videoHeight / videoWidth) * getWidth());
videoViewParam = new LayoutParams(LayoutParams.MATCH_PARENT, height);
videoViewParam.gravity = Gravity.CENTER;
mVideoView.setLayoutParams(videoViewParam);
}
}
/**************************************************
* 对外提供的API *
**************************************************/
public void setSaveVideoPath(String path) {
CameraInterface.getInstance().setSaveVideoPath(path);
}
public void setJCameraLisenter(JCameraListener jCameraLisenter) {
this.jCameraLisenter = jCameraLisenter;
}
private ErrorListener errorLisenter;
//启动Camera错误回调
public void setErrorLisenter(ErrorListener errorLisenter) {
this.errorLisenter = errorLisenter;
CameraInterface.getInstance().setErrorLinsenter(errorLisenter);
}
//设置CaptureButton功能(拍照和录像)
public void setFeatures(int state) {
this.mCaptureLayout.setButtonFeatures(state);
}
//设置录制质量
public void setMediaQuality(int quality) {
CameraInterface.getInstance().setMediaQuality(quality);
}
@Override
public void resetState(int type) {
switch (type) {
case TYPE_VIDEO:
stopVideo(); //停止播放
//初始化VideoView
FileUtil.deleteFile(videoUrl);
mVideoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
machine.start(mVideoView.getHolder(), screenProp);
break;
case TYPE_PICTURE:
mPhoto.setVisibility(INVISIBLE);
break;
case TYPE_SHORT:
break;
case TYPE_DEFAULT:
mVideoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
break;
}
mSwitchCamera.setVisibility(VISIBLE);
mFlashLamp.setVisibility(VISIBLE);
mCaptureLayout.resetCaptureLayout();
}
@Override
public void confirmState(int type) {
switch (type) {
case TYPE_VIDEO:
stopVideo(); //停止播放
mVideoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
machine.start(mVideoView.getHolder(), screenProp);
if (jCameraLisenter != null) {
jCameraLisenter.recordSuccess(videoUrl, firstFrame);
}
break;
case TYPE_PICTURE:
mPhoto.setVisibility(INVISIBLE);
if (jCameraLisenter != null) {
jCameraLisenter.captureSuccess(captureBitmap);
}
break;
case TYPE_SHORT:
break;
case TYPE_DEFAULT:
break;
}
mCaptureLayout.resetCaptureLayout();
}
@Override
public void showPicture(Bitmap bitmap, boolean isVertical) {
if (isVertical) {
mPhoto.setScaleType(ImageView.ScaleType.FIT_XY);
} else {
mPhoto.setScaleType(ImageView.ScaleType.FIT_CENTER);
}
captureBitmap = bitmap;
mPhoto.setImageBitmap(bitmap);
mPhoto.setVisibility(VISIBLE);
mCaptureLayout.startAlphaAnimation();
mCaptureLayout.startTypeBtnAnimator();
}
@Override
public void playVideo(Bitmap firstFrame, final String url) {
videoUrl = url;
JCameraView.this.firstFrame = firstFrame;
new Thread(new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void run() {
try {
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
} else {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(url);
mMediaPlayer.setSurface(mVideoView.getHolder().getSurface());
mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer
.OnVideoSizeChangedListener() {
@Override
public void
onVideoSizeChanged(MediaPlayer mp, int width, int height) {
updateVideoViewSize(mMediaPlayer.getVideoWidth(), mMediaPlayer
.getVideoHeight());
}
});
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
}
});
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void stopVideo() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
@Override
public void setTip(String tip) {
mCaptureLayout.setTip(tip);
}
@Override
public void startPreviewCallback() {
LogUtil.i("startPreviewCallback");
handlerFoucs(mFoucsView.getWidth() / 2, mFoucsView.getHeight() / 2);
}
@Override
public boolean handlerFoucs(float x, float y) {
if (y > mCaptureLayout.getTop()) {
return false;
}
mFoucsView.setVisibility(VISIBLE);
if (x < mFoucsView.getWidth() / 2) {
x = mFoucsView.getWidth() / 2;
}
if (x > layout_width - mFoucsView.getWidth() / 2) {
x = layout_width - mFoucsView.getWidth() / 2;
}
if (y < mFoucsView.getWidth() / 2) {
y = mFoucsView.getWidth() / 2;
}
if (y > mCaptureLayout.getTop() - mFoucsView.getWidth() / 2) {
y = mCaptureLayout.getTop() - mFoucsView.getWidth() / 2;
}
mFoucsView.setX(x - mFoucsView.getWidth() / 2);
mFoucsView.setY(y - mFoucsView.getHeight() / 2);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(mFoucsView, "scaleX", 1, 0.6f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(mFoucsView, "scaleY", 1, 0.6f);
ObjectAnimator alpha = ObjectAnimator.ofFloat(mFoucsView, "alpha", 1f, 0.4f, 1f, 0.4f, 1f, 0.4f, 1f);
AnimatorSet animSet = new AnimatorSet();
animSet.play(scaleX).with(scaleY).before(alpha);
animSet.setDuration(400);
animSet.start();
return true;
}
public void setLeftClickListener(ClickListener clickListener) {
this.leftClickListener = clickListener;
}
public void setRightClickListener(ClickListener clickListener) {
this.rightClickListener = clickListener;
}
private void setFlashRes() {
switch (type_flash) {
case TYPE_FLASH_AUTO:
mFlashLamp.setImageResource(R.drawable.ic_flash_auto);
machine.flash(Camera.Parameters.FLASH_MODE_AUTO);
break;
case TYPE_FLASH_ON:
mFlashLamp.setImageResource(R.drawable.ic_flash_on);
machine.flash(Camera.Parameters.FLASH_MODE_ON);
break;
case TYPE_FLASH_OFF:
mFlashLamp.setImageResource(R.drawable.ic_flash_off);
machine.flash(Camera.Parameters.FLASH_MODE_OFF);
break;
}
}
}