Commit 31cde7c5d39ddec17da5dd1fd7b5b18153cb2227
1 parent
70f46c48
重构项目
Showing
39 changed files
with
0 additions
and
4193 deletions
Too many changes to show.
To preserve performance only 39 of 138 files are displayed.
core/base/src/main/java/com/cjt2325/cameralibrary/CameraInterface.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary; | ||
| 2 | - | ||
| 3 | -import android.animation.AnimatorSet; | ||
| 4 | -import android.animation.ObjectAnimator; | ||
| 5 | -import android.content.Context; | ||
| 6 | -import android.graphics.Bitmap; | ||
| 7 | -import android.graphics.BitmapFactory; | ||
| 8 | -import android.graphics.ImageFormat; | ||
| 9 | -import android.graphics.Matrix; | ||
| 10 | -import android.graphics.Rect; | ||
| 11 | -import android.graphics.RectF; | ||
| 12 | -import android.hardware.Camera; | ||
| 13 | -import android.hardware.Sensor; | ||
| 14 | -import android.hardware.SensorEvent; | ||
| 15 | -import android.hardware.SensorEventListener; | ||
| 16 | -import android.hardware.SensorManager; | ||
| 17 | -import android.media.MediaRecorder; | ||
| 18 | -import android.os.Build; | ||
| 19 | -import android.os.Environment; | ||
| 20 | -import android.util.Log; | ||
| 21 | -import android.view.Surface; | ||
| 22 | -import android.view.SurfaceHolder; | ||
| 23 | -import android.widget.ImageView; | ||
| 24 | - | ||
| 25 | -import com.cjt2325.cameralibrary.listener.ErrorListener; | ||
| 26 | -import com.cjt2325.cameralibrary.util.AngleUtil; | ||
| 27 | -import com.cjt2325.cameralibrary.util.CameraParamUtil; | ||
| 28 | -import com.cjt2325.cameralibrary.util.CheckPermission; | ||
| 29 | -import com.cjt2325.cameralibrary.util.DeviceUtil; | ||
| 30 | -import com.cjt2325.cameralibrary.util.FileUtil; | ||
| 31 | -import com.cjt2325.cameralibrary.util.LogUtil; | ||
| 32 | -import com.cjt2325.cameralibrary.util.ScreenUtils; | ||
| 33 | - | ||
| 34 | -import java.io.File; | ||
| 35 | -import java.io.IOException; | ||
| 36 | -import java.util.ArrayList; | ||
| 37 | -import java.util.List; | ||
| 38 | - | ||
| 39 | -import static android.graphics.Bitmap.createBitmap; | ||
| 40 | - | ||
| 41 | -/** | ||
| 42 | - * ===================================== | ||
| 43 | - * 作 者: 陈嘉桐 | ||
| 44 | - * 版 本:1.1.4 | ||
| 45 | - * 创建日期:2017/4/25 | ||
| 46 | - * 描 述:camera操作单例 | ||
| 47 | - * ===================================== | ||
| 48 | - */ | ||
| 49 | -@SuppressWarnings("deprecation") | ||
| 50 | -public class CameraInterface implements Camera.PreviewCallback { | ||
| 51 | - | ||
| 52 | - private static final String TAG = "CJT"; | ||
| 53 | - | ||
| 54 | - private volatile static CameraInterface mCameraInterface; | ||
| 55 | - | ||
| 56 | - public static void destroyCameraInterface() { | ||
| 57 | - if (mCameraInterface != null) { | ||
| 58 | - mCameraInterface = null; | ||
| 59 | - } | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - private Camera mCamera; | ||
| 63 | - private Camera.Parameters mParams; | ||
| 64 | - private boolean isPreviewing = false; | ||
| 65 | - | ||
| 66 | - private int SELECTED_CAMERA = -1; | ||
| 67 | - private int CAMERA_POST_POSITION = -1; | ||
| 68 | - private int CAMERA_FRONT_POSITION = -1; | ||
| 69 | - | ||
| 70 | - private SurfaceHolder mHolder = null; | ||
| 71 | - private float screenProp = -1.0f; | ||
| 72 | - | ||
| 73 | - private boolean isRecorder = false; | ||
| 74 | - private MediaRecorder mediaRecorder; | ||
| 75 | - private String videoFileName; | ||
| 76 | - private String saveVideoPath; | ||
| 77 | - private String videoFileAbsPath; | ||
| 78 | - | ||
| 79 | - private ErrorListener errorLisenter; | ||
| 80 | - | ||
| 81 | - private ImageView mSwitchView; | ||
| 82 | - private ImageView mFlashLamp; | ||
| 83 | - | ||
| 84 | - private int preview_width; | ||
| 85 | - private int preview_height; | ||
| 86 | - | ||
| 87 | - private int angle = 0; | ||
| 88 | - private int cameraAngle = 90;//摄像头角度 默认为90度 | ||
| 89 | - private int rotation = 0; | ||
| 90 | - private byte[] firstFrame_data; | ||
| 91 | - | ||
| 92 | - public static final int TYPE_RECORDER = 0x090; | ||
| 93 | - public static final int TYPE_CAPTURE = 0x091; | ||
| 94 | - private int nowScaleRate = 0; | ||
| 95 | - private int recordScleRate = 0; | ||
| 96 | - | ||
| 97 | - //视频质量 | ||
| 98 | - private int mediaQuality = JCameraView.MEDIA_QUALITY_HIGH; | ||
| 99 | - | ||
| 100 | - //获取CameraInterface单例 | ||
| 101 | - public static synchronized CameraInterface getInstance() { | ||
| 102 | - if (mCameraInterface == null) | ||
| 103 | - synchronized (CameraInterface.class) { | ||
| 104 | - if (mCameraInterface == null) | ||
| 105 | - mCameraInterface = new CameraInterface(); | ||
| 106 | - } | ||
| 107 | - return mCameraInterface; | ||
| 108 | - } | ||
| 109 | - | ||
| 110 | - public void setSwitchView(ImageView mSwitchView, ImageView mFlashLamp) { | ||
| 111 | - this.mSwitchView = mSwitchView; | ||
| 112 | - this.mFlashLamp = mFlashLamp; | ||
| 113 | - if (mSwitchView != null) { | ||
| 114 | - cameraAngle = CameraParamUtil.getInstance().getCameraDisplayOrientation(mSwitchView.getContext(), | ||
| 115 | - SELECTED_CAMERA); | ||
| 116 | - } | ||
| 117 | - } | ||
| 118 | - | ||
| 119 | - private SensorEventListener sensorEventListener = new SensorEventListener() { | ||
| 120 | - public void onSensorChanged(SensorEvent event) { | ||
| 121 | - if (Sensor.TYPE_ACCELEROMETER != event.sensor.getType()) { | ||
| 122 | - return; | ||
| 123 | - } | ||
| 124 | - float[] values = event.values; | ||
| 125 | - angle = AngleUtil.getSensorAngle(values[0], values[1]); | ||
| 126 | - rotationAnimation(); | ||
| 127 | - } | ||
| 128 | - | ||
| 129 | - public void onAccuracyChanged(Sensor sensor, int accuracy) { | ||
| 130 | - } | ||
| 131 | - }; | ||
| 132 | - | ||
| 133 | - //切换摄像头icon跟随手机角度进行旋转 | ||
| 134 | - private void rotationAnimation() { | ||
| 135 | - if (mSwitchView == null) { | ||
| 136 | - return; | ||
| 137 | - } | ||
| 138 | - if (rotation != angle) { | ||
| 139 | - int start_rotaion = 0; | ||
| 140 | - int end_rotation = 0; | ||
| 141 | - switch (rotation) { | ||
| 142 | - case 0: | ||
| 143 | - start_rotaion = 0; | ||
| 144 | - switch (angle) { | ||
| 145 | - case 90: | ||
| 146 | - end_rotation = -90; | ||
| 147 | - break; | ||
| 148 | - case 270: | ||
| 149 | - end_rotation = 90; | ||
| 150 | - break; | ||
| 151 | - } | ||
| 152 | - break; | ||
| 153 | - case 90: | ||
| 154 | - start_rotaion = -90; | ||
| 155 | - switch (angle) { | ||
| 156 | - case 0: | ||
| 157 | - end_rotation = 0; | ||
| 158 | - break; | ||
| 159 | - case 180: | ||
| 160 | - end_rotation = -180; | ||
| 161 | - break; | ||
| 162 | - } | ||
| 163 | - break; | ||
| 164 | - case 180: | ||
| 165 | - start_rotaion = 180; | ||
| 166 | - switch (angle) { | ||
| 167 | - case 90: | ||
| 168 | - end_rotation = 270; | ||
| 169 | - break; | ||
| 170 | - case 270: | ||
| 171 | - end_rotation = 90; | ||
| 172 | - break; | ||
| 173 | - } | ||
| 174 | - break; | ||
| 175 | - case 270: | ||
| 176 | - start_rotaion = 90; | ||
| 177 | - switch (angle) { | ||
| 178 | - case 0: | ||
| 179 | - end_rotation = 0; | ||
| 180 | - break; | ||
| 181 | - case 180: | ||
| 182 | - end_rotation = 180; | ||
| 183 | - break; | ||
| 184 | - } | ||
| 185 | - break; | ||
| 186 | - } | ||
| 187 | - ObjectAnimator animC = ObjectAnimator.ofFloat(mSwitchView, "rotation", start_rotaion, end_rotation); | ||
| 188 | -// ObjectAnimator animF = ObjectAnimator.ofFloat(mFlashLamp, "rotation", start_rotaion, end_rotation); | ||
| 189 | - AnimatorSet set = new AnimatorSet(); | ||
| 190 | - set.playTogether(animC); | ||
| 191 | - set.setDuration(500); | ||
| 192 | - set.start(); | ||
| 193 | - rotation = angle; | ||
| 194 | - } | ||
| 195 | - } | ||
| 196 | - | ||
| 197 | - @SuppressWarnings("ResultOfMethodCallIgnored") | ||
| 198 | - void setSaveVideoPath(String saveVideoPath) { | ||
| 199 | - this.saveVideoPath = saveVideoPath; | ||
| 200 | - File file = new File(saveVideoPath); | ||
| 201 | - if (!file.exists()) { | ||
| 202 | - file.mkdirs(); | ||
| 203 | - } | ||
| 204 | - } | ||
| 205 | - | ||
| 206 | - | ||
| 207 | - public void setZoom(float zoom, int type) { | ||
| 208 | - if (mCamera == null) { | ||
| 209 | - return; | ||
| 210 | - } | ||
| 211 | - try { | ||
| 212 | - if (mParams == null) { | ||
| 213 | - mParams = mCamera.getParameters(); | ||
| 214 | - } | ||
| 215 | - if (!mParams.isZoomSupported() || !mParams.isSmoothZoomSupported()) { | ||
| 216 | - return; | ||
| 217 | - } | ||
| 218 | - switch (type) { | ||
| 219 | - case TYPE_RECORDER: | ||
| 220 | - //如果不是录制视频中,上滑不会缩放 | ||
| 221 | - if (!isRecorder) { | ||
| 222 | - return; | ||
| 223 | - } | ||
| 224 | - if (zoom >= 0) { | ||
| 225 | - //每移动50个像素缩放一个级别 | ||
| 226 | - int scaleRate = (int) (zoom / 40); | ||
| 227 | - if (scaleRate <= mParams.getMaxZoom() && scaleRate >= nowScaleRate && recordScleRate != scaleRate) { | ||
| 228 | - mParams.setZoom(scaleRate); | ||
| 229 | - mCamera.setParameters(mParams); | ||
| 230 | - recordScleRate = scaleRate; | ||
| 231 | - } | ||
| 232 | - } | ||
| 233 | - break; | ||
| 234 | - case TYPE_CAPTURE: | ||
| 235 | - if (isRecorder) { | ||
| 236 | - return; | ||
| 237 | - } | ||
| 238 | - //每移动50个像素缩放一个级别 | ||
| 239 | - int scaleRate = (int) (zoom / 50); | ||
| 240 | - if (scaleRate < mParams.getMaxZoom()) { | ||
| 241 | - nowScaleRate += scaleRate; | ||
| 242 | - if (nowScaleRate < 0) { | ||
| 243 | - nowScaleRate = 0; | ||
| 244 | - } else if (nowScaleRate > mParams.getMaxZoom()) { | ||
| 245 | - nowScaleRate = mParams.getMaxZoom(); | ||
| 246 | - } | ||
| 247 | - mParams.setZoom(nowScaleRate); | ||
| 248 | - mCamera.setParameters(mParams); | ||
| 249 | - } | ||
| 250 | - LogUtil.i("setZoom = " + nowScaleRate); | ||
| 251 | - break; | ||
| 252 | - } | ||
| 253 | - } catch (Exception ignored) { | ||
| 254 | - | ||
| 255 | - } | ||
| 256 | - } | ||
| 257 | - | ||
| 258 | - void setMediaQuality(int quality) { | ||
| 259 | - this.mediaQuality = quality; | ||
| 260 | - } | ||
| 261 | - | ||
| 262 | - | ||
| 263 | - @Override | ||
| 264 | - public void onPreviewFrame(byte[] data, Camera camera) { | ||
| 265 | - firstFrame_data = data; | ||
| 266 | - } | ||
| 267 | - | ||
| 268 | - public void setFlashMode(String flashMode) { | ||
| 269 | - if (mCamera == null) | ||
| 270 | - return; | ||
| 271 | - try { | ||
| 272 | - | ||
| 273 | - Camera.Parameters parameters = mCamera.getParameters(); | ||
| 274 | - List<String> flashModes = parameters.getSupportedFlashModes(); | ||
| 275 | - if (flashModes == null) { | ||
| 276 | - return; | ||
| 277 | - } | ||
| 278 | - | ||
| 279 | - if (flashMode.contains(Camera.Parameters.FLASH_MODE_ON)) { | ||
| 280 | - parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON); | ||
| 281 | - mCamera.setParameters(parameters); | ||
| 282 | - } else if (flashMode.contains(Camera.Parameters.FLASH_MODE_OFF)) { | ||
| 283 | - parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); | ||
| 284 | - mCamera.setParameters(parameters); | ||
| 285 | - } | ||
| 286 | - | ||
| 287 | - return; | ||
| 288 | - } catch (Exception e) { | ||
| 289 | - Log.e("Camera", "闪光灯异常", e); | ||
| 290 | - } | ||
| 291 | - return; | ||
| 292 | - } | ||
| 293 | - | ||
| 294 | - public int getSelectedCamera() { | ||
| 295 | - return SELECTED_CAMERA; | ||
| 296 | - } | ||
| 297 | - | ||
| 298 | - public interface CameraOpenOverCallback { | ||
| 299 | - void cameraHasOpened(); | ||
| 300 | - } | ||
| 301 | - | ||
| 302 | - private CameraInterface() { | ||
| 303 | - findAvailableCameras(); | ||
| 304 | - SELECTED_CAMERA = CAMERA_POST_POSITION; | ||
| 305 | - saveVideoPath = ""; | ||
| 306 | - } | ||
| 307 | - | ||
| 308 | - | ||
| 309 | - /** | ||
| 310 | - * open Camera | ||
| 311 | - */ | ||
| 312 | - void doOpenCamera(CameraOpenOverCallback callback) { | ||
| 313 | - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { | ||
| 314 | - if (!CheckPermission.isCameraUseable(SELECTED_CAMERA) && this.errorLisenter != null) { | ||
| 315 | - this.errorLisenter.onError(); | ||
| 316 | - return; | ||
| 317 | - } | ||
| 318 | - } | ||
| 319 | - if (mCamera == null) { | ||
| 320 | - openCamera(SELECTED_CAMERA); | ||
| 321 | - } | ||
| 322 | - callback.cameraHasOpened(); | ||
| 323 | - } | ||
| 324 | - | ||
| 325 | - private void setFlashModel() { | ||
| 326 | - mParams = mCamera.getParameters(); | ||
| 327 | - mParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); //设置camera参数为Torch模式 | ||
| 328 | - mCamera.setParameters(mParams); | ||
| 329 | - } | ||
| 330 | - | ||
| 331 | - private synchronized boolean openCamera(int id) { | ||
| 332 | - try { | ||
| 333 | - this.mCamera = Camera.open(id); | ||
| 334 | - } catch (Exception e) { | ||
| 335 | - if (this.errorLisenter != null) this.errorLisenter.onError(); | ||
| 336 | - Log.e("CJT", "enable shutter sound faild", e); | ||
| 337 | - } | ||
| 338 | - | ||
| 339 | - try { | ||
| 340 | - if (Build.VERSION.SDK_INT > 17 && this.mCamera != null) { | ||
| 341 | - this.mCamera.enableShutterSound(false); | ||
| 342 | - } | ||
| 343 | - } catch (Exception e) { | ||
| 344 | - Log.e("CJT", "enable shutter sound faild", e); | ||
| 345 | - } | ||
| 346 | - | ||
| 347 | - return mCamera != null; | ||
| 348 | - } | ||
| 349 | - | ||
| 350 | - public synchronized boolean switchCamera(SurfaceHolder holder, float screenProp) { | ||
| 351 | - if (SELECTED_CAMERA == CAMERA_POST_POSITION) { | ||
| 352 | - SELECTED_CAMERA = CAMERA_FRONT_POSITION; | ||
| 353 | - } else { | ||
| 354 | - SELECTED_CAMERA = CAMERA_POST_POSITION; | ||
| 355 | - } | ||
| 356 | - doDestroyCamera(); | ||
| 357 | - LogUtil.i("open start"); | ||
| 358 | - openCamera(SELECTED_CAMERA); | ||
| 359 | - LogUtil.i("open end"); | ||
| 360 | - return doStartPreview(holder, screenProp); | ||
| 361 | - } | ||
| 362 | - | ||
| 363 | - /** | ||
| 364 | - * doStartPreview | ||
| 365 | - */ | ||
| 366 | - public boolean doStartPreview(SurfaceHolder holder, float screenProp) { | ||
| 367 | - if (isPreviewing) { | ||
| 368 | - LogUtil.i("doStartPreview isPreviewing"); | ||
| 369 | - } | ||
| 370 | - if (this.screenProp < 0) { | ||
| 371 | - this.screenProp = screenProp; | ||
| 372 | - } | ||
| 373 | - if (holder == null) { | ||
| 374 | - return false; | ||
| 375 | - } | ||
| 376 | - this.mHolder = holder; | ||
| 377 | - if (mCamera != null) { | ||
| 378 | - try { | ||
| 379 | - mParams = mCamera.getParameters(); | ||
| 380 | - Camera.Size previewSize = CameraParamUtil.getInstance().getPreviewSize(mParams | ||
| 381 | - .getSupportedPreviewSizes(), 1000, screenProp); | ||
| 382 | - Camera.Size pictureSize = CameraParamUtil.getInstance().getPictureSize(mParams | ||
| 383 | - .getSupportedPictureSizes(), 1200, screenProp); | ||
| 384 | - | ||
| 385 | - mParams.setPreviewSize(previewSize.width, previewSize.height); | ||
| 386 | - | ||
| 387 | - preview_width = previewSize.width; | ||
| 388 | - preview_height = previewSize.height; | ||
| 389 | - | ||
| 390 | - mParams.setPictureSize(pictureSize.width, pictureSize.height); | ||
| 391 | - | ||
| 392 | - if (CameraParamUtil.getInstance().isSupportedFocusMode( | ||
| 393 | - mParams.getSupportedFocusModes(), | ||
| 394 | - Camera.Parameters.FOCUS_MODE_AUTO)) { | ||
| 395 | - mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); | ||
| 396 | - } | ||
| 397 | - if (CameraParamUtil.getInstance().isSupportedPictureFormats(mParams.getSupportedPictureFormats(), | ||
| 398 | - ImageFormat.JPEG)) { | ||
| 399 | - mParams.setPictureFormat(ImageFormat.JPEG); | ||
| 400 | - mParams.setJpegQuality(100); | ||
| 401 | - } | ||
| 402 | - try { | ||
| 403 | - mCamera.setParameters(mParams); | ||
| 404 | - } catch (Exception e) { | ||
| 405 | - Log.e("Camera", "开启预览异常", e); | ||
| 406 | - } | ||
| 407 | - // mParams = mCamera.getParameters(); | ||
| 408 | - mCamera.setPreviewDisplay(holder); //SurfaceView | ||
| 409 | - mCamera.setDisplayOrientation(cameraAngle);//浏览角度 | ||
| 410 | - mCamera.setPreviewCallback(this); //每一帧回调 | ||
| 411 | - mCamera.startPreview();//启动浏览 | ||
| 412 | - isPreviewing = true; | ||
| 413 | - Log.i(TAG, "=== Start Preview ==="); | ||
| 414 | - return true; | ||
| 415 | - } catch (Exception e) { | ||
| 416 | - Log.e("Camera", "开启预览异常", e); | ||
| 417 | - } | ||
| 418 | - } | ||
| 419 | - return false; | ||
| 420 | - } | ||
| 421 | - | ||
| 422 | - /** | ||
| 423 | - * 停止预览 | ||
| 424 | - */ | ||
| 425 | - public void doStopPreview() { | ||
| 426 | - if (null != mCamera) { | ||
| 427 | - try { | ||
| 428 | - mCamera.setPreviewCallback(null); | ||
| 429 | - mCamera.stopPreview(); | ||
| 430 | - //这句要在stopPreview后执行,不然会卡顿或者花屏 | ||
| 431 | - mCamera.setPreviewDisplay(null); | ||
| 432 | - isPreviewing = false; | ||
| 433 | - mCamera.lock(); | ||
| 434 | - Log.i(TAG, "=== Stop Preview ==="); | ||
| 435 | - } catch (Exception e) { | ||
| 436 | - Log.e("Camera", "", e); | ||
| 437 | - } | ||
| 438 | - } | ||
| 439 | - } | ||
| 440 | - | ||
| 441 | - /** | ||
| 442 | - * 销毁Camera | ||
| 443 | - */ | ||
| 444 | - boolean doDestroyCamera() { | ||
| 445 | - errorLisenter = null; | ||
| 446 | - if (null != mCamera) { | ||
| 447 | - try { | ||
| 448 | - mCamera.setPreviewCallback(null); | ||
| 449 | - mSwitchView = null; | ||
| 450 | -// mFlashLamp = null; | ||
| 451 | - mCamera.stopPreview(); | ||
| 452 | - //这句要在stopPreview后执行,不然会卡顿或者花屏 | ||
| 453 | - mCamera.setPreviewDisplay(null); | ||
| 454 | - mHolder = null; | ||
| 455 | - isPreviewing = false; | ||
| 456 | - mCamera.lock(); | ||
| 457 | - mCamera.release(); | ||
| 458 | - mCamera = null; | ||
| 459 | -// destroyCameraInterface(); | ||
| 460 | - Log.i(TAG, "=== Destroy Camera ==="); | ||
| 461 | - return true; | ||
| 462 | - } catch (Exception e) { | ||
| 463 | - Log.e("Camera", "", e); | ||
| 464 | - } | ||
| 465 | - return false; | ||
| 466 | - } else { | ||
| 467 | - Log.i(TAG, "=== Camera Null==="); | ||
| 468 | - } | ||
| 469 | - return true; | ||
| 470 | - } | ||
| 471 | - | ||
| 472 | - /** | ||
| 473 | - * 拍照 | ||
| 474 | - */ | ||
| 475 | - private int nowAngle; | ||
| 476 | - | ||
| 477 | - public boolean takePicture(TakePictureCallback callback) { | ||
| 478 | - if (mCamera == null) { | ||
| 479 | - return false; | ||
| 480 | - } | ||
| 481 | - switch (cameraAngle) { | ||
| 482 | - case 90: | ||
| 483 | - nowAngle = Math.abs(angle + cameraAngle) % 360; | ||
| 484 | - break; | ||
| 485 | - case 270: | ||
| 486 | - nowAngle = Math.abs(cameraAngle - angle); | ||
| 487 | - break; | ||
| 488 | - } | ||
| 489 | -// | ||
| 490 | - Log.i("CJT", angle + " = " + cameraAngle + " = " + nowAngle); | ||
| 491 | - try { | ||
| 492 | - mCamera.takePicture(null, null, new Camera.PictureCallback() { | ||
| 493 | - @Override | ||
| 494 | - public void onPictureTaken(byte[] data, Camera camera) { | ||
| 495 | - Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); | ||
| 496 | - Matrix matrix = new Matrix(); | ||
| 497 | - if (SELECTED_CAMERA == CAMERA_POST_POSITION) { | ||
| 498 | - matrix.setRotate(nowAngle); | ||
| 499 | - } else if (SELECTED_CAMERA == CAMERA_FRONT_POSITION) { | ||
| 500 | - matrix.setRotate(360 - nowAngle); | ||
| 501 | - matrix.postScale(-1, 1); | ||
| 502 | - } | ||
| 503 | - | ||
| 504 | - bitmap = createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); | ||
| 505 | - if (callback != null) { | ||
| 506 | - if (nowAngle == 90 || nowAngle == 270) { | ||
| 507 | - callback.captureResult(bitmap, true); | ||
| 508 | - } else { | ||
| 509 | - callback.captureResult(bitmap, false); | ||
| 510 | - } | ||
| 511 | - } | ||
| 512 | - } | ||
| 513 | - }); | ||
| 514 | - return true; | ||
| 515 | - } catch (Exception e) { | ||
| 516 | - Log.e("Camera", "拍摄异常", e); | ||
| 517 | - } | ||
| 518 | - return false; | ||
| 519 | - } | ||
| 520 | - | ||
| 521 | - //启动录像 | ||
| 522 | - public boolean startRecord(Surface surface, float screenProp, ErrorCallback callback,Context context) { | ||
| 523 | - if (mCamera == null) return false; | ||
| 524 | - mCamera.setPreviewCallback(null); | ||
| 525 | - final int nowAngle = (angle + 90) % 360; | ||
| 526 | - //获取第一帧图片 | ||
| 527 | -// Camera.Parameters parameters = mCamera.getParameters(); | ||
| 528 | -// int width = parameters.getPreviewSize().width; | ||
| 529 | -// int height = parameters.getPreviewSize().height; | ||
| 530 | -// YuvImage yuv = new YuvImage(firstFrame_data, parameters.getPreviewFormat(), width, height, null); | ||
| 531 | -// ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| 532 | -// yuv.compressToJpeg(new Rect(0, 0, width, height), 50, out); | ||
| 533 | -// Matrix matrix = new Matrix(); | ||
| 534 | -// if (SELECTED_CAMERA == CAMERA_POST_POSITION) { | ||
| 535 | -// matrix.setRotate(nowAngle); | ||
| 536 | -// } else if (SELECTED_CAMERA == CAMERA_FRONT_POSITION) { | ||
| 537 | -// matrix.setRotate(270); | ||
| 538 | -// } | ||
| 539 | - | ||
| 540 | - if (isRecorder) { | ||
| 541 | - return true; | ||
| 542 | - } | ||
| 543 | - if (mCamera == null) { | ||
| 544 | - openCamera(SELECTED_CAMERA); | ||
| 545 | - } | ||
| 546 | - if (mCamera == null) { | ||
| 547 | - return false; | ||
| 548 | - } | ||
| 549 | - if (mediaRecorder == null) { | ||
| 550 | - mediaRecorder = new MediaRecorder(); | ||
| 551 | - } | ||
| 552 | - if (mParams == null) { | ||
| 553 | - mParams = mCamera.getParameters(); | ||
| 554 | - } | ||
| 555 | - List<String> focusModes = mParams.getSupportedFocusModes(); | ||
| 556 | - if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { | ||
| 557 | - mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); | ||
| 558 | - } | ||
| 559 | - try { | ||
| 560 | - mCamera.setParameters(mParams); | ||
| 561 | - } catch (Exception e) { | ||
| 562 | - | ||
| 563 | - } | ||
| 564 | - try { | ||
| 565 | - mCamera.unlock(); | ||
| 566 | - } catch (Exception e) { | ||
| 567 | - | ||
| 568 | - } | ||
| 569 | - try { | ||
| 570 | - mediaRecorder.reset(); | ||
| 571 | - mediaRecorder.setCamera(mCamera); | ||
| 572 | - mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); | ||
| 573 | - mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); | ||
| 574 | - mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); | ||
| 575 | - mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); | ||
| 576 | - mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); | ||
| 577 | - | ||
| 578 | - | ||
| 579 | - Camera.Size videoSize; | ||
| 580 | - if (mParams.getSupportedVideoSizes() == null) { | ||
| 581 | - videoSize = CameraParamUtil.getInstance().getPreviewSize(mParams.getSupportedPreviewSizes(), 600, | ||
| 582 | - screenProp); | ||
| 583 | - } else { | ||
| 584 | - videoSize = CameraParamUtil.getInstance().getPreviewSize(mParams.getSupportedVideoSizes(), 600, | ||
| 585 | - screenProp); | ||
| 586 | - } | ||
| 587 | - Log.i(TAG, "setVideoSize width = " + videoSize.width + "height = " + videoSize.height); | ||
| 588 | - if (videoSize.width == videoSize.height) { | ||
| 589 | - mediaRecorder.setVideoSize(preview_width, preview_height); | ||
| 590 | - } else { | ||
| 591 | - mediaRecorder.setVideoSize(videoSize.width, videoSize.height); | ||
| 592 | - } | ||
| 593 | -// if (SELECTED_CAMERA == CAMERA_FRONT_POSITION) { | ||
| 594 | -// mediaRecorder.setOrientationHint(270); | ||
| 595 | -// } else { | ||
| 596 | -// mediaRecorder.setOrientationHint(nowAngle); | ||
| 597 | -//// mediaRecorder.setOrientationHint(90); | ||
| 598 | -// } | ||
| 599 | - | ||
| 600 | - if (SELECTED_CAMERA == CAMERA_FRONT_POSITION) { | ||
| 601 | - //手机预览倒立的处理 | ||
| 602 | - if (cameraAngle == 270) { | ||
| 603 | - //横屏 | ||
| 604 | - if (nowAngle == 0) { | ||
| 605 | - mediaRecorder.setOrientationHint(180); | ||
| 606 | - } else if (nowAngle == 270) { | ||
| 607 | - mediaRecorder.setOrientationHint(270); | ||
| 608 | - } else { | ||
| 609 | - mediaRecorder.setOrientationHint(90); | ||
| 610 | - } | ||
| 611 | - } else { | ||
| 612 | - if (nowAngle == 90) { | ||
| 613 | - mediaRecorder.setOrientationHint(270); | ||
| 614 | - } else if (nowAngle == 270) { | ||
| 615 | - mediaRecorder.setOrientationHint(90); | ||
| 616 | - } else { | ||
| 617 | - mediaRecorder.setOrientationHint(nowAngle); | ||
| 618 | - } | ||
| 619 | - } | ||
| 620 | - } else { | ||
| 621 | - mediaRecorder.setOrientationHint(nowAngle); | ||
| 622 | - } | ||
| 623 | - | ||
| 624 | - | ||
| 625 | - if (DeviceUtil.isHuaWeiRongyao()) { | ||
| 626 | - mediaRecorder.setVideoEncodingBitRate(4 * 100000); | ||
| 627 | - } else { | ||
| 628 | - mediaRecorder.setVideoEncodingBitRate(mediaQuality); | ||
| 629 | - } | ||
| 630 | - mediaRecorder.setPreviewDisplay(surface); | ||
| 631 | - | ||
| 632 | - videoFileName = "video_" + System.currentTimeMillis() + ".mp4"; | ||
| 633 | - if (saveVideoPath.equals("")) { | ||
| 634 | - saveVideoPath = (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q ?context.getExternalFilesDir("") : Environment.getExternalStorageDirectory()).getPath(); | ||
| 635 | - } | ||
| 636 | - videoFileAbsPath = saveVideoPath + File.separator + videoFileName; | ||
| 637 | - mediaRecorder.setOutputFile(videoFileAbsPath); | ||
| 638 | - | ||
| 639 | - mediaRecorder.prepare(); | ||
| 640 | - mediaRecorder.start(); | ||
| 641 | - isRecorder = true; | ||
| 642 | - return true; | ||
| 643 | - } catch (IllegalStateException e) { | ||
| 644 | - e.printStackTrace(); | ||
| 645 | - Log.i("CJT", "startRecord IllegalStateException"); | ||
| 646 | - if (this.errorLisenter != null) { | ||
| 647 | - this.errorLisenter.onError(); | ||
| 648 | - } | ||
| 649 | - } catch (IOException e) { | ||
| 650 | - e.printStackTrace(); | ||
| 651 | - Log.i("CJT", "startRecord IOException"); | ||
| 652 | - if (this.errorLisenter != null) { | ||
| 653 | - this.errorLisenter.onError(); | ||
| 654 | - } | ||
| 655 | - } catch (RuntimeException e) { | ||
| 656 | - Log.i("CJT", "startRecord RuntimeException"); | ||
| 657 | - } catch (Exception e) { | ||
| 658 | - Log.i("CJT", "startRecord Exception"); | ||
| 659 | - } | ||
| 660 | - return false; | ||
| 661 | - } | ||
| 662 | - | ||
| 663 | - public void onDestroy() { | ||
| 664 | - if (isRecorder) { | ||
| 665 | - return; | ||
| 666 | - } | ||
| 667 | - if (mediaRecorder != null) { | ||
| 668 | - mediaRecorder.setOnErrorListener(null); | ||
| 669 | - mediaRecorder.setOnInfoListener(null); | ||
| 670 | - mediaRecorder.setPreviewDisplay(null); | ||
| 671 | - try { | ||
| 672 | - mediaRecorder.stop(); | ||
| 673 | - } catch (RuntimeException e) { | ||
| 674 | - e.printStackTrace(); | ||
| 675 | - mediaRecorder = null; | ||
| 676 | - mediaRecorder = new MediaRecorder(); | ||
| 677 | - } finally { | ||
| 678 | - if (mediaRecorder != null) mediaRecorder.release(); | ||
| 679 | - mediaRecorder = null; | ||
| 680 | - isRecorder = false; | ||
| 681 | - } | ||
| 682 | - } | ||
| 683 | - doStopPreview(); | ||
| 684 | - } | ||
| 685 | - | ||
| 686 | - //停止录像 | ||
| 687 | - public void stopRecord(boolean isShort, StopRecordCallback callback) { | ||
| 688 | - if (!isRecorder) { | ||
| 689 | - return; | ||
| 690 | - } | ||
| 691 | - if (mediaRecorder != null) { | ||
| 692 | - mediaRecorder.setOnErrorListener(null); | ||
| 693 | - mediaRecorder.setOnInfoListener(null); | ||
| 694 | - mediaRecorder.setPreviewDisplay(null); | ||
| 695 | - try { | ||
| 696 | - mediaRecorder.stop(); | ||
| 697 | - } catch (RuntimeException e) { | ||
| 698 | - e.printStackTrace(); | ||
| 699 | - mediaRecorder = null; | ||
| 700 | - mediaRecorder = new MediaRecorder(); | ||
| 701 | - } finally { | ||
| 702 | - if (mediaRecorder != null) { | ||
| 703 | - mediaRecorder.release(); | ||
| 704 | - } | ||
| 705 | - mediaRecorder = null; | ||
| 706 | - isRecorder = false; | ||
| 707 | - } | ||
| 708 | - if (isShort) { | ||
| 709 | - if (FileUtil.deleteFile(videoFileAbsPath)) { | ||
| 710 | - callback.recordResult(null); | ||
| 711 | - } | ||
| 712 | - return; | ||
| 713 | - } | ||
| 714 | - doStopPreview(); | ||
| 715 | - String fileName = saveVideoPath + File.separator + videoFileName; | ||
| 716 | - callback.recordResult(fileName); | ||
| 717 | - } | ||
| 718 | - } | ||
| 719 | - | ||
| 720 | - private void findAvailableCameras() { | ||
| 721 | - Camera.CameraInfo info = new Camera.CameraInfo(); | ||
| 722 | - int cameraNum = Camera.getNumberOfCameras(); | ||
| 723 | - for (int i = 0; i < cameraNum; i++) { | ||
| 724 | - //TODO 存在异常隐患 | ||
| 725 | - try { | ||
| 726 | - Camera.getCameraInfo(i, info); | ||
| 727 | - } catch (Exception e) { | ||
| 728 | - Log.e("CameraInterface", "相机信息获取异常", e); | ||
| 729 | - continue; | ||
| 730 | - } | ||
| 731 | - switch (info.facing) { | ||
| 732 | - case Camera.CameraInfo.CAMERA_FACING_FRONT: | ||
| 733 | - CAMERA_FRONT_POSITION = info.facing; | ||
| 734 | - break; | ||
| 735 | - case Camera.CameraInfo.CAMERA_FACING_BACK: | ||
| 736 | - CAMERA_POST_POSITION = info.facing; | ||
| 737 | - break; | ||
| 738 | - } | ||
| 739 | - } | ||
| 740 | - } | ||
| 741 | - | ||
| 742 | - private int handlerTime = 0; | ||
| 743 | - | ||
| 744 | - public boolean handleFocus(final Context context, final float x, final float y, final FocusCallback callback) { | ||
| 745 | - if (mCamera == null) { | ||
| 746 | - return false; | ||
| 747 | - } | ||
| 748 | - try { | ||
| 749 | - final Camera.Parameters params = mCamera.getParameters(); | ||
| 750 | - Rect focusRect = calculateTapArea(x, y, 1f, context); | ||
| 751 | - mCamera.cancelAutoFocus(); | ||
| 752 | - if (params.getMaxNumFocusAreas() > 0) { | ||
| 753 | - List<Camera.Area> focusAreas = new ArrayList<>(); | ||
| 754 | - focusAreas.add(new Camera.Area(focusRect, 800)); | ||
| 755 | - params.setFocusAreas(focusAreas); | ||
| 756 | - } else { | ||
| 757 | - Log.i(TAG, "focus areas not supported"); | ||
| 758 | - callback.focusSuccess(); | ||
| 759 | - return false; | ||
| 760 | - } | ||
| 761 | - final String currentFocusMode = params.getFocusMode(); | ||
| 762 | - | ||
| 763 | - params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); | ||
| 764 | - mCamera.setParameters(params); | ||
| 765 | - mCamera.autoFocus(new Camera.AutoFocusCallback() { | ||
| 766 | - @Override | ||
| 767 | - public void onAutoFocus(boolean success, Camera camera) { | ||
| 768 | - if (success || handlerTime > 10) { | ||
| 769 | - Camera.Parameters params = camera.getParameters(); | ||
| 770 | - params.setFocusMode(currentFocusMode); | ||
| 771 | - camera.setParameters(params); | ||
| 772 | - handlerTime = 0; | ||
| 773 | - callback.focusSuccess(); | ||
| 774 | - } else { | ||
| 775 | - handlerTime++; | ||
| 776 | - handleFocus(context, x, y, callback); | ||
| 777 | - } | ||
| 778 | - } | ||
| 779 | - }); | ||
| 780 | - return true; | ||
| 781 | - } catch (Exception e) { | ||
| 782 | - Log.e(TAG, "autoFocus failer"); | ||
| 783 | - } | ||
| 784 | - return false; | ||
| 785 | - } | ||
| 786 | - | ||
| 787 | - | ||
| 788 | - private static Rect calculateTapArea(float x, float y, float coefficient, Context context) { | ||
| 789 | - float focusAreaSize = 300; | ||
| 790 | - int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue(); | ||
| 791 | - int centerX = (int) (x / ScreenUtils.getScreenWidth(context) * 2000 - 1000); | ||
| 792 | - int centerY = (int) (y / ScreenUtils.getScreenHeight(context) * 2000 - 1000); | ||
| 793 | - int left = clamp(centerX - areaSize / 2, -1000, 1000); | ||
| 794 | - int top = clamp(centerY - areaSize / 2, -1000, 1000); | ||
| 795 | - RectF rectF = new RectF(left, top, left + areaSize, top + areaSize); | ||
| 796 | - return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), Math.round(rectF | ||
| 797 | - .bottom)); | ||
| 798 | - } | ||
| 799 | - | ||
| 800 | - private static int clamp(int x, int min, int max) { | ||
| 801 | - if (x > max) { | ||
| 802 | - return max; | ||
| 803 | - } | ||
| 804 | - if (x < min) { | ||
| 805 | - return min; | ||
| 806 | - } | ||
| 807 | - return x; | ||
| 808 | - } | ||
| 809 | - | ||
| 810 | - void setErrorLinsenter(ErrorListener errorLisenter) { | ||
| 811 | - this.errorLisenter = errorLisenter; | ||
| 812 | - } | ||
| 813 | - | ||
| 814 | - | ||
| 815 | - public interface StopRecordCallback { | ||
| 816 | - void recordResult(String url); | ||
| 817 | - } | ||
| 818 | - | ||
| 819 | - interface ErrorCallback { | ||
| 820 | - void onError(); | ||
| 821 | - } | ||
| 822 | - | ||
| 823 | - public interface TakePictureCallback { | ||
| 824 | - void captureResult(Bitmap bitmap, boolean isVertical); | ||
| 825 | - } | ||
| 826 | - | ||
| 827 | - public interface FocusCallback { | ||
| 828 | - void focusSuccess(); | ||
| 829 | - | ||
| 830 | - } | ||
| 831 | - | ||
| 832 | - | ||
| 833 | - void registerSensorManager(Context context) { | ||
| 834 | - SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); | ||
| 835 | - if (sm == null) return; | ||
| 836 | - Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | ||
| 837 | - sm.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL); | ||
| 838 | - } | ||
| 839 | - | ||
| 840 | - void unregisterSensorManager(Context context) { | ||
| 841 | - SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); | ||
| 842 | - if (sm == null) return; | ||
| 843 | - sm.unregisterListener(sensorEventListener); | ||
| 844 | - } | ||
| 845 | - | ||
| 846 | - void isPreview(boolean res) { | ||
| 847 | - this.isPreviewing = res; | ||
| 848 | - } | ||
| 849 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/CaptureButton.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary; | ||
| 2 | - | ||
| 3 | -import android.animation.Animator; | ||
| 4 | -import android.animation.AnimatorListenerAdapter; | ||
| 5 | -import android.animation.AnimatorSet; | ||
| 6 | -import android.animation.ValueAnimator; | ||
| 7 | -import android.content.Context; | ||
| 8 | -import android.graphics.Canvas; | ||
| 9 | -import android.graphics.Color; | ||
| 10 | -import android.graphics.Paint; | ||
| 11 | -import android.graphics.RectF; | ||
| 12 | -import android.os.CountDownTimer; | ||
| 13 | -import android.view.MotionEvent; | ||
| 14 | -import android.view.View; | ||
| 15 | - | ||
| 16 | -import com.cjt2325.cameralibrary.listener.CaptureListener; | ||
| 17 | -import com.cjt2325.cameralibrary.util.CheckPermission; | ||
| 18 | -import com.cjt2325.cameralibrary.util.LogUtil; | ||
| 19 | - | ||
| 20 | -import static com.cjt2325.cameralibrary.JCameraView.BUTTON_STATE_BOTH; | ||
| 21 | -import static com.cjt2325.cameralibrary.JCameraView.BUTTON_STATE_ONLY_CAPTURE; | ||
| 22 | -import static com.cjt2325.cameralibrary.JCameraView.BUTTON_STATE_ONLY_RECORDER; | ||
| 23 | - | ||
| 24 | - | ||
| 25 | -/** | ||
| 26 | - * ===================================== | ||
| 27 | - * 作 者: 陈嘉桐 445263848@qq.com | ||
| 28 | - * 版 本:1.1.4 | ||
| 29 | - * 创建日期:2017/4/25 | ||
| 30 | - * 描 述:拍照按钮 | ||
| 31 | - * ===================================== | ||
| 32 | - */ | ||
| 33 | -public class CaptureButton extends View { | ||
| 34 | - | ||
| 35 | - private int state; //当前按钮状态 | ||
| 36 | - private int button_state; //按钮可执行的功能状态(拍照,录制,两者) | ||
| 37 | - | ||
| 38 | - public static final int STATE_IDLE = 0x001; //空闲状态 | ||
| 39 | - public static final int STATE_PRESS = 0x002; //按下状态 | ||
| 40 | - public static final int STATE_LONG_PRESS = 0x003; //长按状态 | ||
| 41 | - public static final int STATE_RECORDERING = 0x004; //录制状态 | ||
| 42 | - public static final int STATE_BAN = 0x005; //禁止状态 | ||
| 43 | - | ||
| 44 | - private int progress_color = Color.GREEN; //进度条颜色 | ||
| 45 | - private int outside_color = 0xEEf2f2f2; //外圆背景色 | ||
| 46 | - private int inside_color = Color.WHITE; //内圆背景色 | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - private float event_Y; //Touch_Event_Down时候记录的Y值 | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - private Paint mPaint; | ||
| 53 | - | ||
| 54 | - private float strokeWidth; //进度条宽度 | ||
| 55 | - private int outside_add_size; //长按外圆半径变大的Size | ||
| 56 | - private int inside_reduce_size; //长安内圆缩小的Size | ||
| 57 | - | ||
| 58 | - //中心坐标 | ||
| 59 | - private float center_X; | ||
| 60 | - private float center_Y; | ||
| 61 | - | ||
| 62 | - private float button_radius; //按钮半径 | ||
| 63 | - private float button_outside_radius; //外圆半径 | ||
| 64 | - private float button_inside_radius; //内圆半径 | ||
| 65 | - private int button_size; //按钮大小 | ||
| 66 | - | ||
| 67 | - private float progress; //录制视频的进度 | ||
| 68 | - private int duration; //录制视频最大时间长度 | ||
| 69 | - private int min_duration; //最短录制时间限制 | ||
| 70 | - private int recorded_time; //记录当前录制的时间 | ||
| 71 | - | ||
| 72 | - private RectF rectF; | ||
| 73 | - | ||
| 74 | - private LongPressRunnable longPressRunnable; //长按后处理的逻辑Runnable | ||
| 75 | - private CaptureListener captureLisenter; //按钮回调接口 | ||
| 76 | - private RecordCountDownTimer timer; //计时器 | ||
| 77 | - | ||
| 78 | - public CaptureButton(Context context) { | ||
| 79 | - super(context); | ||
| 80 | - } | ||
| 81 | - | ||
| 82 | - public CaptureButton(Context context, int size) { | ||
| 83 | - super(context); | ||
| 84 | - this.button_size = size; | ||
| 85 | - button_radius = size / 2.0f; | ||
| 86 | - | ||
| 87 | - button_outside_radius = button_radius; | ||
| 88 | - button_inside_radius = button_radius * 0.75f; | ||
| 89 | - | ||
| 90 | - strokeWidth = size / 15; | ||
| 91 | - outside_add_size = size / 5; | ||
| 92 | - inside_reduce_size = size / 8; | ||
| 93 | - | ||
| 94 | - mPaint = new Paint(); | ||
| 95 | - mPaint.setAntiAlias(true); | ||
| 96 | - | ||
| 97 | - progress = 0; | ||
| 98 | - longPressRunnable = new LongPressRunnable(); | ||
| 99 | - | ||
| 100 | - state = STATE_IDLE; //初始化为空闲状态 | ||
| 101 | - LogUtil.i("CaptureButtom start"); | ||
| 102 | - duration = 10 * 1000; //默认最长录制时间为10s | ||
| 103 | - LogUtil.i("CaptureButtom end"); | ||
| 104 | - min_duration = 2000; //默认最短录制时间为1.5s | ||
| 105 | - | ||
| 106 | - center_X = (button_size + outside_add_size * 2) / 2; | ||
| 107 | - center_Y = (button_size + outside_add_size * 2) / 2; | ||
| 108 | - | ||
| 109 | - rectF = new RectF( | ||
| 110 | - center_X - (button_radius + outside_add_size - strokeWidth / 2), | ||
| 111 | - center_Y - (button_radius + outside_add_size - strokeWidth / 2), | ||
| 112 | - center_X + (button_radius + outside_add_size - strokeWidth / 2), | ||
| 113 | - center_Y + (button_radius + outside_add_size - strokeWidth / 2)); | ||
| 114 | - | ||
| 115 | - timer = new RecordCountDownTimer(duration, duration / 360); //录制定时器 | ||
| 116 | - } | ||
| 117 | - | ||
| 118 | - @Override | ||
| 119 | - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
| 120 | - super.onMeasure(widthMeasureSpec, heightMeasureSpec); | ||
| 121 | - setMeasuredDimension(button_size + outside_add_size * 2, button_size + outside_add_size * 2); | ||
| 122 | - } | ||
| 123 | - | ||
| 124 | - @Override | ||
| 125 | - protected void onDraw(Canvas canvas) { | ||
| 126 | - super.onDraw(canvas); | ||
| 127 | - mPaint.setStyle(Paint.Style.FILL); | ||
| 128 | - | ||
| 129 | - mPaint.setColor(outside_color); //外圆(半透明灰色) | ||
| 130 | - canvas.drawCircle(center_X, center_Y, button_outside_radius, mPaint); | ||
| 131 | - | ||
| 132 | - mPaint.setColor(inside_color); //内圆(白色) | ||
| 133 | - canvas.drawCircle(center_X, center_Y, button_inside_radius, mPaint); | ||
| 134 | - | ||
| 135 | - //如果状态为录制状态,则绘制录制进度条 | ||
| 136 | - if (state == STATE_RECORDERING) { | ||
| 137 | - mPaint.setColor(progress_color); | ||
| 138 | - mPaint.setStyle(Paint.Style.STROKE); | ||
| 139 | - mPaint.setStrokeWidth(strokeWidth); | ||
| 140 | - canvas.drawArc(rectF, -90, progress, false, mPaint); | ||
| 141 | - } | ||
| 142 | - } | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - @Override | ||
| 146 | - public boolean onTouchEvent(MotionEvent event) { | ||
| 147 | - switch (event.getAction()) { | ||
| 148 | - case MotionEvent.ACTION_DOWN: | ||
| 149 | - LogUtil.i("state = " + state); | ||
| 150 | - if (event.getPointerCount() > 1 || state != STATE_IDLE) | ||
| 151 | - break; | ||
| 152 | - event_Y = event.getY(); //记录Y值 | ||
| 153 | - state = STATE_PRESS; //修改当前状态为点击按下 | ||
| 154 | - | ||
| 155 | - //判断按钮状态是否为可录制状态 | ||
| 156 | - if ((button_state == BUTTON_STATE_ONLY_RECORDER || button_state == BUTTON_STATE_BOTH)) | ||
| 157 | - postDelayed(longPressRunnable, 200); //同时延长500启动长按后处理的逻辑Runnable | ||
| 158 | - break; | ||
| 159 | - case MotionEvent.ACTION_MOVE: | ||
| 160 | - if (captureLisenter != null | ||
| 161 | - && state == STATE_RECORDERING | ||
| 162 | - && (button_state == BUTTON_STATE_ONLY_RECORDER || button_state == BUTTON_STATE_BOTH)) { | ||
| 163 | - //记录当前Y值与按下时候Y值的差值,调用缩放回调接口 | ||
| 164 | - captureLisenter.recordZoom(event_Y - event.getY()); | ||
| 165 | - } | ||
| 166 | - break; | ||
| 167 | - case MotionEvent.ACTION_UP: | ||
| 168 | - //根据当前按钮的状态进行相应的处理 | ||
| 169 | - handlerUnpressByState(); | ||
| 170 | - break; | ||
| 171 | - } | ||
| 172 | - return true; | ||
| 173 | - } | ||
| 174 | - | ||
| 175 | - //当手指松开按钮时候处理的逻辑 | ||
| 176 | - private void handlerUnpressByState() { | ||
| 177 | - removeCallbacks(longPressRunnable); //移除长按逻辑的Runnable | ||
| 178 | - //根据当前状态处理 | ||
| 179 | - switch (state) { | ||
| 180 | - //当前是点击按下 | ||
| 181 | - case STATE_PRESS: | ||
| 182 | - if (captureLisenter != null && (button_state == BUTTON_STATE_ONLY_CAPTURE || button_state == | ||
| 183 | - BUTTON_STATE_BOTH)) { | ||
| 184 | - startCaptureAnimation(button_inside_radius); | ||
| 185 | - } else { | ||
| 186 | - state = STATE_IDLE; | ||
| 187 | - } | ||
| 188 | - break; | ||
| 189 | - //当前是长按状态 | ||
| 190 | - case STATE_RECORDERING: | ||
| 191 | - timer.cancel(); //停止计时器 | ||
| 192 | - recordEnd(); //录制结束 | ||
| 193 | - break; | ||
| 194 | - } | ||
| 195 | - } | ||
| 196 | - | ||
| 197 | - //录制结束 | ||
| 198 | - private void recordEnd() { | ||
| 199 | - if (captureLisenter != null) { | ||
| 200 | - if (recorded_time < min_duration) | ||
| 201 | - captureLisenter.recordShort(recorded_time);//回调录制时间过短 | ||
| 202 | - else | ||
| 203 | - captureLisenter.recordEnd(recorded_time); //回调录制结束 | ||
| 204 | - } | ||
| 205 | - resetRecordAnim(); //重制按钮状态 | ||
| 206 | - } | ||
| 207 | - | ||
| 208 | - //重制状态 | ||
| 209 | - public void resetRecordAnim() { | ||
| 210 | - state = STATE_BAN; | ||
| 211 | - progress = 0; //重制进度 | ||
| 212 | - invalidate(); | ||
| 213 | - //还原按钮初始状态动画 | ||
| 214 | - startRecordAnimation( | ||
| 215 | - button_outside_radius, | ||
| 216 | - button_radius, | ||
| 217 | - button_inside_radius, | ||
| 218 | - button_radius * 0.75f | ||
| 219 | - ); | ||
| 220 | - } | ||
| 221 | - | ||
| 222 | - //内圆动画 | ||
| 223 | - private void startCaptureAnimation(float inside_start) { | ||
| 224 | - ValueAnimator inside_anim = ValueAnimator.ofFloat(inside_start, inside_start * 0.75f, inside_start); | ||
| 225 | - inside_anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | ||
| 226 | - @Override | ||
| 227 | - public void onAnimationUpdate(ValueAnimator animation) { | ||
| 228 | - button_inside_radius = (float) animation.getAnimatedValue(); | ||
| 229 | - invalidate(); | ||
| 230 | - } | ||
| 231 | - }); | ||
| 232 | - inside_anim.addListener(new AnimatorListenerAdapter() { | ||
| 233 | - @Override | ||
| 234 | - public void onAnimationEnd(Animator animation) { | ||
| 235 | - super.onAnimationEnd(animation); | ||
| 236 | - //回调拍照接口 | ||
| 237 | - captureLisenter.takePictures(); | ||
| 238 | - state = STATE_BAN; | ||
| 239 | - } | ||
| 240 | - }); | ||
| 241 | - inside_anim.setDuration(100); | ||
| 242 | - inside_anim.start(); | ||
| 243 | - } | ||
| 244 | - | ||
| 245 | - //内外圆动画 | ||
| 246 | - private void startRecordAnimation(float outside_start, float outside_end, float inside_start, float inside_end) { | ||
| 247 | - ValueAnimator outside_anim = ValueAnimator.ofFloat(outside_start, outside_end); | ||
| 248 | - ValueAnimator inside_anim = ValueAnimator.ofFloat(inside_start, inside_end); | ||
| 249 | - //外圆动画监听 | ||
| 250 | - outside_anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | ||
| 251 | - @Override | ||
| 252 | - public void onAnimationUpdate(ValueAnimator animation) { | ||
| 253 | - button_outside_radius = (float) animation.getAnimatedValue(); | ||
| 254 | - invalidate(); | ||
| 255 | - } | ||
| 256 | - }); | ||
| 257 | - //内圆动画监听 | ||
| 258 | - inside_anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | ||
| 259 | - @Override | ||
| 260 | - public void onAnimationUpdate(ValueAnimator animation) { | ||
| 261 | - button_inside_radius = (float) animation.getAnimatedValue(); | ||
| 262 | - invalidate(); | ||
| 263 | - } | ||
| 264 | - }); | ||
| 265 | - AnimatorSet set = new AnimatorSet(); | ||
| 266 | - //当动画结束后启动录像Runnable并且回调录像开始接口 | ||
| 267 | - set.addListener(new AnimatorListenerAdapter() { | ||
| 268 | - @Override | ||
| 269 | - public void onAnimationEnd(Animator animation) { | ||
| 270 | - super.onAnimationEnd(animation); | ||
| 271 | - //设置为录制状态 | ||
| 272 | - if (state == STATE_LONG_PRESS) { | ||
| 273 | - if (captureLisenter != null) | ||
| 274 | - captureLisenter.recordStart(); | ||
| 275 | - state = STATE_RECORDERING; | ||
| 276 | - timer.start(); | ||
| 277 | - } | ||
| 278 | - } | ||
| 279 | - }); | ||
| 280 | - set.playTogether(outside_anim, inside_anim); | ||
| 281 | - set.setDuration(0); | ||
| 282 | - set.start(); | ||
| 283 | - } | ||
| 284 | - | ||
| 285 | - | ||
| 286 | - //更新进度条 | ||
| 287 | - private void updateProgress(long millisUntilFinished) { | ||
| 288 | - recorded_time = (int) (duration - millisUntilFinished); | ||
| 289 | - progress = 360f - millisUntilFinished / (float) duration * 360f; | ||
| 290 | - invalidate(); | ||
| 291 | - } | ||
| 292 | - | ||
| 293 | - //录制视频计时器 | ||
| 294 | - private class RecordCountDownTimer extends CountDownTimer { | ||
| 295 | - RecordCountDownTimer(long millisInFuture, long countDownInterval) { | ||
| 296 | - super(millisInFuture, countDownInterval); | ||
| 297 | - } | ||
| 298 | - | ||
| 299 | - @Override | ||
| 300 | - public void onTick(long millisUntilFinished) { | ||
| 301 | - updateProgress(millisUntilFinished); | ||
| 302 | - } | ||
| 303 | - | ||
| 304 | - @Override | ||
| 305 | - public void onFinish() { | ||
| 306 | - updateProgress(0); | ||
| 307 | - recordEnd(); | ||
| 308 | - } | ||
| 309 | - } | ||
| 310 | - | ||
| 311 | - //长按线程 | ||
| 312 | - private class LongPressRunnable implements Runnable { | ||
| 313 | - @Override | ||
| 314 | - public void run() { | ||
| 315 | - state = STATE_LONG_PRESS; //如果按下后经过500毫秒则会修改当前状态为长按状态 | ||
| 316 | - //没有录制权限 | ||
| 317 | - if (CheckPermission.getRecordState() != CheckPermission.STATE_SUCCESS) { | ||
| 318 | - state = STATE_IDLE; | ||
| 319 | - if (captureLisenter != null) { | ||
| 320 | - captureLisenter.recordError(); | ||
| 321 | - return; | ||
| 322 | - } | ||
| 323 | - } | ||
| 324 | - //启动按钮动画,外圆变大,内圆缩小 | ||
| 325 | - startRecordAnimation( | ||
| 326 | - button_outside_radius, | ||
| 327 | - button_outside_radius + outside_add_size, | ||
| 328 | - button_inside_radius, | ||
| 329 | - button_inside_radius - inside_reduce_size | ||
| 330 | - ); | ||
| 331 | - } | ||
| 332 | - } | ||
| 333 | - | ||
| 334 | - /************************************************** | ||
| 335 | - * 对外提供的API * | ||
| 336 | - **************************************************/ | ||
| 337 | - | ||
| 338 | - //设置最长录制时间 | ||
| 339 | - public void setDuration(int duration) { | ||
| 340 | - this.duration = duration; | ||
| 341 | - timer = new RecordCountDownTimer(duration, duration / 360); //录制定时器 | ||
| 342 | - } | ||
| 343 | - | ||
| 344 | - //设置最短录制时间 | ||
| 345 | - public void setMinDuration(int duration) { | ||
| 346 | - this.min_duration = duration; | ||
| 347 | - } | ||
| 348 | - | ||
| 349 | - //设置回调接口 | ||
| 350 | - public void setCaptureLisenter(CaptureListener captureLisenter) { | ||
| 351 | - this.captureLisenter = captureLisenter; | ||
| 352 | - } | ||
| 353 | - | ||
| 354 | - //设置按钮功能(拍照和录像) | ||
| 355 | - public void setButtonFeatures(int state) { | ||
| 356 | - this.button_state = state; | ||
| 357 | - } | ||
| 358 | - | ||
| 359 | - //是否空闲状态 | ||
| 360 | - public boolean isIdle() { | ||
| 361 | - return state == STATE_IDLE ? true : false; | ||
| 362 | - } | ||
| 363 | - | ||
| 364 | - //设置状态 | ||
| 365 | - public void resetState() { | ||
| 366 | - state = STATE_IDLE; | ||
| 367 | - } | ||
| 368 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/CaptureLayout.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary; | ||
| 2 | - | ||
| 3 | -import android.animation.Animator; | ||
| 4 | -import android.animation.AnimatorListenerAdapter; | ||
| 5 | -import android.animation.AnimatorSet; | ||
| 6 | -import android.animation.ObjectAnimator; | ||
| 7 | -import android.content.Context; | ||
| 8 | -import android.content.res.Configuration; | ||
| 9 | -import android.content.res.Resources; | ||
| 10 | -import android.util.AttributeSet; | ||
| 11 | -import android.view.Gravity; | ||
| 12 | -import android.view.View; | ||
| 13 | -import android.widget.FrameLayout; | ||
| 14 | -import android.widget.ImageView; | ||
| 15 | -import android.widget.TextView; | ||
| 16 | - | ||
| 17 | -import com.cjt2325.cameralibrary.listener.CaptureListener; | ||
| 18 | -import com.cjt2325.cameralibrary.listener.ClickListener; | ||
| 19 | -import com.cjt2325.cameralibrary.listener.ReturnListener; | ||
| 20 | -import com.cjt2325.cameralibrary.listener.TypeListener; | ||
| 21 | - | ||
| 22 | -import static com.cjt2325.cameralibrary.JCameraView.BUTTON_STATE_BOTH; | ||
| 23 | -import static com.cjt2325.cameralibrary.JCameraView.BUTTON_STATE_ONLY_CAPTURE; | ||
| 24 | -import static com.cjt2325.cameralibrary.JCameraView.BUTTON_STATE_ONLY_RECORDER; | ||
| 25 | - | ||
| 26 | -/** | ||
| 27 | - * ===================================== | ||
| 28 | - * 作 者: 陈嘉桐 445263848@qq.com | ||
| 29 | - * 版 本:1.0.4 | ||
| 30 | - * 创建日期:2017/4/26 | ||
| 31 | - * 描 述:集成各个控件的布局 | ||
| 32 | - * ===================================== | ||
| 33 | - */ | ||
| 34 | - | ||
| 35 | -public class CaptureLayout extends FrameLayout { | ||
| 36 | - private boolean isErrorState = false; | ||
| 37 | - private CaptureListener captureLisenter; //拍照按钮监听 | ||
| 38 | - private TypeListener typeLisenter; //拍照或录制后接结果按钮监听 | ||
| 39 | - private ReturnListener returnListener; //退出按钮监听 | ||
| 40 | - private ClickListener leftClickListener; //左边按钮监听 | ||
| 41 | - private ClickListener rightClickListener; //右边按钮监听 | ||
| 42 | - | ||
| 43 | - public void setTypeLisenter(TypeListener typeLisenter) { | ||
| 44 | - this.typeLisenter = typeLisenter; | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - public void setCaptureLisenter(CaptureListener captureLisenter) { | ||
| 48 | - this.captureLisenter = captureLisenter; | ||
| 49 | - } | ||
| 50 | - | ||
| 51 | - public void setReturnLisenter(ReturnListener returnListener) { | ||
| 52 | - this.returnListener = returnListener; | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | - private CaptureButton btn_capture; //拍照按钮 | ||
| 56 | - private TypeButton btn_confirm; //确认按钮 | ||
| 57 | - private TypeButton btn_cancel; //取消按钮 | ||
| 58 | - private ReturnButton btn_return; //返回按钮 | ||
| 59 | - private ImageView iv_custom_left; //左边自定义按钮 | ||
| 60 | - private ImageView iv_custom_right; //右边自定义按钮 | ||
| 61 | - private TextView txt_tip; //提示文本 | ||
| 62 | - | ||
| 63 | - private int layout_width; | ||
| 64 | - private int layout_height; | ||
| 65 | - private int button_size; | ||
| 66 | - private int iconLeft = 0; | ||
| 67 | - private int iconRight = 0; | ||
| 68 | - | ||
| 69 | - private boolean isFirst = true; | ||
| 70 | - | ||
| 71 | - public CaptureLayout(Context context) { | ||
| 72 | - this(context, null); | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | - public CaptureLayout(Context context, AttributeSet attrs) { | ||
| 76 | - this(context, attrs, 0); | ||
| 77 | - } | ||
| 78 | - | ||
| 79 | - public CaptureLayout(Context context, AttributeSet attrs, int defStyleAttr) { | ||
| 80 | - super(context, attrs, defStyleAttr); | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - // WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | ||
| 84 | - // DisplayMetrics outMetrics = new DisplayMetrics(); | ||
| 85 | - // manager.getDefaultDisplay().getMetrics(outMetrics); | ||
| 86 | - if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { | ||
| 87 | - layout_width = dip2px(250); | ||
| 88 | - // layout_width = outMetrics.widthPixels; | ||
| 89 | - } else { | ||
| 90 | - layout_width = dip2px(250); | ||
| 91 | - } | ||
| 92 | - // button_size = (int) (layout_width / 6f); | ||
| 93 | - // layout_height = button_size + (button_size / 5) * 2 + 100; | ||
| 94 | - layout_height = dip2px(100); | ||
| 95 | - button_size = dip2px(50); | ||
| 96 | - initView(); | ||
| 97 | - initEvent(); | ||
| 98 | - } | ||
| 99 | - | ||
| 100 | - @Override | ||
| 101 | - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
| 102 | - super.onMeasure(widthMeasureSpec, heightMeasureSpec); | ||
| 103 | - setMeasuredDimension(layout_width, layout_height); | ||
| 104 | - } | ||
| 105 | - | ||
| 106 | - public void initEvent() { | ||
| 107 | - //默认Typebutton为隐藏 | ||
| 108 | - iv_custom_right.setVisibility(GONE); | ||
| 109 | - btn_cancel.setVisibility(GONE); | ||
| 110 | - btn_confirm.setVisibility(GONE); | ||
| 111 | - } | ||
| 112 | - | ||
| 113 | - public void startTypeBtnAnimator() { | ||
| 114 | - if (checkErrorState()) return; | ||
| 115 | - //拍照录制结果后的动画 | ||
| 116 | - if (this.iconLeft != 0) | ||
| 117 | - iv_custom_left.setVisibility(GONE); | ||
| 118 | - else | ||
| 119 | - btn_return.setVisibility(GONE); | ||
| 120 | - if (this.iconRight != 0) | ||
| 121 | - iv_custom_right.setVisibility(GONE); | ||
| 122 | - btn_capture.setVisibility(GONE); | ||
| 123 | - btn_cancel.setVisibility(VISIBLE); | ||
| 124 | - btn_confirm.setVisibility(VISIBLE); | ||
| 125 | - btn_cancel.setClickable(false); | ||
| 126 | - btn_confirm.setClickable(false); | ||
| 127 | - ObjectAnimator animator_cancel = ObjectAnimator.ofFloat(btn_cancel, "translationX", layout_width / 4, 0); | ||
| 128 | - ObjectAnimator animator_confirm = ObjectAnimator.ofFloat(btn_confirm, "translationX", -layout_width / 4, 0); | ||
| 129 | - | ||
| 130 | - AnimatorSet set = new AnimatorSet(); | ||
| 131 | - set.playTogether(animator_cancel, animator_confirm); | ||
| 132 | - set.addListener(new AnimatorListenerAdapter() { | ||
| 133 | - @Override | ||
| 134 | - public void onAnimationEnd(Animator animation) { | ||
| 135 | - super.onAnimationEnd(animation); | ||
| 136 | - btn_cancel.setClickable(true); | ||
| 137 | - btn_confirm.setClickable(true); | ||
| 138 | - } | ||
| 139 | - }); | ||
| 140 | - set.setDuration(200); | ||
| 141 | - set.start(); | ||
| 142 | - } | ||
| 143 | - private void initView() { | ||
| 144 | - setWillNotDraw(false); | ||
| 145 | - //拍照按钮 | ||
| 146 | - btn_capture = new CaptureButton(getContext(), button_size); | ||
| 147 | - LayoutParams btn_capture_param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); | ||
| 148 | - btn_capture_param.gravity = Gravity.CENTER; | ||
| 149 | - // btn_capture_param.setMargins(0,0,0,dip2px(-4)); | ||
| 150 | - btn_capture.setLayoutParams(btn_capture_param); | ||
| 151 | - btn_capture.setCaptureLisenter(new CaptureListener() { | ||
| 152 | - @Override | ||
| 153 | - public void takePictures() { | ||
| 154 | - btn_return.setClickable(false); | ||
| 155 | - if (captureLisenter != null) { | ||
| 156 | - captureLisenter.takePictures(); | ||
| 157 | - } | ||
| 158 | - } | ||
| 159 | - | ||
| 160 | - @Override | ||
| 161 | - public void recordShort(long time) { | ||
| 162 | - btn_return.setClickable(true); | ||
| 163 | - if (captureLisenter != null) { | ||
| 164 | - captureLisenter.recordShort(time); | ||
| 165 | - } | ||
| 166 | - startAlphaAnimation(); | ||
| 167 | - } | ||
| 168 | - | ||
| 169 | - @Override | ||
| 170 | - public void recordStart() { | ||
| 171 | - btn_return.setClickable(false); | ||
| 172 | - if (captureLisenter != null) { | ||
| 173 | - captureLisenter.recordStart(); | ||
| 174 | - } | ||
| 175 | - startAlphaAnimation(); | ||
| 176 | - } | ||
| 177 | - | ||
| 178 | - @Override | ||
| 179 | - public void recordEnd(long time) { | ||
| 180 | - if (captureLisenter != null) { | ||
| 181 | - captureLisenter.recordEnd(time); | ||
| 182 | - } | ||
| 183 | - startAlphaAnimation(); | ||
| 184 | - startTypeBtnAnimator(); | ||
| 185 | - } | ||
| 186 | - | ||
| 187 | - @Override | ||
| 188 | - public void recordZoom(float zoom) { | ||
| 189 | - if (captureLisenter != null) { | ||
| 190 | - captureLisenter.recordZoom(zoom); | ||
| 191 | - } | ||
| 192 | - } | ||
| 193 | - | ||
| 194 | - @Override | ||
| 195 | - public void recordError() { | ||
| 196 | - if (captureLisenter != null) { | ||
| 197 | - captureLisenter.recordError(); | ||
| 198 | - } | ||
| 199 | - } | ||
| 200 | - }); | ||
| 201 | - | ||
| 202 | - //取消按钮 | ||
| 203 | - btn_cancel = new TypeButton(getContext(), TypeButton.TYPE_CANCEL, button_size); | ||
| 204 | - final LayoutParams btn_cancel_param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); | ||
| 205 | - btn_cancel_param.gravity = Gravity.CENTER_VERTICAL; | ||
| 206 | - btn_cancel_param.setMargins(dip2px(200), 0, 0, 0); | ||
| 207 | - btn_cancel.setLayoutParams(btn_cancel_param); | ||
| 208 | - btn_cancel.setOnClickListener(new OnClickListener() { | ||
| 209 | - @Override | ||
| 210 | - public void onClick(View view) { | ||
| 211 | - if (typeLisenter != null) { | ||
| 212 | - typeLisenter.cancel(); | ||
| 213 | - } | ||
| 214 | - startAlphaAnimation(); | ||
| 215 | -// resetCaptureLayout(); | ||
| 216 | - } | ||
| 217 | - }); | ||
| 218 | - | ||
| 219 | - //确认按钮 | ||
| 220 | - btn_confirm = new TypeButton(getContext(), TypeButton.TYPE_CONFIRM, button_size); | ||
| 221 | - LayoutParams btn_confirm_param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); | ||
| 222 | - btn_confirm_param.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT; | ||
| 223 | - btn_confirm_param.setMargins(0, 0, dip2px(200), 0); | ||
| 224 | - btn_confirm.setLayoutParams(btn_confirm_param); | ||
| 225 | - btn_confirm.setOnClickListener(new OnClickListener() { | ||
| 226 | - @Override | ||
| 227 | - public void onClick(View view) { | ||
| 228 | - if (typeLisenter != null) { | ||
| 229 | - typeLisenter.confirm(); | ||
| 230 | - } | ||
| 231 | - startAlphaAnimation(); | ||
| 232 | -// resetCaptureLayout(); | ||
| 233 | - } | ||
| 234 | - }); | ||
| 235 | - | ||
| 236 | - //返回按钮 | ||
| 237 | - btn_return = new ReturnButton(getContext(), (int) (button_size / 2.5f)); | ||
| 238 | - btn_return.setPadding(5,5,5,5); | ||
| 239 | - LayoutParams btn_return_param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); | ||
| 240 | - btn_return_param.gravity = Gravity.CENTER_VERTICAL; | ||
| 241 | - btn_cancel_param.height = 100; | ||
| 242 | - btn_cancel_param.width = 100; | ||
| 243 | - btn_return_param.setMargins(layout_width / 6, 0, 0, dip2px(-4)); | ||
| 244 | - btn_return.setLayoutParams(btn_return_param); | ||
| 245 | - btn_return.setOnClickListener(new OnClickListener() { | ||
| 246 | - @Override | ||
| 247 | - public void onClick(View v) { | ||
| 248 | - if (leftClickListener != null) { | ||
| 249 | - leftClickListener.onClick(); | ||
| 250 | - } | ||
| 251 | - } | ||
| 252 | - }); | ||
| 253 | - //左边自定义按钮 | ||
| 254 | - iv_custom_left = new ImageView(getContext()); | ||
| 255 | - LayoutParams iv_custom_param_left = new LayoutParams((int) (button_size / 2.5f), (int) (button_size / 2.5f)); | ||
| 256 | - iv_custom_param_left.gravity = Gravity.CENTER_VERTICAL; | ||
| 257 | - iv_custom_param_left.setMargins(layout_width / 6, 0, 0, 0); | ||
| 258 | - iv_custom_left.setLayoutParams(iv_custom_param_left); | ||
| 259 | - iv_custom_left.setOnClickListener(new OnClickListener() { | ||
| 260 | - @Override | ||
| 261 | - public void onClick(View v) { | ||
| 262 | - if (leftClickListener != null) { | ||
| 263 | - leftClickListener.onClick(); | ||
| 264 | - } | ||
| 265 | - } | ||
| 266 | - }); | ||
| 267 | - | ||
| 268 | - //右边自定义按钮 | ||
| 269 | - iv_custom_right = new ImageView(getContext()); | ||
| 270 | - LayoutParams iv_custom_param_right = new LayoutParams((int) (button_size / 2.5f), (int) (button_size / 2.5f)); | ||
| 271 | - iv_custom_param_right.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT; | ||
| 272 | - iv_custom_param_right.setMargins(0, 0, layout_width / 6, 0); | ||
| 273 | - iv_custom_right.setLayoutParams(iv_custom_param_right); | ||
| 274 | - iv_custom_right.setOnClickListener(new OnClickListener() { | ||
| 275 | - @Override | ||
| 276 | - public void onClick(View v) { | ||
| 277 | - if (rightClickListener != null) { | ||
| 278 | - rightClickListener.onClick(); | ||
| 279 | - } | ||
| 280 | - } | ||
| 281 | - }); | ||
| 282 | - | ||
| 283 | - txt_tip = new TextView(getContext()); | ||
| 284 | - LayoutParams txt_param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); | ||
| 285 | - txt_param.gravity = Gravity.CENTER_HORIZONTAL; | ||
| 286 | - txt_param.setMargins(0, 0, 0, 0); | ||
| 287 | - txt_tip.setPadding(0, 0, 0, 50); | ||
| 288 | - txt_tip.setTextColor(0xFFFFFFFF); | ||
| 289 | - txt_tip.setGravity(Gravity.CENTER); | ||
| 290 | - txt_tip.setLayoutParams(txt_param); | ||
| 291 | - | ||
| 292 | - this.addView(btn_capture); | ||
| 293 | - this.addView(btn_cancel); | ||
| 294 | - this.addView(btn_confirm); | ||
| 295 | - this.addView(btn_return); | ||
| 296 | - this.addView(iv_custom_left); | ||
| 297 | - this.addView(iv_custom_right); | ||
| 298 | - this.addView(txt_tip); | ||
| 299 | - } | ||
| 300 | - | ||
| 301 | - public static int dip2px(float dipValue) { | ||
| 302 | - float scale = Resources.getSystem().getDisplayMetrics().density; | ||
| 303 | - return (int) (dipValue * scale + 0.5F); | ||
| 304 | - } | ||
| 305 | - | ||
| 306 | - | ||
| 307 | - /************************************************** | ||
| 308 | - * 对外提供的API * | ||
| 309 | - **************************************************/ | ||
| 310 | - public void resetCaptureLayout() { | ||
| 311 | - btn_capture.resetRecordAnim(); | ||
| 312 | - btn_capture.resetState(); | ||
| 313 | - btn_cancel.setVisibility(GONE); | ||
| 314 | - btn_confirm.setVisibility(GONE); | ||
| 315 | - btn_capture.setVisibility(VISIBLE); | ||
| 316 | - if (this.iconLeft != 0) | ||
| 317 | - iv_custom_left.setVisibility(VISIBLE); | ||
| 318 | - else { | ||
| 319 | - btn_return.setClickable(true); | ||
| 320 | - btn_return.setVisibility(VISIBLE); | ||
| 321 | - } | ||
| 322 | - if (this.iconRight != 0) | ||
| 323 | - iv_custom_right.setVisibility(VISIBLE); | ||
| 324 | - } | ||
| 325 | - | ||
| 326 | - private boolean checkErrorState() { | ||
| 327 | - if (isErrorState) { | ||
| 328 | - isErrorState = false; | ||
| 329 | - return true; | ||
| 330 | - } | ||
| 331 | - return false; | ||
| 332 | - } | ||
| 333 | - | ||
| 334 | - public void onError() { | ||
| 335 | - onError(false); | ||
| 336 | - } | ||
| 337 | - | ||
| 338 | - public void onError(boolean state) { | ||
| 339 | - isErrorState = state; | ||
| 340 | - btn_return.setClickable(true); | ||
| 341 | - } | ||
| 342 | - | ||
| 343 | - public void startAlphaAnimation() { | ||
| 344 | - if (isFirst) { | ||
| 345 | - ObjectAnimator animator_txt_tip = ObjectAnimator.ofFloat(txt_tip, "alpha", 1f, 0f); | ||
| 346 | - animator_txt_tip.setDuration(500); | ||
| 347 | - animator_txt_tip.start(); | ||
| 348 | - isFirst = false; | ||
| 349 | - } | ||
| 350 | - } | ||
| 351 | - | ||
| 352 | - public void setTextWithAnimation(String tip) { | ||
| 353 | - txt_tip.setText(tip); | ||
| 354 | - ObjectAnimator animator_txt_tip = ObjectAnimator.ofFloat(txt_tip, "alpha", 0f, 1f, 1f, 0f); | ||
| 355 | - animator_txt_tip.setDuration(2500); | ||
| 356 | - animator_txt_tip.start(); | ||
| 357 | - } | ||
| 358 | - | ||
| 359 | - public void setDuration(int duration) { | ||
| 360 | - btn_capture.setDuration(duration); | ||
| 361 | - } | ||
| 362 | - | ||
| 363 | - public void setButtonFeatures(int state) { | ||
| 364 | - if (state == BUTTON_STATE_BOTH) { | ||
| 365 | - txt_tip.setText("轻触拍照,长按摄像"); | ||
| 366 | - } else if (state == BUTTON_STATE_ONLY_CAPTURE) { | ||
| 367 | - txt_tip.setText("轻触拍照"); | ||
| 368 | - } else if (state == BUTTON_STATE_ONLY_RECORDER) { | ||
| 369 | - txt_tip.setText("长按摄像"); | ||
| 370 | - } | ||
| 371 | - btn_capture.setButtonFeatures(state); | ||
| 372 | - } | ||
| 373 | - | ||
| 374 | - public void setTip(String tip) { | ||
| 375 | - txt_tip.setText(tip); | ||
| 376 | - } | ||
| 377 | - | ||
| 378 | - public void showTip() { | ||
| 379 | - txt_tip.setVisibility(VISIBLE); | ||
| 380 | - } | ||
| 381 | - | ||
| 382 | - public void setIconSrc(int iconLeft, int iconRight) { | ||
| 383 | - this.iconLeft = iconLeft; | ||
| 384 | - this.iconRight = iconRight; | ||
| 385 | - if (this.iconLeft != 0) { | ||
| 386 | - iv_custom_left.setImageResource(iconLeft); | ||
| 387 | - iv_custom_left.setVisibility(VISIBLE); | ||
| 388 | - btn_return.setVisibility(GONE); | ||
| 389 | - } else { | ||
| 390 | - iv_custom_left.setVisibility(GONE); | ||
| 391 | - btn_return.setClickable(true); | ||
| 392 | - btn_return.setVisibility(VISIBLE); | ||
| 393 | - } | ||
| 394 | - if (this.iconRight != 0) { | ||
| 395 | - iv_custom_right.setImageResource(iconRight); | ||
| 396 | - iv_custom_right.setVisibility(VISIBLE); | ||
| 397 | - } else { | ||
| 398 | - iv_custom_right.setVisibility(GONE); | ||
| 399 | - } | ||
| 400 | - } | ||
| 401 | - | ||
| 402 | - public void setLeftClickListener(ClickListener leftClickListener) { | ||
| 403 | - this.leftClickListener = leftClickListener; | ||
| 404 | - } | ||
| 405 | - | ||
| 406 | - public void setRightClickListener(ClickListener rightClickListener) { | ||
| 407 | - this.rightClickListener = rightClickListener; | ||
| 408 | - } | ||
| 409 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/FoucsView.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.graphics.Canvas; | ||
| 5 | -import android.graphics.Paint; | ||
| 6 | -import android.util.AttributeSet; | ||
| 7 | -import android.view.View; | ||
| 8 | - | ||
| 9 | -import androidx.annotation.Nullable; | ||
| 10 | - | ||
| 11 | -import com.cjt2325.cameralibrary.util.ScreenUtils; | ||
| 12 | - | ||
| 13 | -/** | ||
| 14 | - * ===================================== | ||
| 15 | - * 作 者: 陈嘉桐 | ||
| 16 | - * 版 本:1.1.4 | ||
| 17 | - * 创建日期:2017/4/26 | ||
| 18 | - * 描 述:对焦框 | ||
| 19 | - * ===================================== | ||
| 20 | - */ | ||
| 21 | -public class FoucsView extends View { | ||
| 22 | - private int size; | ||
| 23 | - private int center_x; | ||
| 24 | - private int center_y; | ||
| 25 | - private int length; | ||
| 26 | - private Paint mPaint; | ||
| 27 | - | ||
| 28 | - public FoucsView(Context context) { | ||
| 29 | - this(context, null); | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - public FoucsView(Context context, @Nullable AttributeSet attrs) { | ||
| 33 | - this(context, attrs, 0); | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - public FoucsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
| 37 | - super(context, attrs, defStyleAttr); | ||
| 38 | - this.size = ScreenUtils.getScreenWidth(context) / 4; | ||
| 39 | - mPaint = new Paint(); | ||
| 40 | - mPaint.setAntiAlias(true); | ||
| 41 | - mPaint.setDither(true); | ||
| 42 | - mPaint.setColor(0xEE16AE16); | ||
| 43 | - mPaint.setStrokeWidth(4); | ||
| 44 | - mPaint.setStyle(Paint.Style.STROKE); | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - @Override | ||
| 48 | - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
| 49 | - super.onMeasure(widthMeasureSpec, heightMeasureSpec); | ||
| 50 | - center_x = (int) (size / 2.0); | ||
| 51 | - center_y = (int) (size / 2.0); | ||
| 52 | - length = (int) (size / 2.0) - 2; | ||
| 53 | - setMeasuredDimension(size, size); | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - @Override | ||
| 57 | - protected void onDraw(Canvas canvas) { | ||
| 58 | - super.onDraw(canvas); | ||
| 59 | - canvas.drawRect(center_x - length, center_y - length, center_x + length, center_y + length, mPaint); | ||
| 60 | - canvas.drawLine(2, getHeight() / 2, size / 10, getHeight() / 2, mPaint); | ||
| 61 | - canvas.drawLine(getWidth() - 2, getHeight() / 2, getWidth() - size / 10, getHeight() / 2, mPaint); | ||
| 62 | - canvas.drawLine(getWidth() / 2, 2, getWidth() / 2, size / 10, mPaint); | ||
| 63 | - canvas.drawLine(getWidth() / 2, getHeight() - 2, getWidth() / 2, getHeight() - size / 10, mPaint); | ||
| 64 | - } | ||
| 65 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/JCameraView.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary; | ||
| 2 | - | ||
| 3 | -import android.animation.AnimatorSet; | ||
| 4 | -import android.animation.ObjectAnimator; | ||
| 5 | -import android.content.Context; | ||
| 6 | -import android.content.res.TypedArray; | ||
| 7 | -import android.graphics.Bitmap; | ||
| 8 | -import android.hardware.Camera; | ||
| 9 | -import android.media.AudioManager; | ||
| 10 | -import android.media.MediaPlayer; | ||
| 11 | -import android.os.Build; | ||
| 12 | -import android.util.AttributeSet; | ||
| 13 | -import android.util.Log; | ||
| 14 | -import android.util.TypedValue; | ||
| 15 | -import android.view.Gravity; | ||
| 16 | -import android.view.LayoutInflater; | ||
| 17 | -import android.view.MotionEvent; | ||
| 18 | -import android.view.SurfaceHolder; | ||
| 19 | -import android.view.View; | ||
| 20 | -import android.view.ViewGroup; | ||
| 21 | -import android.view.WindowManager; | ||
| 22 | -import android.widget.FrameLayout; | ||
| 23 | -import android.widget.ImageView; | ||
| 24 | -import android.widget.VideoView; | ||
| 25 | - | ||
| 26 | -import androidx.annotation.RequiresApi; | ||
| 27 | - | ||
| 28 | -import com.cjt2325.cameralibrary.listener.CaptureListener; | ||
| 29 | -import com.cjt2325.cameralibrary.listener.ClickListener; | ||
| 30 | -import com.cjt2325.cameralibrary.listener.ErrorListener; | ||
| 31 | -import com.cjt2325.cameralibrary.listener.JCameraListener; | ||
| 32 | -import com.cjt2325.cameralibrary.listener.TypeListener; | ||
| 33 | -import com.cjt2325.cameralibrary.state.CameraMachine; | ||
| 34 | -import com.cjt2325.cameralibrary.util.FileUtil; | ||
| 35 | -import com.cjt2325.cameralibrary.util.LogUtil; | ||
| 36 | -import com.cjt2325.cameralibrary.util.ScreenUtils; | ||
| 37 | -import com.cjt2325.cameralibrary.view.CameraView; | ||
| 38 | -import com.cnlive.libs.base.util.AlertUtil; | ||
| 39 | -import com.cnlive.libs.base.util.ScreenUtil; | ||
| 40 | -import com.cnlive.strike.base.R; | ||
| 41 | - | ||
| 42 | - | ||
| 43 | -/** | ||
| 44 | - * ===================================== | ||
| 45 | - * 作 者: 陈嘉桐 | ||
| 46 | - * 版 本:1.0.4 | ||
| 47 | - * 创建日期:2017/4/25 | ||
| 48 | - * 描 述: | ||
| 49 | - * ===================================== | ||
| 50 | - */ | ||
| 51 | -public class JCameraView extends FrameLayout implements CameraInterface.CameraOpenOverCallback, SurfaceHolder | ||
| 52 | - .Callback, CameraView { | ||
| 53 | -// private static final String TAG = "JCameraView"; | ||
| 54 | - | ||
| 55 | - //Camera状态机 | ||
| 56 | - private CameraMachine machine; | ||
| 57 | - | ||
| 58 | - //闪关灯状态 | ||
| 59 | - // private static final int TYPE_FLASH_AUTO = 0x021; | ||
| 60 | - private static final int TYPE_FLASH_ON = 0x022; | ||
| 61 | - private static final int TYPE_FLASH_OFF = 0x023; | ||
| 62 | - private int type_flash = TYPE_FLASH_OFF; | ||
| 63 | - | ||
| 64 | - //拍照浏览时候的类型 | ||
| 65 | - public static final int TYPE_PICTURE = 0x001; | ||
| 66 | - public static final int TYPE_VIDEO = 0x002; | ||
| 67 | - public static final int TYPE_SHORT = 0x003; | ||
| 68 | - public static final int TYPE_DEFAULT = 0x004; | ||
| 69 | - | ||
| 70 | - //录制视频比特率 | ||
| 71 | - public static final int MEDIA_QUALITY_HIGH = 20 * 100000; | ||
| 72 | - public static final int MEDIA_QUALITY_MIDDLE = 16 * 100000; | ||
| 73 | - public static final int MEDIA_QUALITY_LOW = 12 * 100000; | ||
| 74 | - public static final int MEDIA_QUALITY_POOR = 8 * 100000; | ||
| 75 | - public static final int MEDIA_QUALITY_FUNNY = 4 * 100000; | ||
| 76 | - public static final int MEDIA_QUALITY_DESPAIR = 2 * 100000; | ||
| 77 | - public static final int MEDIA_QUALITY_SORRY = 1 * 80000; | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - public static final int BUTTON_STATE_ONLY_CAPTURE = 0x101; //只能拍照 | ||
| 81 | - public static final int BUTTON_STATE_ONLY_RECORDER = 0x102; //只能录像 | ||
| 82 | - public static final int BUTTON_STATE_BOTH = 0x103; //两者都可以 | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - //回调监听 | ||
| 86 | - private JCameraListener jCameraLisenter; | ||
| 87 | - private ClickListener leftClickListener; | ||
| 88 | - private ClickListener rightClickListener; | ||
| 89 | - | ||
| 90 | - private VideoView mVideoView; | ||
| 91 | - private ImageView mPhoto; | ||
| 92 | - private ImageView mSwitchCamera; | ||
| 93 | - private ImageView mFlashLamp; | ||
| 94 | - private CaptureLayout mCaptureLayout; | ||
| 95 | - private FoucsView mFoucsView; | ||
| 96 | - private MediaPlayer mMediaPlayer; | ||
| 97 | - | ||
| 98 | - private int layout_width; | ||
| 99 | - private float screenProp = 0f; | ||
| 100 | - | ||
| 101 | - private Bitmap captureBitmap; //捕获的图片 | ||
| 102 | - private String videoUrl; //视频URL | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - //切换摄像头按钮的参数 | ||
| 106 | - private int iconSize = 0; //图标大小 | ||
| 107 | - private int iconMargin = 0; //右上边距 | ||
| 108 | - private int iconSrc = 0; //图标资源 | ||
| 109 | - private int iconLeft = 0; //左图标 | ||
| 110 | - private int iconRight = 0; //右图标 | ||
| 111 | - private int duration = 0; //录制时间 | ||
| 112 | - | ||
| 113 | - //缩放梯度 | ||
| 114 | - private int zoomGradient = 0; | ||
| 115 | - | ||
| 116 | - private boolean firstTouch = true; | ||
| 117 | - private float firstTouchLength = 0; | ||
| 118 | - private boolean recordEnd = false; | ||
| 119 | - private long lastClick; | ||
| 120 | - | ||
| 121 | - public JCameraView(Context context) { | ||
| 122 | - this(context, null); | ||
| 123 | - } | ||
| 124 | - | ||
| 125 | - public JCameraView(Context context, AttributeSet attrs) { | ||
| 126 | - this(context, attrs, 0); | ||
| 127 | - } | ||
| 128 | - | ||
| 129 | - public JCameraView(Context context, AttributeSet attrs, int defStyleAttr) { | ||
| 130 | - super(context, attrs, defStyleAttr); | ||
| 131 | - //get AttributeSet | ||
| 132 | - TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.JCameraView, defStyleAttr, 0); | ||
| 133 | - iconSize = a.getDimensionPixelSize(R.styleable.JCameraView_iconSize, (int) TypedValue.applyDimension( | ||
| 134 | - TypedValue.COMPLEX_UNIT_SP, 35, getResources().getDisplayMetrics())); | ||
| 135 | - iconMargin = a.getDimensionPixelSize(R.styleable.JCameraView_iconMargin, (int) TypedValue.applyDimension( | ||
| 136 | - TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics())); | ||
| 137 | - // iconSrc = a.getResourceId(R.styleable.JCameraView_iconSrc, R.drawable.fb_xiangji); | ||
| 138 | - iconLeft = a.getResourceId(R.styleable.JCameraView_iconLeft, 0); | ||
| 139 | - iconRight = a.getResourceId(R.styleable.JCameraView_iconRight, 0); | ||
| 140 | - duration = a.getInteger(R.styleable.JCameraView_duration_max, 10 * 1000); //没设置默认为10s | ||
| 141 | - a.recycle(); | ||
| 142 | - initData(); | ||
| 143 | - initView(); | ||
| 144 | - } | ||
| 145 | - | ||
| 146 | - private void initData() { | ||
| 147 | - layout_width = ScreenUtils.getScreenWidth(getContext()); | ||
| 148 | - //缩放梯度 | ||
| 149 | - zoomGradient = (int) (layout_width / 16f); | ||
| 150 | - LogUtil.i("zoom = " + zoomGradient); | ||
| 151 | - machine = new CameraMachine(getContext(), this, this); | ||
| 152 | - } | ||
| 153 | - | ||
| 154 | - public int getScreenWidth() { | ||
| 155 | - WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); | ||
| 156 | - return wm == null ? 0 : wm.getDefaultDisplay().getWidth(); | ||
| 157 | - } | ||
| 158 | - | ||
| 159 | - private long lastClickTime = 0L; | ||
| 160 | - private static final int FAST_CLICK_DELAY_TIME = 1000; // 快速点击间隔 | ||
| 161 | - | ||
| 162 | - private void initView() { | ||
| 163 | - int cameraCount = Camera.getNumberOfCameras(); | ||
| 164 | - setWillNotDraw(false); | ||
| 165 | - int[] cid = {1}; | ||
| 166 | - View view = LayoutInflater.from(getContext()).inflate(R.layout.camera_view, this); | ||
| 167 | - mVideoView = (VideoView) view.findViewById(R.id.video_preview); | ||
| 168 | - mVideoView.setLayoutParams(new LayoutParams(getScreenWidth(), getScreenWidth() * 1920 / 1080)); | ||
| 169 | - mPhoto = (ImageView) view.findViewById(R.id.image_photo); | ||
| 170 | - mSwitchCamera = (ImageView) view.findViewById(R.id.image_switch); | ||
| 171 | - mSwitchCamera.setImageResource(R.drawable.fb_xiangji); | ||
| 172 | - mFlashLamp = (ImageView) view.findViewById(R.id.image_flash); | ||
| 173 | - mFlashLamp.setImageResource(R.drawable.ic_flash_off); | ||
| 174 | - machine.flash(Camera.Parameters.FLASH_MODE_OFF); | ||
| 175 | - if (System.currentTimeMillis() - lastClick <= 4000) { | ||
| 176 | - return; | ||
| 177 | - } else { | ||
| 178 | - mFlashLamp.setOnClickListener(new OnClickListener() { | ||
| 179 | - @Override | ||
| 180 | - public void onClick(View v) { | ||
| 181 | - if (CameraInterface.getInstance().getSelectedCamera() == Camera.CameraInfo.CAMERA_FACING_FRONT) | ||
| 182 | - return; | ||
| 183 | - type_flash++; | ||
| 184 | - if (type_flash > 0x023) { | ||
| 185 | - type_flash = 0x022; | ||
| 186 | - } | ||
| 187 | - if (type_flash == TYPE_FLASH_ON) { | ||
| 188 | - mFlashLamp.setImageResource(R.drawable.ic_flash_on); | ||
| 189 | - machine.flash(Camera.Parameters.FLASH_MODE_ON); | ||
| 190 | - } else if (type_flash == TYPE_FLASH_OFF) { | ||
| 191 | - mFlashLamp.setImageResource(R.drawable.ic_flash_off); | ||
| 192 | - machine.flash(Camera.Parameters.FLASH_MODE_OFF); | ||
| 193 | - } else { | ||
| 194 | - AlertUtil.showDeftToast(v.getContext(), "闪光灯打开失败"); | ||
| 195 | - mFlashLamp.setImageResource(R.drawable.ic_flash_off); | ||
| 196 | - machine.flash(Camera.Parameters.FLASH_MODE_OFF); | ||
| 197 | - } | ||
| 198 | - } | ||
| 199 | - }); | ||
| 200 | - } | ||
| 201 | - lastClick = System.currentTimeMillis(); | ||
| 202 | - if (cameraCount == 1) { | ||
| 203 | - mSwitchCamera.setVisibility(GONE); | ||
| 204 | - mSwitchCamera.setClickable(false); | ||
| 205 | - } else { | ||
| 206 | - mSwitchCamera.setVisibility(VISIBLE); | ||
| 207 | - mSwitchCamera.setClickable(true); | ||
| 208 | - mSwitchCamera.setImageResource(R.drawable.fb_xiangji); | ||
| 209 | - //切换摄像头 | ||
| 210 | - long[] mHits = new long[3]; | ||
| 211 | - mSwitchCamera.setOnClickListener(new OnClickListener() { | ||
| 212 | - @Override | ||
| 213 | - public void onClick(View v) { | ||
| 214 | - if (System.currentTimeMillis() - lastClickTime < FAST_CLICK_DELAY_TIME) { | ||
| 215 | - } else { | ||
| 216 | - if (machine.swtich(mVideoView.getHolder(), screenProp)) { | ||
| 217 | - if (cid[0] == 1) { | ||
| 218 | - // mFlashLamp.setVisibility(INVISIBLE); | ||
| 219 | - machine.flash(Camera.Parameters.FLASH_MODE_OFF); | ||
| 220 | - mFlashLamp.setImageResource(R.drawable.ic_flash_off); | ||
| 221 | - cid[0] = 1; | ||
| 222 | - } else if (cid[0] == 0) { | ||
| 223 | - mFlashLamp.setVisibility(VISIBLE); | ||
| 224 | - machine.flash(Camera.Parameters.FLASH_MODE_OFF); | ||
| 225 | - mFlashLamp.setImageResource(R.drawable.ic_flash_off); | ||
| 226 | - cid[0] = 0; | ||
| 227 | - } | ||
| 228 | - } else { | ||
| 229 | - mCaptureLayout.onError(); | ||
| 230 | - } | ||
| 231 | - } | ||
| 232 | - lastClickTime = System.currentTimeMillis(); | ||
| 233 | - | ||
| 234 | - } | ||
| 235 | - }); | ||
| 236 | - } | ||
| 237 | - mCaptureLayout = (CaptureLayout) view.findViewById(R.id.capture_layout); | ||
| 238 | - mCaptureLayout.setDuration(duration); | ||
| 239 | - mCaptureLayout.setIconSrc(iconLeft, iconRight); | ||
| 240 | - mFoucsView = (FoucsView) view.findViewById(R.id.fouce_view); | ||
| 241 | - mVideoView.getHolder().addCallback(this); | ||
| 242 | - //拍照 录像 | ||
| 243 | - mCaptureLayout.setCaptureLisenter(new CaptureListener() { | ||
| 244 | - @Override | ||
| 245 | - public void takePictures() { | ||
| 246 | - if (machine.capture()) { | ||
| 247 | - mSwitchCamera.setVisibility(INVISIBLE); | ||
| 248 | - recordEnd = true; | ||
| 249 | - mFlashLamp.setVisibility(INVISIBLE); | ||
| 250 | - } else { | ||
| 251 | - AlertUtil.showDeftToast(getContext(), "相机拍摄失败"); | ||
| 252 | - Log.e("Camera", "相机拍摄失败."); | ||
| 253 | - mCaptureLayout.onError(); | ||
| 254 | - recordEnd = true; | ||
| 255 | - } | ||
| 256 | - } | ||
| 257 | - | ||
| 258 | - @Override | ||
| 259 | - public void recordStart() { | ||
| 260 | - recordEnd = false; | ||
| 261 | - if (machine.record(mVideoView.getHolder().getSurface(), screenProp,getContext())) { | ||
| 262 | - mSwitchCamera.setVisibility(INVISIBLE); | ||
| 263 | - mFlashLamp.setVisibility(INVISIBLE); | ||
| 264 | - } else { | ||
| 265 | - AlertUtil.showDeftToast(getContext(), "相机开启失败"); | ||
| 266 | - Log.e("Camera", "相机开启失败."); | ||
| 267 | - mCaptureLayout.onError(true); | ||
| 268 | - } | ||
| 269 | - } | ||
| 270 | - | ||
| 271 | - @Override | ||
| 272 | - public void recordShort(final long time) { | ||
| 273 | - mCaptureLayout.setTextWithAnimation("录制时间过短"); | ||
| 274 | - mSwitchCamera.setVisibility(VISIBLE); | ||
| 275 | - mFlashLamp.setVisibility(VISIBLE); | ||
| 276 | - postDelayed(new Runnable() { | ||
| 277 | - @Override | ||
| 278 | - public void run() { | ||
| 279 | - machine.stopRecord(true, time); | ||
| 280 | - } | ||
| 281 | - }, 1500 - time); | ||
| 282 | - } | ||
| 283 | - | ||
| 284 | - @Override | ||
| 285 | - public void recordEnd(long time) { | ||
| 286 | - machine.stopRecord(false, time); | ||
| 287 | - recordEnd = true; | ||
| 288 | - if (jCameraLisenter != null) { | ||
| 289 | - jCameraLisenter.recordEnd(videoUrl, duration); | ||
| 290 | - } | ||
| 291 | - } | ||
| 292 | - | ||
| 293 | - @Override | ||
| 294 | - public void recordZoom(float zoom) { | ||
| 295 | - LogUtil.i("recordZoom"); | ||
| 296 | - machine.zoom(zoom, CameraInterface.TYPE_RECORDER); | ||
| 297 | - } | ||
| 298 | - | ||
| 299 | - @Override | ||
| 300 | - public void recordError() { | ||
| 301 | - if (errorLisenter != null) { | ||
| 302 | - errorLisenter.AudioPermissionError(); | ||
| 303 | - } | ||
| 304 | - } | ||
| 305 | - }); | ||
| 306 | - //确认 取消 | ||
| 307 | - mCaptureLayout.setTypeLisenter(new TypeListener() { | ||
| 308 | - @Override | ||
| 309 | - public void cancel() { | ||
| 310 | - machine.confirm(); | ||
| 311 | - } | ||
| 312 | - | ||
| 313 | - @Override | ||
| 314 | - public void confirm() { | ||
| 315 | - machine.cancle(mVideoView.getHolder(), screenProp); | ||
| 316 | - stopVideo(); | ||
| 317 | - recordEnd = false; | ||
| 318 | - mFlashLamp.setImageResource(R.drawable.ic_flash_off); | ||
| 319 | - machine.flash(Camera.Parameters.FLASH_MODE_OFF); | ||
| 320 | - } | ||
| 321 | - }); | ||
| 322 | - //退出 | ||
| 323 | -// mCaptureLayout.setReturnLisenter(new ReturnListener() { | ||
| 324 | -// @Override | ||
| 325 | -// public void onReturn() { | ||
| 326 | -// if (jCameraLisenter != null) { | ||
| 327 | -// jCameraLisenter.quit(); | ||
| 328 | -// } | ||
| 329 | -// } | ||
| 330 | -// }); | ||
| 331 | - mCaptureLayout.setLeftClickListener(new ClickListener() { | ||
| 332 | - @Override | ||
| 333 | - public void onClick() { | ||
| 334 | - if (leftClickListener != null) { | ||
| 335 | - leftClickListener.onClick(); | ||
| 336 | - } | ||
| 337 | - } | ||
| 338 | - }); | ||
| 339 | - mCaptureLayout.setRightClickListener(new ClickListener() { | ||
| 340 | - @Override | ||
| 341 | - public void onClick() { | ||
| 342 | - if (rightClickListener != null) { | ||
| 343 | - rightClickListener.onClick(); | ||
| 344 | - } | ||
| 345 | - } | ||
| 346 | - }); | ||
| 347 | - } | ||
| 348 | - | ||
| 349 | - public void setDuration(int duration_ss) { | ||
| 350 | - if (mCaptureLayout != null) mCaptureLayout.setDuration(duration = duration_ss); | ||
| 351 | - } | ||
| 352 | - | ||
| 353 | - @Override | ||
| 354 | - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
| 355 | - super.onMeasure(widthMeasureSpec, heightMeasureSpec); | ||
| 356 | - float widthSize = mVideoView.getMeasuredWidth(); | ||
| 357 | - float heightSize = mVideoView.getMeasuredHeight(); | ||
| 358 | - if (screenProp == 0) { | ||
| 359 | - screenProp = heightSize / widthSize; | ||
| 360 | - } | ||
| 361 | - } | ||
| 362 | - | ||
| 363 | - @Override | ||
| 364 | - public void cameraHasOpened() { | ||
| 365 | - CameraInterface.getInstance().doStartPreview(mVideoView.getHolder(), screenProp); | ||
| 366 | - } | ||
| 367 | - | ||
| 368 | - //生命周期onResume | ||
| 369 | - public void onResume() { | ||
| 370 | - LogUtil.i("JCameraView onResume"); | ||
| 371 | - resetState(TYPE_DEFAULT); //重置状态 | ||
| 372 | - CameraInterface.getInstance().registerSensorManager(getContext()); | ||
| 373 | - CameraInterface.getInstance().setSwitchView(mSwitchCamera, mFlashLamp); | ||
| 374 | - machine.setDefaultState(); | ||
| 375 | - machine.start(mVideoView.getHolder(), screenProp); | ||
| 376 | - } | ||
| 377 | - | ||
| 378 | - //生命周期onPause | ||
| 379 | - public void onStop() { | ||
| 380 | - LogUtil.i("JCameraView onPause"); | ||
| 381 | - stopVideo(); | ||
| 382 | - resetState(TYPE_PICTURE); | ||
| 383 | - CameraInterface.getInstance().isPreview(false); | ||
| 384 | - CameraInterface.getInstance().unregisterSensorManager(getContext()); | ||
| 385 | - } | ||
| 386 | - | ||
| 387 | - //SurfaceView生命周期 | ||
| 388 | - @Override | ||
| 389 | - public void surfaceCreated(SurfaceHolder holder) { | ||
| 390 | - LogUtil.i("JCameraView SurfaceCreated"); | ||
| 391 | - if (recordEnd) { | ||
| 392 | - if (mMediaPlayer != null) { | ||
| 393 | - mMediaPlayer.setDisplay(holder); | ||
| 394 | - mMediaPlayer.start(); | ||
| 395 | - } | ||
| 396 | - return; | ||
| 397 | - } | ||
| 398 | - new Thread() { | ||
| 399 | - @Override | ||
| 400 | - public void run() { | ||
| 401 | - CameraInterface.getInstance().doOpenCamera(JCameraView.this); | ||
| 402 | - } | ||
| 403 | - }.start(); | ||
| 404 | - } | ||
| 405 | - | ||
| 406 | - @Override | ||
| 407 | - public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { | ||
| 408 | - } | ||
| 409 | - | ||
| 410 | - @Override | ||
| 411 | - public void surfaceDestroyed(SurfaceHolder holder) { | ||
| 412 | - LogUtil.i("JCameraView SurfaceDestroyed"); | ||
| 413 | - if (recordEnd) { | ||
| 414 | - if (mMediaPlayer != null) | ||
| 415 | - mMediaPlayer.setDisplay(null); | ||
| 416 | - pausePlayer(); | ||
| 417 | - return; | ||
| 418 | - } | ||
| 419 | - CameraInterface.getInstance().doDestroyCamera(); | ||
| 420 | - | ||
| 421 | - } | ||
| 422 | - | ||
| 423 | - | ||
| 424 | - @Override | ||
| 425 | - public boolean onTouchEvent(MotionEvent event) { | ||
| 426 | - switch (event.getAction()) { | ||
| 427 | - case MotionEvent.ACTION_DOWN: | ||
| 428 | - if (event.getPointerCount() == 1) { | ||
| 429 | - //显示对焦指示器 | ||
| 430 | - setFocusViewWidthAnimation(event.getX(), event.getY()); | ||
| 431 | - } | ||
| 432 | - if (event.getPointerCount() == 2) { | ||
| 433 | - Log.i("CJT", "ACTION_DOWN = " + 2); | ||
| 434 | - } | ||
| 435 | - break; | ||
| 436 | - case MotionEvent.ACTION_MOVE: | ||
| 437 | - if (event.getPointerCount() == 1) { | ||
| 438 | - firstTouch = true; | ||
| 439 | - } | ||
| 440 | - if (event.getPointerCount() == 2) { | ||
| 441 | - //第一个点 | ||
| 442 | - float point_1_X = event.getX(0); | ||
| 443 | - float point_1_Y = event.getY(0); | ||
| 444 | - //第二个点 | ||
| 445 | - float point_2_X = event.getX(1); | ||
| 446 | - float point_2_Y = event.getY(1); | ||
| 447 | - | ||
| 448 | - float result = (float) Math.sqrt(Math.pow(point_1_X - point_2_X, 2) + Math.pow(point_1_Y - | ||
| 449 | - point_2_Y, 2)); | ||
| 450 | - | ||
| 451 | - if (firstTouch) { | ||
| 452 | - firstTouchLength = result; | ||
| 453 | - firstTouch = false; | ||
| 454 | - } | ||
| 455 | - if ((int) (result - firstTouchLength) / zoomGradient != 0) { | ||
| 456 | - firstTouch = true; | ||
| 457 | - machine.zoom(result - firstTouchLength, CameraInterface.TYPE_CAPTURE); | ||
| 458 | - } | ||
| 459 | -// Log.i("CJT", "result = " + (result - firstTouchLength)); | ||
| 460 | - } | ||
| 461 | - break; | ||
| 462 | - case MotionEvent.ACTION_UP: | ||
| 463 | - firstTouch = true; | ||
| 464 | - break; | ||
| 465 | - } | ||
| 466 | - return true; | ||
| 467 | - } | ||
| 468 | - | ||
| 469 | - //对焦框指示器动画 | ||
| 470 | - private void setFocusViewWidthAnimation(float x, float y) { | ||
| 471 | - machine.foucs(x, y, new CameraInterface.FocusCallback() { | ||
| 472 | - @Override | ||
| 473 | - public void focusSuccess() { | ||
| 474 | - mFoucsView.setVisibility(INVISIBLE); | ||
| 475 | - } | ||
| 476 | - }); | ||
| 477 | - } | ||
| 478 | - | ||
| 479 | - private void updateVideoViewSize(float videoWidth, float videoHeight) { | ||
| 480 | - if (videoWidth > videoHeight) { | ||
| 481 | - LayoutParams videoViewParam; | ||
| 482 | - int height = (int) ((videoHeight / videoWidth) * getWidth()); | ||
| 483 | - videoViewParam = new LayoutParams(LayoutParams.MATCH_PARENT, height); | ||
| 484 | - videoViewParam.gravity = Gravity.CENTER; | ||
| 485 | - mVideoView.setLayoutParams(videoViewParam); | ||
| 486 | - } | ||
| 487 | - } | ||
| 488 | - | ||
| 489 | - /************************************************** | ||
| 490 | - * 对外提供的API * | ||
| 491 | - **************************************************/ | ||
| 492 | - | ||
| 493 | - public void setSaveVideoPath(String path) { | ||
| 494 | - CameraInterface.getInstance().setSaveVideoPath(path); | ||
| 495 | - } | ||
| 496 | - | ||
| 497 | - | ||
| 498 | - public void setJCameraLisenter(JCameraListener jCameraLisenter) { | ||
| 499 | - this.jCameraLisenter = jCameraLisenter; | ||
| 500 | - } | ||
| 501 | - | ||
| 502 | - | ||
| 503 | - private ErrorListener errorLisenter; | ||
| 504 | - | ||
| 505 | - //启动Camera错误回调 | ||
| 506 | - public void setErrorLisenter(ErrorListener errorLisenter) { | ||
| 507 | - this.errorLisenter = errorLisenter; | ||
| 508 | - CameraInterface.getInstance().setErrorLinsenter(errorLisenter); | ||
| 509 | - } | ||
| 510 | - | ||
| 511 | - //设置CaptureButton功能(拍照和录像) | ||
| 512 | - public void setFeatures(int state) { | ||
| 513 | - this.mCaptureLayout.setButtonFeatures(state); | ||
| 514 | - } | ||
| 515 | - | ||
| 516 | - //设置录制质量 | ||
| 517 | - public void setMediaQuality(int quality) { | ||
| 518 | - CameraInterface.getInstance().setMediaQuality(quality); | ||
| 519 | - } | ||
| 520 | - | ||
| 521 | - public void setButtonVisiblity() { | ||
| 522 | - mCaptureLayout.setVisibility(GONE); | ||
| 523 | - } | ||
| 524 | - | ||
| 525 | - @Override | ||
| 526 | - public void resetState(int type) { | ||
| 527 | - switch (type) { | ||
| 528 | - case TYPE_VIDEO: | ||
| 529 | - stopVideo(); //停止播放 | ||
| 530 | - //初始化VideoView | ||
| 531 | - FileUtil.deleteFile(videoUrl); | ||
| 532 | - mVideoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); | ||
| 533 | - machine.start(mVideoView.getHolder(), screenProp); | ||
| 534 | - break; | ||
| 535 | - case TYPE_PICTURE: | ||
| 536 | - mPhoto.setVisibility(INVISIBLE); | ||
| 537 | - break; | ||
| 538 | - case TYPE_SHORT: | ||
| 539 | - break; | ||
| 540 | - case TYPE_DEFAULT: | ||
| 541 | - mVideoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); | ||
| 542 | - break; | ||
| 543 | - } | ||
| 544 | - mSwitchCamera.setVisibility(VISIBLE); | ||
| 545 | - mFlashLamp.setVisibility(VISIBLE); | ||
| 546 | - mCaptureLayout.resetCaptureLayout(); | ||
| 547 | - } | ||
| 548 | - | ||
| 549 | - @Override | ||
| 550 | - public void confirmState(int type) { | ||
| 551 | - switch (type) { | ||
| 552 | - case TYPE_VIDEO: | ||
| 553 | - int duration = mMediaPlayer.getDuration(); | ||
| 554 | - stopVideo(); //停止播放 | ||
| 555 | - mVideoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); | ||
| 556 | - machine.start(mVideoView.getHolder(), screenProp); | ||
| 557 | - if (jCameraLisenter != null) { | ||
| 558 | - jCameraLisenter.recordSuccess(videoUrl, duration); | ||
| 559 | - } | ||
| 560 | - break; | ||
| 561 | - case TYPE_PICTURE: | ||
| 562 | - mPhoto.setVisibility(INVISIBLE); | ||
| 563 | - if (jCameraLisenter != null) { | ||
| 564 | - jCameraLisenter.captureSuccess(captureBitmap); | ||
| 565 | - } | ||
| 566 | - break; | ||
| 567 | - case TYPE_SHORT: | ||
| 568 | - break; | ||
| 569 | - case TYPE_DEFAULT: | ||
| 570 | - break; | ||
| 571 | - } | ||
| 572 | - mCaptureLayout.resetCaptureLayout(); | ||
| 573 | - } | ||
| 574 | - | ||
| 575 | - @Override | ||
| 576 | - public void showPicture(Bitmap bitmap, boolean isVertical) { | ||
| 577 | - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) { | ||
| 578 | - ViewGroup.LayoutParams layoutParams = mPhoto.getLayoutParams(); | ||
| 579 | - layoutParams.width = ScreenUtil.getScreenWidth(getContext()); | ||
| 580 | - layoutParams.height = ScreenUtil.getScreenHeight(getContext()); | ||
| 581 | - mPhoto.setLayoutParams(layoutParams); | ||
| 582 | - } else { | ||
| 583 | - if (isVertical) { | ||
| 584 | - mPhoto.setScaleType(ImageView.ScaleType.FIT_XY); | ||
| 585 | - } else { | ||
| 586 | - mPhoto.setScaleType(ImageView.ScaleType.FIT_CENTER); | ||
| 587 | - } | ||
| 588 | - } | ||
| 589 | - captureBitmap = bitmap; | ||
| 590 | - mPhoto.setVisibility(VISIBLE); | ||
| 591 | - mPhoto.setImageBitmap(captureBitmap); | ||
| 592 | - | ||
| 593 | - mCaptureLayout.startAlphaAnimation(); | ||
| 594 | - mCaptureLayout.startTypeBtnAnimator(); | ||
| 595 | - } | ||
| 596 | - | ||
| 597 | - @Override | ||
| 598 | - public void playVideo(final String url) { | ||
| 599 | - videoUrl = url; | ||
| 600 | - new Thread(new Runnable() { | ||
| 601 | - @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) | ||
| 602 | - @Override | ||
| 603 | - public void run() { | ||
| 604 | - try { | ||
| 605 | - if (mMediaPlayer == null) { | ||
| 606 | - mMediaPlayer = new MediaPlayer(); | ||
| 607 | - } else { | ||
| 608 | - mMediaPlayer.reset(); | ||
| 609 | - } | ||
| 610 | - mMediaPlayer.setDataSource(url); | ||
| 611 | - mMediaPlayer.setDisplay(mVideoView.getHolder()); | ||
| 612 | - mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT); | ||
| 613 | - mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); | ||
| 614 | - mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer | ||
| 615 | - .OnVideoSizeChangedListener() { | ||
| 616 | - @Override | ||
| 617 | - public void | ||
| 618 | - onVideoSizeChanged(MediaPlayer mp, int width, int height) { | ||
| 619 | - updateVideoViewSize(mMediaPlayer.getVideoWidth(), mMediaPlayer | ||
| 620 | - .getVideoHeight()); | ||
| 621 | - } | ||
| 622 | - }); | ||
| 623 | - mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { | ||
| 624 | - @Override | ||
| 625 | - public void onPrepared(MediaPlayer mp) { | ||
| 626 | - mMediaPlayer.start(); | ||
| 627 | - } | ||
| 628 | - }); | ||
| 629 | - mMediaPlayer.setLooping(true); | ||
| 630 | - mMediaPlayer.prepare(); | ||
| 631 | - } catch (Exception e) { | ||
| 632 | - Log.e("MediaPlayer", "", e); | ||
| 633 | - } | ||
| 634 | - } | ||
| 635 | - }).start(); | ||
| 636 | - } | ||
| 637 | - | ||
| 638 | - @Override | ||
| 639 | - public void stopVideo() { | ||
| 640 | - if (mMediaPlayer != null) { | ||
| 641 | - mMediaPlayer.stop(); | ||
| 642 | - mMediaPlayer.release(); | ||
| 643 | - mMediaPlayer = null; | ||
| 644 | - } | ||
| 645 | - } | ||
| 646 | - | ||
| 647 | - @Override | ||
| 648 | - public void setTip(String tip) { | ||
| 649 | - mCaptureLayout.setTip(tip); | ||
| 650 | - } | ||
| 651 | - | ||
| 652 | - @Override | ||
| 653 | - public void startPreviewCallback() { | ||
| 654 | - LogUtil.i("startPreviewCallback"); | ||
| 655 | - handlerFoucs(mFoucsView.getWidth() / 2, mFoucsView.getHeight() / 2); | ||
| 656 | - } | ||
| 657 | - | ||
| 658 | - @Override | ||
| 659 | - public boolean handlerFoucs(float x, float y) { | ||
| 660 | - if (y > mCaptureLayout.getTop()) { | ||
| 661 | - return false; | ||
| 662 | - } | ||
| 663 | - mFoucsView.setVisibility(VISIBLE); | ||
| 664 | - if (x < mFoucsView.getWidth() / 2) { | ||
| 665 | - x = mFoucsView.getWidth() / 2; | ||
| 666 | - } | ||
| 667 | - if (x > layout_width - mFoucsView.getWidth() / 2) { | ||
| 668 | - x = layout_width - mFoucsView.getWidth() / 2; | ||
| 669 | - } | ||
| 670 | - if (y < mFoucsView.getWidth() / 2) { | ||
| 671 | - y = mFoucsView.getWidth() / 2; | ||
| 672 | - } | ||
| 673 | - if (y > mCaptureLayout.getTop() - mFoucsView.getWidth() / 2) { | ||
| 674 | - y = mCaptureLayout.getTop() - mFoucsView.getWidth() / 2; | ||
| 675 | - } | ||
| 676 | - mFoucsView.setX(x - mFoucsView.getWidth() / 2); | ||
| 677 | - mFoucsView.setY(y - mFoucsView.getHeight() / 2); | ||
| 678 | - ObjectAnimator scaleX = ObjectAnimator.ofFloat(mFoucsView, "scaleX", 1, 0.6f); | ||
| 679 | - ObjectAnimator scaleY = ObjectAnimator.ofFloat(mFoucsView, "scaleY", 1, 0.6f); | ||
| 680 | - ObjectAnimator alpha = ObjectAnimator.ofFloat(mFoucsView, "alpha", 1f, 0.4f, 1f, 0.4f, 1f, 0.4f, 1f); | ||
| 681 | - AnimatorSet animSet = new AnimatorSet(); | ||
| 682 | - animSet.play(scaleX).with(scaleY).before(alpha); | ||
| 683 | - animSet.setDuration(400); | ||
| 684 | - animSet.start(); | ||
| 685 | - return true; | ||
| 686 | - } | ||
| 687 | - | ||
| 688 | - @Override | ||
| 689 | - public void pausePlayer() { | ||
| 690 | - if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { | ||
| 691 | - mMediaPlayer.pause(); | ||
| 692 | - } | ||
| 693 | - } | ||
| 694 | - | ||
| 695 | - @Override | ||
| 696 | - public void resumePlayer() { | ||
| 697 | - if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) { | ||
| 698 | - mMediaPlayer.start(); | ||
| 699 | - } | ||
| 700 | - } | ||
| 701 | - | ||
| 702 | - public void setLeftClickListener(ClickListener clickListener) { | ||
| 703 | - this.leftClickListener = clickListener; | ||
| 704 | - } | ||
| 705 | - | ||
| 706 | - public void setRightClickListener(ClickListener clickListener) { | ||
| 707 | - this.rightClickListener = clickListener; | ||
| 708 | - } | ||
| 709 | - | ||
| 710 | - public void onDestroy() { | ||
| 711 | - if (recordEnd) { | ||
| 712 | - CameraInterface.getInstance().doDestroyCamera(); | ||
| 713 | - } | ||
| 714 | - if (machine != null) machine.onDestroy(); | ||
| 715 | - } | ||
| 716 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/ReturnButton.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.graphics.Canvas; | ||
| 5 | -import android.graphics.Paint; | ||
| 6 | -import android.graphics.Path; | ||
| 7 | -import android.view.View; | ||
| 8 | - | ||
| 9 | -/** | ||
| 10 | - * ===================================== | ||
| 11 | - * 作 者: 陈嘉桐 445263848@qq.com | ||
| 12 | - * 版 本:1.0.4 | ||
| 13 | - * 创建日期:2017/4/26 | ||
| 14 | - * 描 述:向下箭头的退出按钮 | ||
| 15 | - * ===================================== | ||
| 16 | - */ | ||
| 17 | -public class ReturnButton extends View { | ||
| 18 | - | ||
| 19 | - private int size; | ||
| 20 | - | ||
| 21 | - private int center_X; | ||
| 22 | - private int center_Y; | ||
| 23 | - private float strokeWidth; | ||
| 24 | - | ||
| 25 | - private Paint paint; | ||
| 26 | - Path path; | ||
| 27 | - | ||
| 28 | - public ReturnButton(Context context, int size) { | ||
| 29 | - this(context); | ||
| 30 | - this.size = size; | ||
| 31 | - center_X = size / 2; | ||
| 32 | - center_Y = size / 2; | ||
| 33 | - | ||
| 34 | - strokeWidth = size / 15f; | ||
| 35 | - | ||
| 36 | - paint = new Paint(); | ||
| 37 | - paint.setAntiAlias(true); | ||
| 38 | - paint.setColor(0xffffa300); | ||
| 39 | - paint.setStyle(Paint.Style.STROKE); | ||
| 40 | - paint.setStrokeWidth(strokeWidth); | ||
| 41 | - | ||
| 42 | - path = new Path(); | ||
| 43 | - } | ||
| 44 | - | ||
| 45 | - public ReturnButton(Context context) { | ||
| 46 | - super(context); | ||
| 47 | - } | ||
| 48 | - | ||
| 49 | - @Override | ||
| 50 | - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
| 51 | - setMeasuredDimension(size, size / 2); | ||
| 52 | - } | ||
| 53 | - | ||
| 54 | - @Override | ||
| 55 | - protected void onDraw(Canvas canvas) { | ||
| 56 | - super.onDraw(canvas); | ||
| 57 | - path.moveTo(strokeWidth, strokeWidth/2); | ||
| 58 | - path.lineTo(center_X, center_Y - strokeWidth/2); | ||
| 59 | - path.lineTo(size - strokeWidth, strokeWidth/2); | ||
| 60 | - | ||
| 61 | - canvas.drawPath(path, paint); | ||
| 62 | - } | ||
| 63 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/TypeButton.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.widget.ImageView; | ||
| 5 | - | ||
| 6 | -import com.cnlive.strike.base.R; | ||
| 7 | - | ||
| 8 | - | ||
| 9 | -/** | ||
| 10 | - * ===================================== | ||
| 11 | - * 作 者: 陈嘉桐 445263848@qq.com | ||
| 12 | - * 版 本:1.0.4 | ||
| 13 | - * 创建日期:2017/4/26 | ||
| 14 | - * 描 述:拍照或录制完成后弹出的确认和返回按钮 | ||
| 15 | - * ===================================== | ||
| 16 | - */ | ||
| 17 | -public class TypeButton extends ImageView { | ||
| 18 | - public static final int TYPE_CANCEL = 0x001; | ||
| 19 | - public static final int TYPE_CONFIRM = 0x002; | ||
| 20 | -// private int button_type; | ||
| 21 | - private int button_size; | ||
| 22 | - | ||
| 23 | -// private float center_X; | ||
| 24 | -// private float center_Y; | ||
| 25 | -// private float button_radius; | ||
| 26 | - | ||
| 27 | -// private Paint mPaint; | ||
| 28 | -// private Path path; | ||
| 29 | -// private float strokeWidth; | ||
| 30 | - | ||
| 31 | - private float index; | ||
| 32 | -// private RectF rectF; | ||
| 33 | - | ||
| 34 | - public TypeButton(Context context, int type, int size) { | ||
| 35 | - super(context); | ||
| 36 | - if (type == TYPE_CANCEL) { | ||
| 37 | - setImageResource(R.drawable.fb_wancheng); | ||
| 38 | - } | ||
| 39 | - if (type == TYPE_CONFIRM){ | ||
| 40 | - setImageResource(R.drawable.fb_chongpai); | ||
| 41 | - } | ||
| 42 | -// this.button_type = type; | ||
| 43 | - button_size = size; | ||
| 44 | -// button_radius = size / 2.0f; | ||
| 45 | -// center_X = size / 2.0f; | ||
| 46 | -// center_Y = size / 2.0f; | ||
| 47 | - | ||
| 48 | -// mPaint = new Paint(); | ||
| 49 | -// path = new Path(); | ||
| 50 | -// strokeWidth = size / 50f; | ||
| 51 | -// index = button_size / 12f; | ||
| 52 | -// rectF = new RectF(center_X, center_Y - index, center_X + index * 2, center_Y + index); | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | - @Override | ||
| 56 | - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
| 57 | - super.onMeasure(widthMeasureSpec, heightMeasureSpec); | ||
| 58 | - setMeasuredDimension(button_size, button_size); | ||
| 59 | - } | ||
| 60 | - | ||
| 61 | -// @Override | ||
| 62 | -// protected void onDraw(Canvas canvas) { | ||
| 63 | -// super.onDraw(canvas); | ||
| 64 | -// //如果类型为取消,则绘制内部为返回箭头 | ||
| 65 | -// if (button_type == TYPE_CANCEL) { | ||
| 66 | -// mPaint.setAntiAlias(true); | ||
| 67 | -// mPaint.setColor(0xEEDCDCDC); | ||
| 68 | -// mPaint.setStyle(Paint.Style.FILL); | ||
| 69 | -// canvas.drawCircle(center_X, center_Y, button_radius, mPaint); | ||
| 70 | -// | ||
| 71 | -// mPaint.setColor(Color.BLACK); | ||
| 72 | -// mPaint.setStyle(Paint.Style.STROKE); | ||
| 73 | -// mPaint.setStrokeWidth(strokeWidth); | ||
| 74 | -// | ||
| 75 | -// path.moveTo(center_X - index / 7, center_Y + index); | ||
| 76 | -// path.lineTo(center_X + index, center_Y + index); | ||
| 77 | -// | ||
| 78 | -// path.arcTo(rectF, 90, -180); | ||
| 79 | -// path.lineTo(center_X - index, center_Y - index); | ||
| 80 | -// canvas.drawPath(path, mPaint); | ||
| 81 | -// mPaint.setStyle(Paint.Style.FILL); | ||
| 82 | -// path.reset(); | ||
| 83 | -// path.moveTo(center_X - index, (float) (center_Y - index * 1.5)); | ||
| 84 | -// path.lineTo(center_X - index, (float) (center_Y - index / 2.3)); | ||
| 85 | -// path.lineTo((float) (center_X - index * 1.6), center_Y - index); | ||
| 86 | -// path.close(); | ||
| 87 | -// canvas.drawPath(path, mPaint); | ||
| 88 | -// | ||
| 89 | -// } | ||
| 90 | -// //如果类型为确认,则绘制绿色勾 | ||
| 91 | -// if (button_type == TYPE_CONFIRM) { | ||
| 92 | -// mPaint.setAntiAlias(true); | ||
| 93 | -// mPaint.setColor(0xFFFFFFFF); | ||
| 94 | -// mPaint.setStyle(Paint.Style.FILL); | ||
| 95 | -// canvas.drawCircle(center_X, center_Y, button_radius, mPaint); | ||
| 96 | -// mPaint.setAntiAlias(true); | ||
| 97 | -// mPaint.setStyle(Paint.Style.STROKE); | ||
| 98 | -// mPaint.setColor(0xFF00CC00); | ||
| 99 | -// mPaint.setStrokeWidth(strokeWidth); | ||
| 100 | -// | ||
| 101 | -// path.moveTo(center_X - button_size / 6f, center_Y); | ||
| 102 | -// path.lineTo(center_X - button_size / 21.2f, center_Y + button_size / 7.7f); | ||
| 103 | -// path.lineTo(center_X + button_size / 4.0f, center_Y - button_size / 8.5f); | ||
| 104 | -// path.lineTo(center_X - button_size / 21.2f, center_Y + button_size / 9.4f); | ||
| 105 | -// path.close(); | ||
| 106 | -// canvas.drawPath(path, mPaint); | ||
| 107 | -// } | ||
| 108 | -// } | ||
| 109 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/listener/CaptureListener.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.listener; | ||
| 2 | - | ||
| 3 | -/** | ||
| 4 | - * create by CJT2325 | ||
| 5 | - * 445263848@qq.com. | ||
| 6 | - */ | ||
| 7 | - | ||
| 8 | -public interface CaptureListener { | ||
| 9 | - void takePictures(); | ||
| 10 | - | ||
| 11 | - void recordShort(long time); | ||
| 12 | - | ||
| 13 | - void recordStart(); | ||
| 14 | - | ||
| 15 | - void recordEnd(long time); | ||
| 16 | - | ||
| 17 | - void recordZoom(float zoom); | ||
| 18 | - | ||
| 19 | - void recordError(); | ||
| 20 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/listener/ClickListener.java deleted
100644 → 0
core/base/src/main/java/com/cjt2325/cameralibrary/listener/ErrorListener.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.listener; | ||
| 2 | - | ||
| 3 | -/** | ||
| 4 | - * ===================================== | ||
| 5 | - * 作 者: 陈嘉桐 | ||
| 6 | - * 版 本:1.1.4 | ||
| 7 | - * 创建日期:2017/6/5 | ||
| 8 | - * 描 述: | ||
| 9 | - * ===================================== | ||
| 10 | - */ | ||
| 11 | -public interface ErrorListener { | ||
| 12 | - void onError(); | ||
| 13 | - void AudioPermissionError(); | ||
| 14 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/listener/JCameraListener.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.listener; | ||
| 2 | - | ||
| 3 | -import android.graphics.Bitmap; | ||
| 4 | - | ||
| 5 | -/** | ||
| 6 | - * ===================================== | ||
| 7 | - * 作 者: 陈嘉桐 | ||
| 8 | - * 版 本:1.1.4 | ||
| 9 | - * 创建日期:2017/4/26 | ||
| 10 | - * 描 述: | ||
| 11 | - * ===================================== | ||
| 12 | - */ | ||
| 13 | -public interface JCameraListener { | ||
| 14 | - | ||
| 15 | - void captureSuccess(Bitmap bitmap); | ||
| 16 | - | ||
| 17 | - void recordSuccess(String url, int duration); | ||
| 18 | - | ||
| 19 | - void recordEnd(String url, int duration); | ||
| 20 | - | ||
| 21 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/listener/ResultListener.java deleted
100644 → 0
core/base/src/main/java/com/cjt2325/cameralibrary/listener/ReturnListener.java deleted
100644 → 0
core/base/src/main/java/com/cjt2325/cameralibrary/listener/TypeListener.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.listener; | ||
| 2 | - | ||
| 3 | -/** | ||
| 4 | - * ===================================== | ||
| 5 | - * 作 者: 陈嘉桐 | ||
| 6 | - * 版 本:1.1.4 | ||
| 7 | - * 创建日期:2017/4/25 | ||
| 8 | - * 描 述: | ||
| 9 | - * ===================================== | ||
| 10 | - */ | ||
| 11 | -public interface TypeListener { | ||
| 12 | - void cancel(); | ||
| 13 | - | ||
| 14 | - void confirm(); | ||
| 15 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/state/BorrowPictureState.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.state; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.view.Surface; | ||
| 5 | -import android.view.SurfaceHolder; | ||
| 6 | - | ||
| 7 | -import com.cjt2325.cameralibrary.CameraInterface; | ||
| 8 | -import com.cjt2325.cameralibrary.JCameraView; | ||
| 9 | -import com.cjt2325.cameralibrary.util.LogUtil; | ||
| 10 | - | ||
| 11 | -/** | ||
| 12 | - * ===================================== | ||
| 13 | - * 作 者: 陈嘉桐 | ||
| 14 | - * 版 本:1.1.4 | ||
| 15 | - * 创建日期:2017/9/8 | ||
| 16 | - * 描 述: | ||
| 17 | - * ===================================== | ||
| 18 | - */ | ||
| 19 | -public class BorrowPictureState implements State { | ||
| 20 | - private final String TAG = "BorrowPictureState"; | ||
| 21 | - private CameraMachine machine; | ||
| 22 | - | ||
| 23 | - public BorrowPictureState(CameraMachine machine) { | ||
| 24 | - this.machine = machine; | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - @Override | ||
| 28 | - public boolean start(SurfaceHolder holder, float screenProp) { | ||
| 29 | - if (CameraInterface.getInstance().doStartPreview(holder, screenProp)) { | ||
| 30 | - machine.setState(machine.getPreviewState()); | ||
| 31 | - return true; | ||
| 32 | - } | ||
| 33 | - return false; | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - @Override | ||
| 37 | - public void stop() { | ||
| 38 | - | ||
| 39 | - } | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - @Override | ||
| 43 | - public boolean foucs(float x, float y, CameraInterface.FocusCallback callback) { | ||
| 44 | - return false; | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - @Override | ||
| 48 | - public boolean swtich(SurfaceHolder holder, float screenProp) { | ||
| 49 | - return false; | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - @Override | ||
| 53 | - public void restart() { | ||
| 54 | - | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - @Override | ||
| 58 | - public boolean capture() { | ||
| 59 | - return false; | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - @Override | ||
| 63 | - public boolean record(Surface surface, float screenProp, Context context) { | ||
| 64 | - return false; | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - @Override | ||
| 68 | - public void stopRecord(boolean isShort, long time) { | ||
| 69 | - } | ||
| 70 | - | ||
| 71 | - @Override | ||
| 72 | - public void cancle(SurfaceHolder holder, float screenProp) { | ||
| 73 | - CameraInterface.getInstance().doStartPreview(holder, screenProp); | ||
| 74 | - machine.getView().resetState(JCameraView.TYPE_PICTURE); | ||
| 75 | - machine.setState(machine.getPreviewState()); | ||
| 76 | - } | ||
| 77 | - | ||
| 78 | - @Override | ||
| 79 | - public void confirm() { | ||
| 80 | - machine.getView().confirmState(JCameraView.TYPE_PICTURE); | ||
| 81 | - machine.setState(machine.getPreviewState()); | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | - @Override | ||
| 85 | - public void zoom(float zoom, int type) { | ||
| 86 | - LogUtil.i(TAG, "zoom"); | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - @Override | ||
| 90 | - public void flash(String mode) { | ||
| 91 | - return ; | ||
| 92 | - } | ||
| 93 | - | ||
| 94 | - @Override | ||
| 95 | - public void onDestroy() { | ||
| 96 | - CameraInterface.getInstance().onDestroy(); | ||
| 97 | - } | ||
| 98 | - | ||
| 99 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/state/BorrowVideoState.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.state; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.view.Surface; | ||
| 5 | -import android.view.SurfaceHolder; | ||
| 6 | - | ||
| 7 | -import com.cjt2325.cameralibrary.CameraInterface; | ||
| 8 | -import com.cjt2325.cameralibrary.JCameraView; | ||
| 9 | -import com.cjt2325.cameralibrary.util.LogUtil; | ||
| 10 | - | ||
| 11 | -/** | ||
| 12 | - * ===================================== | ||
| 13 | - * 作 者: 陈嘉桐 | ||
| 14 | - * 版 本:1.1.4 | ||
| 15 | - * 创建日期:2017/9/8 | ||
| 16 | - * 描 述: | ||
| 17 | - * ===================================== | ||
| 18 | - */ | ||
| 19 | -public class BorrowVideoState implements State { | ||
| 20 | - private final String TAG = "BorrowVideoState"; | ||
| 21 | - private CameraMachine machine; | ||
| 22 | - | ||
| 23 | - public BorrowVideoState(CameraMachine machine) { | ||
| 24 | - this.machine = machine; | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - @Override | ||
| 28 | - public boolean start(SurfaceHolder holder, float screenProp) { | ||
| 29 | - if (CameraInterface.getInstance().doStartPreview(holder, screenProp)) { | ||
| 30 | - machine.setState(machine.getPreviewState()); | ||
| 31 | - return true; | ||
| 32 | - } | ||
| 33 | - return false; | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - @Override | ||
| 37 | - public void stop() { | ||
| 38 | - | ||
| 39 | - } | ||
| 40 | - | ||
| 41 | - @Override | ||
| 42 | - public boolean foucs(float x, float y, CameraInterface.FocusCallback callback) { | ||
| 43 | - return false; | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - @Override | ||
| 48 | - public boolean swtich(SurfaceHolder holder, float screenProp) { | ||
| 49 | - return false; | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - @Override | ||
| 53 | - public void restart() { | ||
| 54 | - | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - @Override | ||
| 58 | - public boolean capture() { | ||
| 59 | - return false; | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - @Override | ||
| 63 | - public boolean record(Surface surface, float screenProp, Context context) { | ||
| 64 | - return false; | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - @Override | ||
| 68 | - public void stopRecord(boolean isShort, long time) { | ||
| 69 | - | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | - @Override | ||
| 73 | - public void cancle(SurfaceHolder holder, float screenProp) { | ||
| 74 | - machine.getView().resetState(JCameraView.TYPE_VIDEO); | ||
| 75 | - machine.setState(machine.getPreviewState()); | ||
| 76 | - } | ||
| 77 | - | ||
| 78 | - @Override | ||
| 79 | - public void confirm() { | ||
| 80 | - machine.getView().confirmState(JCameraView.TYPE_VIDEO); | ||
| 81 | - machine.setState(machine.getPreviewState()); | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | - @Override | ||
| 85 | - public void zoom(float zoom, int type) { | ||
| 86 | - LogUtil.i(TAG, "zoom"); | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - @Override | ||
| 90 | - public void flash(String mode) { | ||
| 91 | - return ; | ||
| 92 | - } | ||
| 93 | - | ||
| 94 | - @Override | ||
| 95 | - public void onDestroy() { | ||
| 96 | - CameraInterface.getInstance().onDestroy(); | ||
| 97 | - } | ||
| 98 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/state/CameraMachine.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.state; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.view.Surface; | ||
| 5 | -import android.view.SurfaceHolder; | ||
| 6 | - | ||
| 7 | -import com.cjt2325.cameralibrary.CameraInterface; | ||
| 8 | -import com.cjt2325.cameralibrary.view.CameraView; | ||
| 9 | - | ||
| 10 | -/** | ||
| 11 | - * ===================================== | ||
| 12 | - * 作 者: 陈嘉桐 | ||
| 13 | - * 版 本:1.1.4 | ||
| 14 | - * 创建日期:2017/9/8 | ||
| 15 | - * 描 述: | ||
| 16 | - * ===================================== | ||
| 17 | - */ | ||
| 18 | -public class CameraMachine implements State { | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - private Context context; | ||
| 22 | - private State state; | ||
| 23 | - private CameraView view; | ||
| 24 | -// private CameraInterface.CameraOpenOverCallback cameraOpenOverCallback; | ||
| 25 | - | ||
| 26 | - private State previewState; //浏览状态(空闲) | ||
| 27 | - private State borrowPictureState; //浏览图片 | ||
| 28 | - private State borrowVideoState; //浏览视频 | ||
| 29 | - | ||
| 30 | - public CameraMachine(Context context, CameraView view, CameraInterface.CameraOpenOverCallback | ||
| 31 | - cameraOpenOverCallback) { | ||
| 32 | - this.context = context; | ||
| 33 | - previewState = new PreviewState(this); | ||
| 34 | - borrowPictureState = new BorrowPictureState(this); | ||
| 35 | - borrowVideoState = new BorrowVideoState(this); | ||
| 36 | - //默认设置为空闲状态 | ||
| 37 | - this.state = previewState; | ||
| 38 | -// this.cameraOpenOverCallback = cameraOpenOverCallback; | ||
| 39 | - this.view = view; | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - public CameraView getView() { | ||
| 43 | - return view; | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - public Context getContext() { | ||
| 47 | - return context; | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - public void setState(State state) { | ||
| 51 | - this.state = state; | ||
| 52 | - } | ||
| 53 | - | ||
| 54 | - public void setDefaultState() { | ||
| 55 | - this.state = previewState; | ||
| 56 | - } | ||
| 57 | - | ||
| 58 | - //获取浏览图片状态 | ||
| 59 | - State getBorrowPictureState() { | ||
| 60 | - return borrowPictureState; | ||
| 61 | - } | ||
| 62 | - | ||
| 63 | - //获取浏览视频状态 | ||
| 64 | - State getBorrowVideoState() { | ||
| 65 | - return borrowVideoState; | ||
| 66 | - } | ||
| 67 | - | ||
| 68 | - //获取空闲状态 | ||
| 69 | - public State getPreviewState() { | ||
| 70 | - return previewState; | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - @Override | ||
| 74 | - public boolean start(SurfaceHolder holder, float screenProp) { | ||
| 75 | - return state.start(holder, screenProp); | ||
| 76 | - } | ||
| 77 | - | ||
| 78 | - @Override | ||
| 79 | - public void stop() { | ||
| 80 | - state.stop(); | ||
| 81 | - } | ||
| 82 | - | ||
| 83 | - @Override | ||
| 84 | - public boolean foucs(float x, float y, CameraInterface.FocusCallback callback) { | ||
| 85 | - return state.foucs(x, y, callback); | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - @Override | ||
| 89 | - public boolean swtich(SurfaceHolder holder, float screenProp) { | ||
| 90 | - return state.swtich(holder, screenProp); | ||
| 91 | - } | ||
| 92 | - | ||
| 93 | - @Override | ||
| 94 | - public void restart() { | ||
| 95 | - state.restart(); | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - @Override | ||
| 99 | - public boolean capture() { | ||
| 100 | - return state.capture(); | ||
| 101 | - } | ||
| 102 | - | ||
| 103 | - @Override | ||
| 104 | - public boolean record(Surface surface, float screenProp,Context context) { | ||
| 105 | - return state.record(surface, screenProp,context); | ||
| 106 | - } | ||
| 107 | - | ||
| 108 | - @Override | ||
| 109 | - public void stopRecord(boolean isShort, long time) { | ||
| 110 | - state.stopRecord(isShort, time); | ||
| 111 | - } | ||
| 112 | - | ||
| 113 | - @Override | ||
| 114 | - public void cancle(SurfaceHolder holder, float screenProp) { | ||
| 115 | - state.cancle(holder, screenProp); | ||
| 116 | - } | ||
| 117 | - | ||
| 118 | - @Override | ||
| 119 | - public void confirm() { | ||
| 120 | - state.confirm(); | ||
| 121 | - } | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - @Override | ||
| 125 | - public void zoom(float zoom, int type) { | ||
| 126 | - state.zoom(zoom, type); | ||
| 127 | - } | ||
| 128 | - | ||
| 129 | - @Override | ||
| 130 | - public void flash(String mode) { | ||
| 131 | - state.flash(mode); | ||
| 132 | - } | ||
| 133 | - | ||
| 134 | - @Override | ||
| 135 | - public void onDestroy() { | ||
| 136 | - if (state != null) state.onDestroy(); | ||
| 137 | - } | ||
| 138 | - | ||
| 139 | - public State getState() { | ||
| 140 | - return this.state; | ||
| 141 | - } | ||
| 142 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/state/PreviewState.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.state; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.graphics.Bitmap; | ||
| 5 | -import android.util.Log; | ||
| 6 | -import android.view.Surface; | ||
| 7 | -import android.view.SurfaceHolder; | ||
| 8 | - | ||
| 9 | -import com.cjt2325.cameralibrary.CameraInterface; | ||
| 10 | -import com.cjt2325.cameralibrary.JCameraView; | ||
| 11 | -import com.cjt2325.cameralibrary.util.LogUtil; | ||
| 12 | - | ||
| 13 | -/** | ||
| 14 | - * ===================================== | ||
| 15 | - * 作 者: 陈嘉桐 | ||
| 16 | - * 版 本:1.1.4 | ||
| 17 | - * 创建日期:2017/9/8 | ||
| 18 | - * 描 述:空闲状态 | ||
| 19 | - * ===================================== | ||
| 20 | - */ | ||
| 21 | -class PreviewState implements State { | ||
| 22 | - public static final String TAG = "PreviewState"; | ||
| 23 | - | ||
| 24 | - private CameraMachine machine; | ||
| 25 | - | ||
| 26 | - PreviewState(CameraMachine machine) { | ||
| 27 | - this.machine = machine; | ||
| 28 | - } | ||
| 29 | - | ||
| 30 | - @Override | ||
| 31 | - public boolean start(SurfaceHolder holder, float screenProp) { | ||
| 32 | - return CameraInterface.getInstance().doStartPreview(holder, screenProp); | ||
| 33 | - } | ||
| 34 | - | ||
| 35 | - @Override | ||
| 36 | - public void stop() { | ||
| 37 | - CameraInterface.getInstance().doStopPreview(); | ||
| 38 | - } | ||
| 39 | - | ||
| 40 | - | ||
| 41 | - @Override | ||
| 42 | - public boolean foucs(float x, float y, CameraInterface.FocusCallback callback) { | ||
| 43 | - LogUtil.i("preview state foucs"); | ||
| 44 | - try { | ||
| 45 | - if (machine.getView().handlerFoucs(x, y)) { | ||
| 46 | - return CameraInterface.getInstance().handleFocus(machine.getContext(), x, y, callback); | ||
| 47 | - } | ||
| 48 | - } catch (Exception e) { | ||
| 49 | - Log.e("Camera", "", e); | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - return false; | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | - @Override | ||
| 56 | - public boolean swtich(SurfaceHolder holder, float screenProp) { | ||
| 57 | - return CameraInterface.getInstance().switchCamera(holder, screenProp); | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - @Override | ||
| 61 | - public void restart() { | ||
| 62 | - | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - @Override | ||
| 66 | - public boolean capture() { | ||
| 67 | - return CameraInterface.getInstance().takePicture(new CameraInterface.TakePictureCallback() { | ||
| 68 | - @Override | ||
| 69 | - public void captureResult(Bitmap bitmap, boolean isVertical) { | ||
| 70 | - machine.getView().showPicture(bitmap, isVertical); | ||
| 71 | - machine.setState(machine.getBorrowPictureState()); | ||
| 72 | - LogUtil.i("capture"); | ||
| 73 | - } | ||
| 74 | - }); | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | - @Override | ||
| 78 | - public boolean record(Surface surface, float screenProp, Context context) { | ||
| 79 | - return CameraInterface.getInstance().startRecord(surface, screenProp, null,context); | ||
| 80 | - } | ||
| 81 | - | ||
| 82 | - @Override | ||
| 83 | - public void stopRecord(final boolean isShort, long time) { | ||
| 84 | - CameraInterface.getInstance().stopRecord(isShort, new CameraInterface.StopRecordCallback() { | ||
| 85 | - @Override | ||
| 86 | - public void recordResult(String url) { | ||
| 87 | - if (isShort) { | ||
| 88 | - machine.getView().resetState(JCameraView.TYPE_SHORT); | ||
| 89 | - } else { | ||
| 90 | - machine.getView().playVideo(url); | ||
| 91 | - machine.setState(machine.getBorrowVideoState()); | ||
| 92 | - } | ||
| 93 | - } | ||
| 94 | - }); | ||
| 95 | - } | ||
| 96 | - | ||
| 97 | - @Override | ||
| 98 | - public void cancle(SurfaceHolder holder, float screenProp) { | ||
| 99 | - LogUtil.i("浏览状态下,没有 cancle 事件"); | ||
| 100 | - } | ||
| 101 | - | ||
| 102 | - @Override | ||
| 103 | - public void confirm() { | ||
| 104 | - LogUtil.i("浏览状态下,没有 confirm 事件"); | ||
| 105 | - } | ||
| 106 | - | ||
| 107 | - @Override | ||
| 108 | - public void zoom(float zoom, int type) { | ||
| 109 | - LogUtil.i(TAG, "zoom"); | ||
| 110 | - CameraInterface.getInstance().setZoom(zoom, type); | ||
| 111 | - } | ||
| 112 | - | ||
| 113 | - @Override | ||
| 114 | - public void flash(String mode) { | ||
| 115 | - CameraInterface.getInstance().setFlashMode(mode); | ||
| 116 | - } | ||
| 117 | - | ||
| 118 | - @Override | ||
| 119 | - public void onDestroy() { | ||
| 120 | - CameraInterface.getInstance().onDestroy(); | ||
| 121 | - } | ||
| 122 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/state/State.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.state; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.view.Surface; | ||
| 5 | -import android.view.SurfaceHolder; | ||
| 6 | - | ||
| 7 | -import com.cjt2325.cameralibrary.CameraInterface; | ||
| 8 | - | ||
| 9 | -/** | ||
| 10 | - * ===================================== | ||
| 11 | - * 作 者: 陈嘉桐 | ||
| 12 | - * 版 本:1.1.4 | ||
| 13 | - * 创建日期:2017/9/8 | ||
| 14 | - * 描 述: | ||
| 15 | - * ===================================== | ||
| 16 | - */ | ||
| 17 | -public interface State { | ||
| 18 | - | ||
| 19 | - boolean start(SurfaceHolder holder, float screenProp); | ||
| 20 | - | ||
| 21 | - void stop(); | ||
| 22 | - | ||
| 23 | - boolean foucs(float x, float y, CameraInterface.FocusCallback callback); | ||
| 24 | - | ||
| 25 | - boolean swtich(SurfaceHolder holder, float screenProp); | ||
| 26 | - | ||
| 27 | - void restart(); | ||
| 28 | - | ||
| 29 | - boolean capture(); | ||
| 30 | - | ||
| 31 | - boolean record(Surface surface, float screenProp, Context context); | ||
| 32 | - | ||
| 33 | - void stopRecord(boolean isShort, long time); | ||
| 34 | - | ||
| 35 | - void cancle(SurfaceHolder holder, float screenProp); | ||
| 36 | - | ||
| 37 | - void confirm(); | ||
| 38 | - | ||
| 39 | - void zoom(float zoom, int type); | ||
| 40 | - | ||
| 41 | - void flash(String mode); | ||
| 42 | - | ||
| 43 | - void onDestroy(); | ||
| 44 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/util/AngleUtil.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.util; | ||
| 2 | - | ||
| 3 | -/** | ||
| 4 | - * ===================================== | ||
| 5 | - * 作 者: 陈嘉桐 | ||
| 6 | - * 版 本:1.1.4 | ||
| 7 | - * 创建日期:2017/5/2 | ||
| 8 | - * 描 述: | ||
| 9 | - * ===================================== | ||
| 10 | - */ | ||
| 11 | -public class AngleUtil { | ||
| 12 | - public static int getSensorAngle(float x, float y) { | ||
| 13 | - if (Math.abs(x) > Math.abs(y)) { | ||
| 14 | - /** | ||
| 15 | - * 横屏倾斜角度比较大 | ||
| 16 | - */ | ||
| 17 | - if (x > 4) { | ||
| 18 | - /** | ||
| 19 | - * 左边倾斜 | ||
| 20 | - */ | ||
| 21 | - return 270; | ||
| 22 | - } else if (x < -4) { | ||
| 23 | - /** | ||
| 24 | - * 右边倾斜 | ||
| 25 | - */ | ||
| 26 | - return 90; | ||
| 27 | - } else { | ||
| 28 | - /** | ||
| 29 | - * 倾斜角度不够大 | ||
| 30 | - */ | ||
| 31 | - return 0; | ||
| 32 | - } | ||
| 33 | - } else { | ||
| 34 | - if (y > 7) { | ||
| 35 | - /** | ||
| 36 | - * 左边倾斜 | ||
| 37 | - */ | ||
| 38 | - return 0; | ||
| 39 | - } else if (y < -7) { | ||
| 40 | - /** | ||
| 41 | - * 右边倾斜 | ||
| 42 | - */ | ||
| 43 | - return 180; | ||
| 44 | - } else { | ||
| 45 | - /** | ||
| 46 | - * 倾斜角度不够大 | ||
| 47 | - */ | ||
| 48 | - return 0; | ||
| 49 | - } | ||
| 50 | - } | ||
| 51 | - } | ||
| 52 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/util/AudioUtil.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.util; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.media.AudioManager; | ||
| 5 | - | ||
| 6 | -/** | ||
| 7 | - * ===================================== | ||
| 8 | - * 作 者: 陈嘉桐 | ||
| 9 | - * 版 本:1.1.4 | ||
| 10 | - * 创建日期:2017/4/26 | ||
| 11 | - * 描 述: | ||
| 12 | - * ===================================== | ||
| 13 | - */ | ||
| 14 | -public class AudioUtil { | ||
| 15 | - public static void setAudioManage(Context context) { | ||
| 16 | - AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); | ||
| 17 | - audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true); | ||
| 18 | - audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true); | ||
| 19 | - audioManager.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0); | ||
| 20 | - audioManager.setStreamVolume(AudioManager.STREAM_DTMF, 0, 0); | ||
| 21 | - audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, 0); | ||
| 22 | - audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0); | ||
| 23 | - } | ||
| 24 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/util/CameraParamUtil.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.util; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.hardware.Camera; | ||
| 5 | -import android.util.Log; | ||
| 6 | -import android.view.Surface; | ||
| 7 | -import android.view.WindowManager; | ||
| 8 | - | ||
| 9 | -import com.cnlive.libs.base.util.AlertUtil; | ||
| 10 | - | ||
| 11 | -import java.util.Collections; | ||
| 12 | -import java.util.Comparator; | ||
| 13 | -import java.util.List; | ||
| 14 | - | ||
| 15 | -/** | ||
| 16 | - * ===================================== | ||
| 17 | - * 作 者: 陈嘉桐 | ||
| 18 | - * 版 本:1.1.4 | ||
| 19 | - * 创建日期:2017/4/25 | ||
| 20 | - * 描 述: | ||
| 21 | - * ===================================== | ||
| 22 | - */ | ||
| 23 | -public class CameraParamUtil { | ||
| 24 | - private static final String TAG = "JCameraView"; | ||
| 25 | - private CameraSizeComparator sizeComparator = new CameraSizeComparator(); | ||
| 26 | - private static CameraParamUtil cameraParamUtil = null; | ||
| 27 | - | ||
| 28 | - private CameraParamUtil() { | ||
| 29 | - | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - public static CameraParamUtil getInstance() { | ||
| 33 | - if (cameraParamUtil == null) { | ||
| 34 | - cameraParamUtil = new CameraParamUtil(); | ||
| 35 | - return cameraParamUtil; | ||
| 36 | - } else { | ||
| 37 | - return cameraParamUtil; | ||
| 38 | - } | ||
| 39 | - } | ||
| 40 | - | ||
| 41 | - public Camera.Size getPreviewSize(List<Camera.Size> list, int th, float rate) { | ||
| 42 | - Collections.sort(list, sizeComparator); | ||
| 43 | - int i = 0; | ||
| 44 | - for (Camera.Size s : list) { | ||
| 45 | - if ((s.width > th) && equalRate(s, rate)) { | ||
| 46 | - Log.i(TAG, "MakeSure Preview :w = " + s.width + " h = " + s.height); | ||
| 47 | - break; | ||
| 48 | - } | ||
| 49 | - i++; | ||
| 50 | - } | ||
| 51 | - if (i == list.size()) { | ||
| 52 | - return getBestSize(list, rate); | ||
| 53 | - } else { | ||
| 54 | - return list.get(i); | ||
| 55 | - } | ||
| 56 | - } | ||
| 57 | - | ||
| 58 | - public Camera.Size getPictureSize(List<Camera.Size> list, int th, float rate) { | ||
| 59 | - Collections.sort(list, sizeComparator); | ||
| 60 | - int i = 0; | ||
| 61 | - for (Camera.Size s : list) { | ||
| 62 | - if ((s.width > th) && equalRate(s, rate)) { | ||
| 63 | - Log.i(TAG, "MakeSure Picture :w = " + s.width + " h = " + s.height); | ||
| 64 | - break; | ||
| 65 | - } | ||
| 66 | - i++; | ||
| 67 | - } | ||
| 68 | - if (i == list.size()) { | ||
| 69 | - return getBestSize(list, rate); | ||
| 70 | - } else { | ||
| 71 | - return list.get(i); | ||
| 72 | - } | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | - private Camera.Size getBestSize(List<Camera.Size> list, float rate) { | ||
| 76 | - float previewDisparity = 100; | ||
| 77 | - int index = 0; | ||
| 78 | - for (int i = 0; i < list.size(); i++) { | ||
| 79 | - Camera.Size cur = list.get(i); | ||
| 80 | - float prop = (float) cur.width / (float) cur.height; | ||
| 81 | - if (Math.abs(rate - prop) < previewDisparity) { | ||
| 82 | - previewDisparity = Math.abs(rate - prop); | ||
| 83 | - index = i; | ||
| 84 | - } | ||
| 85 | - } | ||
| 86 | - return list.get(index); | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - private boolean equalRate(Camera.Size s, float rate) { | ||
| 91 | - float r = (float) (s.width) / (float) (s.height); | ||
| 92 | - return Math.abs(r - rate) <= 0.2; | ||
| 93 | - } | ||
| 94 | - | ||
| 95 | - public boolean isSupportedFocusMode(List<String> focusList, String focusMode) { | ||
| 96 | - for (int i = 0; i < focusList.size(); i++) { | ||
| 97 | - if (focusMode.equals(focusList.get(i))) { | ||
| 98 | - Log.i(TAG, "FocusMode supported " + focusMode); | ||
| 99 | - return true; | ||
| 100 | - } | ||
| 101 | - } | ||
| 102 | - Log.i(TAG, "FocusMode not supported " + focusMode); | ||
| 103 | - return false; | ||
| 104 | - } | ||
| 105 | - | ||
| 106 | - public boolean isSupportedPictureFormats(List<Integer> supportedPictureFormats, int jpeg) { | ||
| 107 | - for (int i = 0; i < supportedPictureFormats.size(); i++) { | ||
| 108 | - if (jpeg == supportedPictureFormats.get(i)) { | ||
| 109 | - Log.i(TAG, "Formats supported " + jpeg); | ||
| 110 | - return true; | ||
| 111 | - } | ||
| 112 | - } | ||
| 113 | - Log.i(TAG, "Formats not supported " + jpeg); | ||
| 114 | - return false; | ||
| 115 | - } | ||
| 116 | - | ||
| 117 | - private class CameraSizeComparator implements Comparator<Camera.Size> { | ||
| 118 | - public int compare(Camera.Size lhs, Camera.Size rhs) { | ||
| 119 | - if (lhs.width == rhs.width) { | ||
| 120 | - return 0; | ||
| 121 | - } else if (lhs.width > rhs.width) { | ||
| 122 | - return 1; | ||
| 123 | - } else { | ||
| 124 | - return -1; | ||
| 125 | - } | ||
| 126 | - } | ||
| 127 | - | ||
| 128 | - } | ||
| 129 | - | ||
| 130 | - public int getCameraDisplayOrientation(Context context, int cameraId) { | ||
| 131 | - Camera.CameraInfo info = new Camera.CameraInfo(); | ||
| 132 | - try { | ||
| 133 | - Camera.getCameraInfo(cameraId, info); | ||
| 134 | - } catch (Exception e) { | ||
| 135 | - Log.e("CameraInterface", "像机ID识别错误", e); | ||
| 136 | - AlertUtil.showFailedToast(context,"像机ID识别错误"+cameraId); | ||
| 137 | - } | ||
| 138 | - WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | ||
| 139 | - int rotation = wm.getDefaultDisplay().getRotation(); | ||
| 140 | - int degrees = 0; | ||
| 141 | - switch (rotation) { | ||
| 142 | - case Surface.ROTATION_0: | ||
| 143 | - degrees = 0; | ||
| 144 | - break; | ||
| 145 | - case Surface.ROTATION_90: | ||
| 146 | - degrees = 90; | ||
| 147 | - break; | ||
| 148 | - case Surface.ROTATION_180: | ||
| 149 | - degrees = 180; | ||
| 150 | - break; | ||
| 151 | - case Surface.ROTATION_270: | ||
| 152 | - degrees = 270; | ||
| 153 | - break; | ||
| 154 | - } | ||
| 155 | - int result; | ||
| 156 | - if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { | ||
| 157 | - result = (info.orientation + degrees) % 360; | ||
| 158 | - result = (360 - result) % 360; // compensate the mirror | ||
| 159 | - } else { | ||
| 160 | - // back-facing | ||
| 161 | - result = (info.orientation - degrees + 360) % 360; | ||
| 162 | - } | ||
| 163 | - return result; | ||
| 164 | - } | ||
| 165 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/util/CheckPermission.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.util; | ||
| 2 | - | ||
| 3 | -import android.hardware.Camera; | ||
| 4 | -import android.media.AudioFormat; | ||
| 5 | -import android.media.AudioRecord; | ||
| 6 | -import android.media.MediaRecorder; | ||
| 7 | -import android.util.Log; | ||
| 8 | - | ||
| 9 | -/** | ||
| 10 | - * ===================================== | ||
| 11 | - * 作 者: 陈嘉桐 | ||
| 12 | - * 版 本:1.1.4 | ||
| 13 | - * 创建日期:2017/6/8 | ||
| 14 | - * 描 述: | ||
| 15 | - * ===================================== | ||
| 16 | - */ | ||
| 17 | -public class CheckPermission { | ||
| 18 | - public static final int STATE_RECORDING = -1; | ||
| 19 | - public static final int STATE_NO_PERMISSION = -2; | ||
| 20 | - public static final int STATE_SUCCESS = 1; | ||
| 21 | - | ||
| 22 | - /** | ||
| 23 | - * 用于检测是否具有录音权限 | ||
| 24 | - * | ||
| 25 | - * @return | ||
| 26 | - */ | ||
| 27 | - public static int getRecordState() { | ||
| 28 | - AudioRecord audioRecord =null; | ||
| 29 | - int minBuffer = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat | ||
| 30 | - .ENCODING_PCM_16BIT); | ||
| 31 | - try { | ||
| 32 | - audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat | ||
| 33 | - .CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 100)); | ||
| 34 | - }catch (Exception e){ | ||
| 35 | - | ||
| 36 | - }; | ||
| 37 | - if(audioRecord==null){ | ||
| 38 | - return STATE_NO_PERMISSION; | ||
| 39 | - } | ||
| 40 | - short[] point = new short[minBuffer]; | ||
| 41 | - int readSize = 0; | ||
| 42 | - try { | ||
| 43 | - | ||
| 44 | - audioRecord.startRecording();//检测是否可以进入初始化状态 | ||
| 45 | - } catch (Exception e) { | ||
| 46 | - if (audioRecord != null) { | ||
| 47 | - audioRecord.release(); | ||
| 48 | - audioRecord = null; | ||
| 49 | - } | ||
| 50 | - return STATE_NO_PERMISSION; | ||
| 51 | - } | ||
| 52 | - if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) { | ||
| 53 | - //6.0以下机型都会返回此状态,故使用时需要判断bulid版本 | ||
| 54 | - //检测是否在录音中 | ||
| 55 | - if (audioRecord != null) { | ||
| 56 | - audioRecord.stop(); | ||
| 57 | - audioRecord.release(); | ||
| 58 | - audioRecord = null; | ||
| 59 | - Log.d("CheckAudioPermission", "录音机被占用"); | ||
| 60 | - } | ||
| 61 | - return STATE_RECORDING; | ||
| 62 | - } else { | ||
| 63 | - //检测是否可以获取录音结果 | ||
| 64 | - | ||
| 65 | - readSize = audioRecord.read(point, 0, point.length); | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - if (readSize <= 0) { | ||
| 69 | - if (audioRecord != null) { | ||
| 70 | - audioRecord.stop(); | ||
| 71 | - audioRecord.release(); | ||
| 72 | - audioRecord = null; | ||
| 73 | - | ||
| 74 | - } | ||
| 75 | - Log.d("CheckAudioPermission", "录音的结果为空"); | ||
| 76 | - return STATE_NO_PERMISSION; | ||
| 77 | - | ||
| 78 | - } else { | ||
| 79 | - if (audioRecord != null) { | ||
| 80 | - audioRecord.stop(); | ||
| 81 | - audioRecord.release(); | ||
| 82 | - audioRecord = null; | ||
| 83 | - | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | - return STATE_SUCCESS; | ||
| 87 | - } | ||
| 88 | - } | ||
| 89 | - } | ||
| 90 | - | ||
| 91 | - public synchronized static boolean isCameraUseable(int cameraID) { | ||
| 92 | - boolean canUse = true; | ||
| 93 | - Camera mCamera = null; | ||
| 94 | - try { | ||
| 95 | - mCamera = Camera.open(cameraID); | ||
| 96 | - // setParameters 是针对魅族MX5。MX5通过Camera.open()拿到的Camera对象不为null | ||
| 97 | - Camera.Parameters mParameters = mCamera.getParameters(); | ||
| 98 | - mCamera.setParameters(mParameters); | ||
| 99 | - } catch (Exception e) { | ||
| 100 | - e.printStackTrace(); | ||
| 101 | - canUse = false; | ||
| 102 | - } finally { | ||
| 103 | - if (mCamera != null) { | ||
| 104 | - mCamera.release(); | ||
| 105 | - } else { | ||
| 106 | - canUse = false; | ||
| 107 | - } | ||
| 108 | - mCamera = null; | ||
| 109 | - } | ||
| 110 | - return canUse; | ||
| 111 | - } | ||
| 112 | -} | ||
| 113 | \ No newline at end of file | 0 | \ No newline at end of file |
core/base/src/main/java/com/cjt2325/cameralibrary/util/DeviceUtil.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.util; | ||
| 2 | - | ||
| 3 | -import android.os.Build; | ||
| 4 | - | ||
| 5 | -/** | ||
| 6 | - * ===================================== | ||
| 7 | - * 作 者: 陈嘉桐 | ||
| 8 | - * 版 本:1.1.4 | ||
| 9 | - * 创建日期:2017/6/9 | ||
| 10 | - * 描 述: | ||
| 11 | - * ===================================== | ||
| 12 | - */ | ||
| 13 | -public class DeviceUtil { | ||
| 14 | - | ||
| 15 | - private static String[] huaweiRongyao = { | ||
| 16 | - "hwH60", //荣耀6 | ||
| 17 | - "hwPE", //荣耀6 plus | ||
| 18 | - "hwH30", //3c | ||
| 19 | - "hwHol", //3c畅玩版 | ||
| 20 | - "hwG750", //3x | ||
| 21 | - "hw7D", //x1 | ||
| 22 | - "hwChe2", //x1 | ||
| 23 | - }; | ||
| 24 | - | ||
| 25 | - public static String getDeviceInfo() { | ||
| 26 | - String handSetInfo = | ||
| 27 | - "手机型号:" + Build.DEVICE + | ||
| 28 | - "\n系统版本:" + Build.VERSION.RELEASE + | ||
| 29 | - "\nSDK版本:" + Build.VERSION.SDK_INT; | ||
| 30 | - return handSetInfo; | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - public static String getDeviceModel() { | ||
| 34 | - return Build.DEVICE; | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - public static boolean isHuaWeiRongyao() { | ||
| 38 | - int length = huaweiRongyao.length; | ||
| 39 | - for (int i = 0; i < length; i++) { | ||
| 40 | - if (huaweiRongyao[i].equals(getDeviceModel())) { | ||
| 41 | - return true; | ||
| 42 | - } | ||
| 43 | - } | ||
| 44 | - return false; | ||
| 45 | - } | ||
| 46 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/util/FileUtil.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.util; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.graphics.Bitmap; | ||
| 5 | -import android.os.Build; | ||
| 6 | -import android.os.Environment; | ||
| 7 | - | ||
| 8 | -import java.io.BufferedOutputStream; | ||
| 9 | -import java.io.File; | ||
| 10 | -import java.io.FileOutputStream; | ||
| 11 | -import java.io.IOException; | ||
| 12 | - | ||
| 13 | -/** | ||
| 14 | - * ===================================== | ||
| 15 | - * 作 者: 陈嘉桐 | ||
| 16 | - * 版 本:1.1.4 | ||
| 17 | - * 创建日期:2017/4/25 | ||
| 18 | - * 描 述: | ||
| 19 | - * ===================================== | ||
| 20 | - */ | ||
| 21 | -public class FileUtil { | ||
| 22 | - private static final String TAG = "CJT"; | ||
| 23 | - private static String storagePath = ""; | ||
| 24 | - private static String DST_FOLDER_NAME = "JCamera"; | ||
| 25 | - | ||
| 26 | - private static String initPath(Context context) { | ||
| 27 | - if (storagePath.equals("")) { | ||
| 28 | - storagePath = (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q ? context.getExternalFilesDir("") : Environment.getExternalStorageDirectory()).getAbsolutePath() + File.separator + DST_FOLDER_NAME; | ||
| 29 | - File f = new File(storagePath); | ||
| 30 | - if (!f.exists()) { | ||
| 31 | - f.mkdir(); | ||
| 32 | - } | ||
| 33 | - } | ||
| 34 | - return storagePath; | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - public static String saveBitmap(Context context,String dir, Bitmap b) { | ||
| 38 | - DST_FOLDER_NAME = dir; | ||
| 39 | - | ||
| 40 | - String path = initPath(context); | ||
| 41 | - long dataTake = System.currentTimeMillis(); | ||
| 42 | - String jpegName = path + File.separator + "picture_" + dataTake + ".jpg"; | ||
| 43 | - try { | ||
| 44 | - FileOutputStream fout = new FileOutputStream(jpegName); | ||
| 45 | - BufferedOutputStream bos = new BufferedOutputStream(fout); | ||
| 46 | - b.compress(Bitmap.CompressFormat.JPEG, 100, bos); | ||
| 47 | - bos.flush(); | ||
| 48 | - bos.close(); | ||
| 49 | - return jpegName; | ||
| 50 | - } catch (IOException e) { | ||
| 51 | - e.printStackTrace(); | ||
| 52 | - return ""; | ||
| 53 | - } | ||
| 54 | - } | ||
| 55 | - | ||
| 56 | - public static boolean deleteFile(String url) { | ||
| 57 | - boolean result = false; | ||
| 58 | - File file = new File(url); | ||
| 59 | - if (file.exists()) { | ||
| 60 | - result = file.delete(); | ||
| 61 | - } | ||
| 62 | - return result; | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - public static boolean isExternalStorageWritable() { | ||
| 66 | - String state = Environment.getExternalStorageState(); | ||
| 67 | - if (Environment.MEDIA_MOUNTED.equals(state)) { | ||
| 68 | - return true; | ||
| 69 | - } | ||
| 70 | - return false; | ||
| 71 | - } | ||
| 72 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/util/LogUtil.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.util; | ||
| 2 | - | ||
| 3 | -import android.util.Log; | ||
| 4 | - | ||
| 5 | - | ||
| 6 | -/** | ||
| 7 | - * ===================================== | ||
| 8 | - * 作 者: 陈嘉桐 | ||
| 9 | - * 版 本:1.1.4 | ||
| 10 | - * 创建日期:2017/9/7 | ||
| 11 | - * 描 述: | ||
| 12 | - * ===================================== | ||
| 13 | - */ | ||
| 14 | -public class LogUtil { | ||
| 15 | - | ||
| 16 | - private static final String DEFAULT_TAG = "CJT"; | ||
| 17 | - | ||
| 18 | - public static void i(String tag, String msg) { | ||
| 19 | -// if (DEBUG) | ||
| 20 | - Log.i(tag, msg); | ||
| 21 | - } | ||
| 22 | - | ||
| 23 | - public static void v(String tag, String msg) { | ||
| 24 | - Log.v(tag, msg); | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - public static void d(String tag, String msg) { | ||
| 28 | - Log.d(tag, msg); | ||
| 29 | - } | ||
| 30 | - | ||
| 31 | - public static void e(String tag, String msg) { | ||
| 32 | - Log.e(tag, msg); | ||
| 33 | - } | ||
| 34 | - | ||
| 35 | - public static void i(String msg) { | ||
| 36 | - i(DEFAULT_TAG, msg); | ||
| 37 | - } | ||
| 38 | - | ||
| 39 | - public static void v(String msg) { | ||
| 40 | - v(DEFAULT_TAG, msg); | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - public static void d(String msg) { | ||
| 44 | - d(DEFAULT_TAG, msg); | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - public static void e(String msg) { | ||
| 48 | - e(DEFAULT_TAG, msg); | ||
| 49 | - } | ||
| 50 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/util/ScreenUtils.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.util; | ||
| 2 | - | ||
| 3 | -import android.content.Context; | ||
| 4 | -import android.util.DisplayMetrics; | ||
| 5 | -import android.util.TypedValue; | ||
| 6 | -import android.view.WindowManager; | ||
| 7 | - | ||
| 8 | -/** | ||
| 9 | - * ===================================== | ||
| 10 | - * 作 者: 陈嘉桐 | ||
| 11 | - * 版 本:1.1.4 | ||
| 12 | - * 创建日期:2017/5/25 | ||
| 13 | - * 描 述: | ||
| 14 | - * ===================================== | ||
| 15 | - */ | ||
| 16 | -public class ScreenUtils { | ||
| 17 | - public static int getScreenHeight(Context context) { | ||
| 18 | - DisplayMetrics metric = new DisplayMetrics(); | ||
| 19 | - WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | ||
| 20 | - wm.getDefaultDisplay().getMetrics(metric); | ||
| 21 | - return metric.heightPixels; | ||
| 22 | - } | ||
| 23 | - | ||
| 24 | - public static int getScreenWidth(Context context) { | ||
| 25 | - DisplayMetrics metric = new DisplayMetrics(); | ||
| 26 | - WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | ||
| 27 | - wm.getDefaultDisplay().getMetrics(metric); | ||
| 28 | - return metric.widthPixels; | ||
| 29 | - } | ||
| 30 | - /** | ||
| 31 | - * dp转px | ||
| 32 | - * | ||
| 33 | - * @param context | ||
| 34 | - * @param dpVal | ||
| 35 | - * @return | ||
| 36 | - */ | ||
| 37 | - public static int dp2px(Context context, float dpVal) { | ||
| 38 | - return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, | ||
| 39 | - dpVal, context.getResources().getDisplayMetrics()); | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - /** | ||
| 43 | - * sp转px | ||
| 44 | - * | ||
| 45 | - * @param context | ||
| 46 | - * @param spVal | ||
| 47 | - * @return | ||
| 48 | - */ | ||
| 49 | - public static int sp2px(Context context, float spVal) { | ||
| 50 | - return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, | ||
| 51 | - spVal, context.getResources().getDisplayMetrics()); | ||
| 52 | - } | ||
| 53 | - | ||
| 54 | - /** | ||
| 55 | - * px转dp | ||
| 56 | - * | ||
| 57 | - * @param context | ||
| 58 | - * @param pxVal | ||
| 59 | - * @return | ||
| 60 | - */ | ||
| 61 | - public static float px2dp(Context context, float pxVal) { | ||
| 62 | - final float scale = context.getResources().getDisplayMetrics().density; | ||
| 63 | - return (pxVal / scale); | ||
| 64 | - } | ||
| 65 | - | ||
| 66 | - /** | ||
| 67 | - * px转sp | ||
| 68 | - * | ||
| 69 | - * @param context | ||
| 70 | - * @param pxVal | ||
| 71 | - * @return | ||
| 72 | - */ | ||
| 73 | - public static float px2sp(Context context, float pxVal) { | ||
| 74 | - return (pxVal / context.getResources().getDisplayMetrics().scaledDensity); | ||
| 75 | - } | ||
| 76 | -} |
core/base/src/main/java/com/cjt2325/cameralibrary/view/CameraView.java deleted
100644 → 0
| 1 | -package com.cjt2325.cameralibrary.view; | ||
| 2 | - | ||
| 3 | -import android.graphics.Bitmap; | ||
| 4 | - | ||
| 5 | -/** | ||
| 6 | - * ===================================== | ||
| 7 | - * 作 者: 陈嘉桐 | ||
| 8 | - * 版 本:1.1.4 | ||
| 9 | - * 创建日期:2017/9/8 | ||
| 10 | - * 描 述: | ||
| 11 | - * ===================================== | ||
| 12 | - */ | ||
| 13 | -public interface CameraView { | ||
| 14 | - void resetState(int type); | ||
| 15 | - | ||
| 16 | - void confirmState(int type); | ||
| 17 | - | ||
| 18 | - void showPicture(Bitmap bitmap, boolean isVertical); | ||
| 19 | - | ||
| 20 | - void playVideo(String url); | ||
| 21 | - | ||
| 22 | - void stopVideo(); | ||
| 23 | - | ||
| 24 | - void setTip(String tip); | ||
| 25 | - | ||
| 26 | - void startPreviewCallback(); | ||
| 27 | - | ||
| 28 | - boolean handlerFoucs(float x, float y); | ||
| 29 | - | ||
| 30 | - void pausePlayer(); | ||
| 31 | - void resumePlayer(); | ||
| 32 | -} |
core/base/src/main/java/com/cnlive/libs/base/frame/activity/BaseActivity.java
| @@ -4,7 +4,6 @@ package com.cnlive.libs.base.frame.activity; | @@ -4,7 +4,6 @@ package com.cnlive.libs.base.frame.activity; | ||
| 4 | import android.annotation.SuppressLint; | 4 | import android.annotation.SuppressLint; |
| 5 | import android.content.Context; | 5 | import android.content.Context; |
| 6 | import android.content.pm.ActivityInfo; | 6 | import android.content.pm.ActivityInfo; |
| 7 | -import android.database.DatabaseUtils; | ||
| 8 | import android.os.Bundle; | 7 | import android.os.Bundle; |
| 9 | import android.os.Looper; | 8 | import android.os.Looper; |
| 10 | import android.view.View; | 9 | import android.view.View; |
| @@ -23,7 +22,6 @@ import androidx.databinding.ViewDataBinding; | @@ -23,7 +22,6 @@ import androidx.databinding.ViewDataBinding; | ||
| 23 | 22 | ||
| 24 | import com.cnlive.libs.base.util.StatusBarConfig; | 23 | import com.cnlive.libs.base.util.StatusBarConfig; |
| 25 | import com.cnlive.libs.base.util.StatusBarUtil; | 24 | import com.cnlive.libs.base.util.StatusBarUtil; |
| 26 | -import com.cnlive.libs.base.util.lib_permisshelper.PermissionsUtils; | ||
| 27 | import com.cnlive.libs.base.util.ActivityCollector; | 25 | import com.cnlive.libs.base.util.ActivityCollector; |
| 28 | 26 | ||
| 29 | /** | 27 | /** |
| @@ -42,7 +40,6 @@ public abstract class BaseActivity extends AppCompatActivity { | @@ -42,7 +40,6 @@ public abstract class BaseActivity extends AppCompatActivity { | ||
| 42 | //封装Toast对象 | 40 | //封装Toast对象 |
| 43 | private static Toast toast; | 41 | private static Toast toast; |
| 44 | public Context context; | 42 | public Context context; |
| 45 | - private PermissionsUtils mPermissionsUtils;//权限申请 | ||
| 46 | private int stateBarColor = -1; | 43 | private int stateBarColor = -1; |
| 47 | public ViewDataBinding binding; | 44 | public ViewDataBinding binding; |
| 48 | 45 | ||
| @@ -68,7 +65,6 @@ public abstract class BaseActivity extends AppCompatActivity { | @@ -68,7 +65,6 @@ public abstract class BaseActivity extends AppCompatActivity { | ||
| 68 | } else { | 65 | } else { |
| 69 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | 66 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
| 70 | } | 67 | } |
| 71 | - mPermissionsUtils = new PermissionsUtils();//权限申请 | ||
| 72 | //初始化控件 | 68 | //初始化控件 |
| 73 | initView(); | 69 | initView(); |
| 74 | //设置数据 | 70 | //设置数据 |
core/base/src/main/java/com/cnlive/libs/base/logic/Config.java deleted
100644 → 0
| 1 | -package com.cnlive.libs.base.logic; | ||
| 2 | - | ||
| 3 | -/** | ||
| 4 | - * Created by xiansong on 2018/1/4. | ||
| 5 | - */ | ||
| 6 | - | ||
| 7 | -public class Config { | ||
| 8 | - | ||
| 9 | - //成功状态 | ||
| 10 | - public static final int SUCCESS = 0; | ||
| 11 | - //失败状态 | ||
| 12 | - public static final int FAIL = -1; | ||
| 13 | - //系统错误 | ||
| 14 | - public static final int ERROR_SYSTEM = 2003; | ||
| 15 | - //上传/下载错误 | ||
| 16 | - public static final int ERROR_UPLOAD = 2004; | ||
| 17 | - //空闲状态 | ||
| 18 | - public static final int STATE_IDLE = 3001; | ||
| 19 | - //加载状态 | ||
| 20 | - public static final int STATE_LOAD = 3002; | ||
| 21 | - | ||
| 22 | - public static final String ERROR_DEF = "%S_ERROR_%s code:%s message:%s"; | ||
| 23 | -} |
core/base/src/main/java/com/cnlive/libs/base/logic/Event.java deleted
100644 → 0
| 1 | -package com.cnlive.libs.base.logic; | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - | ||
| 5 | -import com.cnlive.libs.base.logic.callback.Callback; | ||
| 6 | -import com.cnlive.libs.base.logic.callback.FailureCallback; | ||
| 7 | -import com.cnlive.libs.base.logic.callback.CancelCallback; | ||
| 8 | -import com.cnlive.libs.base.logic.callback.ProgressCallback; | ||
| 9 | -import com.cnlive.libs.base.logic.callback.StateCallback; | ||
| 10 | -import com.cnlive.libs.base.logic.callback.SuccessCallback; | ||
| 11 | - | ||
| 12 | -import java.util.ArrayList; | ||
| 13 | -import java.util.HashMap; | ||
| 14 | -import java.util.List; | ||
| 15 | -import java.util.Map; | ||
| 16 | - | ||
| 17 | -import io.reactivex.disposables.Disposable; | ||
| 18 | - | ||
| 19 | -/** | ||
| 20 | - * Created by xiansong on 2018/1/8. | ||
| 21 | - */ | ||
| 22 | - | ||
| 23 | -public class Event<T> { | ||
| 24 | - | ||
| 25 | - private static final int success_call = 1; | ||
| 26 | - private static final int failure_call = 2; | ||
| 27 | - private static final int state_call = 3; | ||
| 28 | - private static final int cancel_call = 4; | ||
| 29 | - private static final int progress_call = 5; | ||
| 30 | - | ||
| 31 | - public boolean active = true; | ||
| 32 | - private Logic logic; | ||
| 33 | - private List<Disposable> disposables = new ArrayList<>(); | ||
| 34 | - private Map<Integer, Callback> callbackArray; | ||
| 35 | - | ||
| 36 | - public Event() { | ||
| 37 | - this.callbackArray = new HashMap<>(); | ||
| 38 | - } | ||
| 39 | - | ||
| 40 | - public void setLogic(Logic logic) { | ||
| 41 | - this.logic = logic; | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - public Event<T> setSuccessCallback(SuccessCallback<T> callback) { | ||
| 45 | - callbackArray.put(success_call, callback); | ||
| 46 | - return this; | ||
| 47 | - } | ||
| 48 | - | ||
| 49 | - public Event<T> setFailureCallback(FailureCallback callback) { | ||
| 50 | - callbackArray.put(failure_call, callback); | ||
| 51 | - return this; | ||
| 52 | - } | ||
| 53 | - | ||
| 54 | - public Event<T> setStateCallback(StateCallback callback) { | ||
| 55 | - callbackArray.put(state_call, callback); | ||
| 56 | - return this; | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - public Event<T> setCancelCallback(CancelCallback callback) { | ||
| 60 | - callbackArray.put(cancel_call, callback); | ||
| 61 | - return this; | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - public Event<T> setProgressCallback(ProgressCallback callback) { | ||
| 65 | - callbackArray.put(progress_call, callback); | ||
| 66 | - return this; | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | - public Event<T> addDisposable(Disposable disposable) { | ||
| 70 | - if (disposable != null) disposables.add(disposable); | ||
| 71 | - return this; | ||
| 72 | - } | ||
| 73 | - | ||
| 74 | - public void onSuccess(T data) { | ||
| 75 | - active = false; | ||
| 76 | - SuccessCallback<T> callback = (SuccessCallback<T>) callbackArray.get(success_call); | ||
| 77 | - if (callback != null) callback.onSuccess(data); | ||
| 78 | - } | ||
| 79 | - | ||
| 80 | - public void onFailure(int state, String message) { | ||
| 81 | - active = false; | ||
| 82 | - FailureCallback callback = (FailureCallback) callbackArray.get(failure_call); | ||
| 83 | - if (callback != null) callback.onFailure(state, message); | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | - public void onState(int state) { | ||
| 87 | - StateCallback callback = (StateCallback) callbackArray.get(state_call); | ||
| 88 | - if (callback != null) callback.onState(state); | ||
| 89 | - } | ||
| 90 | - | ||
| 91 | - public void onCancle() { | ||
| 92 | - CancelCallback callback = (CancelCallback) callbackArray.get(cancel_call); | ||
| 93 | - if (callback != null) callback.onCancel(); | ||
| 94 | - destroy(); | ||
| 95 | - } | ||
| 96 | - | ||
| 97 | - public void onProgress(double progress) { | ||
| 98 | - ProgressCallback callback = (ProgressCallback) callbackArray.get(progress_call); | ||
| 99 | - if (callback != null) callback.onProgress(progress); | ||
| 100 | - } | ||
| 101 | - | ||
| 102 | - public void cencel() { | ||
| 103 | - onCancle(); | ||
| 104 | - } | ||
| 105 | - | ||
| 106 | - private void destroy() { | ||
| 107 | - active = false; | ||
| 108 | - callbackArray.clear(); | ||
| 109 | - if (disposables != null) for (Disposable disposable : disposables) disposable.dispose(); | ||
| 110 | - } | ||
| 111 | - | ||
| 112 | - public Event<T> start() { | ||
| 113 | - if (logic != null) logic.start(this); | ||
| 114 | - return this; | ||
| 115 | - } | ||
| 116 | -} |
core/base/src/main/java/com/cnlive/libs/base/logic/Logic.java deleted
100644 → 0
| 1 | -package com.cnlive.libs.base.logic; | ||
| 2 | - | ||
| 3 | - | ||
| 4 | -import android.util.Log; | ||
| 5 | - | ||
| 6 | -import com.cnlive.libs.base.logic.callback.DataCallback; | ||
| 7 | - | ||
| 8 | -import java.util.ArrayList; | ||
| 9 | -import java.util.HashMap; | ||
| 10 | -import java.util.List; | ||
| 11 | -import java.util.Map; | ||
| 12 | - | ||
| 13 | -import io.reactivex.Observable; | ||
| 14 | -import io.reactivex.ObservableOnSubscribe; | ||
| 15 | -import io.reactivex.android.schedulers.AndroidSchedulers; | ||
| 16 | -import io.reactivex.disposables.Disposable; | ||
| 17 | -import io.reactivex.functions.Consumer; | ||
| 18 | -import io.reactivex.schedulers.Schedulers; | ||
| 19 | - | ||
| 20 | -public class Logic { | ||
| 21 | - private Event event; | ||
| 22 | - | ||
| 23 | - protected List<BaseAction> actions; | ||
| 24 | - | ||
| 25 | - private Object startParem; | ||
| 26 | - | ||
| 27 | - private Paramer paramer = new Paramer(); | ||
| 28 | - | ||
| 29 | - protected Logic(Object startParem) { | ||
| 30 | - this.startParem = startParem; | ||
| 31 | - actions = new ArrayList<>(); | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | - public static Logic create(Object startParem) { | ||
| 35 | - return new Logic(startParem); | ||
| 36 | - } | ||
| 37 | - | ||
| 38 | - public static Logic create() { | ||
| 39 | - return new Logic(null); | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - public <param, result> Logic action(Action<param, result> action) { | ||
| 43 | - if (action == null) return this; | ||
| 44 | - actions.add(action); | ||
| 45 | - return this; | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - public <param, result> Logic action(ActionMerge<param, result> action) { | ||
| 49 | - if (action == null) return this; | ||
| 50 | - actions.add(action); | ||
| 51 | - return this; | ||
| 52 | - } | ||
| 53 | - | ||
| 54 | - private int action_index = -1; | ||
| 55 | - | ||
| 56 | - private boolean inStart() { | ||
| 57 | - return action_index != -1; | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - private <Data, Return> void next(Data data) { | ||
| 61 | - BaseAction action = actions.get(action_index++); | ||
| 62 | - BaseAction newAction = action_index < actions.size() ? actions.get(action_index) : null; | ||
| 63 | - LogicCallback<Return> callback = new LogicCallback<>(event, newAction); | ||
| 64 | - Disposable disposable = null; | ||
| 65 | - Exception exception = null; | ||
| 66 | - try { | ||
| 67 | - if (action == null) return; | ||
| 68 | - if (action instanceof Action) disposable = ((Action) action).action(data, callback); | ||
| 69 | - if (action instanceof ActionMerge) disposable = ((ActionMerge) action).action(data, paramer, callback); | ||
| 70 | - } catch (Exception e) { | ||
| 71 | - exception = e; | ||
| 72 | - Log.e("Logic", "页面逻辑出错!", e); | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | - if (disposable != null) event.addDisposable(disposable); | ||
| 76 | - else if (exception != null) onError(Config.FAIL, exception.getMessage(), event); | ||
| 77 | - } | ||
| 78 | - | ||
| 79 | - public <T> Event<T> event() { | ||
| 80 | - if (event != null) return event; | ||
| 81 | - event = new Event<T>(); | ||
| 82 | - event.setLogic(this); | ||
| 83 | - return event; | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | - // | ||
| 87 | - public void start(Event event) { | ||
| 88 | - if (event == null) return; | ||
| 89 | - if (inStart()) return; | ||
| 90 | - | ||
| 91 | - this.action_index = 0; | ||
| 92 | - this.event = event; | ||
| 93 | - | ||
| 94 | - if (event.active) { | ||
| 95 | - event.onState(Config.STATE_LOAD); | ||
| 96 | - next(startParem); | ||
| 97 | - } | ||
| 98 | - } | ||
| 99 | - | ||
| 100 | - public interface Execute<T> { | ||
| 101 | - T execute(); | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - private static String getErrorMessage(Throwable throwable) { | ||
| 105 | - return throwable != null ? throwable.getMessage() : ""; | ||
| 106 | - } | ||
| 107 | - | ||
| 108 | - public static <T> Disposable process(Execute<T> exce, DataCallback<T> callback) { | ||
| 109 | - Consumer<T> successEV = data -> { | ||
| 110 | - if (callback != null) callback.callback(Config.SUCCESS, "", data); | ||
| 111 | - }; | ||
| 112 | - Consumer<Throwable> errorEV = throwable -> { | ||
| 113 | - if (callback != null) callback.callback(Config.FAIL, getErrorMessage(throwable), null); | ||
| 114 | - }; | ||
| 115 | - return Observable | ||
| 116 | - .create((ObservableOnSubscribe<T>) emitter -> emitter.onNext(exce.execute())) | ||
| 117 | - .subscribeOn(Schedulers.io()) | ||
| 118 | - .observeOn(AndroidSchedulers.mainThread()) | ||
| 119 | - .subscribe(successEV, errorEV); | ||
| 120 | - } | ||
| 121 | - | ||
| 122 | - private interface BaseAction { | ||
| 123 | - } | ||
| 124 | - | ||
| 125 | - public interface Action<param, result> extends BaseAction { | ||
| 126 | - Disposable action(param data, DataCallback<result> callback); | ||
| 127 | - } | ||
| 128 | - | ||
| 129 | - public interface ActionMerge<param, result> extends BaseAction { | ||
| 130 | - Disposable action(param data, Paramer paramer, DataCallback<result> callback); | ||
| 131 | - } | ||
| 132 | - | ||
| 133 | - public class LogicCallback<T> implements DataCallback<T> { | ||
| 134 | - private BaseAction action; | ||
| 135 | - private Event event; | ||
| 136 | - | ||
| 137 | - private LogicCallback(Event event, BaseAction action) { | ||
| 138 | - this.event = event; | ||
| 139 | - this.action = action; | ||
| 140 | - } | ||
| 141 | - | ||
| 142 | - @Override | ||
| 143 | - public void callback(int code, String message, T data) { | ||
| 144 | - if (code == Config.SUCCESS) { | ||
| 145 | - if (event.active) { | ||
| 146 | - if (action != null) next(data); | ||
| 147 | - else { | ||
| 148 | - event.onState(Config.STATE_IDLE); | ||
| 149 | - event.onSuccess(data); | ||
| 150 | - } | ||
| 151 | - } | ||
| 152 | - } else onError(code, message, event); | ||
| 153 | - } | ||
| 154 | - } | ||
| 155 | - | ||
| 156 | - private void onError(int code, String message, Event event) { | ||
| 157 | - if (event.active) { | ||
| 158 | - event.onState(Config.STATE_IDLE); | ||
| 159 | - event.onFailure(code, message); | ||
| 160 | - } | ||
| 161 | - } | ||
| 162 | - | ||
| 163 | - public static class Paramer { | ||
| 164 | - private Map<String, Object> map = new HashMap<>(); | ||
| 165 | - | ||
| 166 | - public void addParamer(String key, Object value) { | ||
| 167 | - map.put(key, value); | ||
| 168 | - } | ||
| 169 | - | ||
| 170 | - public <T> T getParamer(String key) { | ||
| 171 | - return (T) map.get(key); | ||
| 172 | - } | ||
| 173 | - } | ||
| 174 | -} |
core/base/src/main/java/com/cnlive/libs/base/logic/callback/Callback.java deleted
100644 → 0
core/base/src/main/java/com/cnlive/libs/base/logic/callback/CancelCallback.java deleted
100644 → 0
core/base/src/main/java/com/cnlive/libs/base/logic/callback/DataCallback.java deleted
100644 → 0
core/base/src/main/java/com/cnlive/libs/base/logic/callback/FailureCallback.java deleted
100644 → 0
core/base/src/main/java/com/cnlive/libs/base/logic/callback/ProgressCallback.java deleted
100644 → 0
core/base/src/main/java/com/cnlive/libs/base/logic/callback/StateCallback.java deleted
100644 → 0
core/base/src/main/java/com/cnlive/libs/base/logic/callback/SuccessCallback.java deleted
100644 → 0