Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.89 KB | None | 0 0
  1. @Override
  2. public void onResume() {
  3. super.onResume();
  4. startBackgroundThread();
  5. // When the screen is turned off and turned back on, the SurfaceTexture is already
  6. // available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
  7. // a camera and start preview from here (otherwise, we wait until the surface is ready in
  8. // the SurfaceTextureListener).
  9. if (mTextureView.isAvailable()) {
  10. openCamera(mTextureView.getWidth(), mTextureView.getHeight());
  11. } else {
  12. mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
  13. }
  14. if(null != takenPictureBytes && takenPictureBytes.length > 0){
  15. switchPanels(true);
  16. }else{
  17. switchPanels(false);
  18. }
  19. }
  20.  
  21. private final TextureView.SurfaceTextureListener mSurfaceTextureListener
  22. = new TextureView.SurfaceTextureListener() {
  23.  
  24. @Override
  25. public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
  26. openCamera(width, height);
  27. }
  28.  
  29. @Override
  30. public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {
  31. configureTransform(width, height);
  32. }
  33.  
  34. @Override
  35. public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) {
  36. return true;
  37. }
  38.  
  39. @Override
  40. public void onSurfaceTextureUpdated(SurfaceTexture texture) {
  41. }
  42.  
  43. };
  44. private void openCamera(int width, int height) {
  45. if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
  46. != PackageManager.PERMISSION_GRANTED) {
  47. requestCameraPermission();
  48. return;
  49. }
  50. setUpCameraOutputs(width, height);
  51. configureTransform(width, height);
  52. Activity activity = getActivity();
  53. CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
  54. try {
  55. if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
  56. throw new RuntimeException("Time out waiting to lock camera opening.");
  57. }
  58. manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
  59. } catch (CameraAccessException e) {
  60. e.printStackTrace();
  61. } catch (InterruptedException e) {
  62. throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
  63. }
  64. }
  65.  
  66. @OnClick(R.id.take_picture_button)
  67. public void takePictureClick() {
  68. takePictureButton.setEnabled(false);
  69. EventsTracker.photoCaptureClicked(getContext());
  70. EventsTracker.photoCaptureClicked(getContext());
  71. Log.d(TAG, "take picture clicked");
  72. progress.setVisibility(View.VISIBLE);
  73. stopMultipleClicks();
  74. takePicture();
  75. }
  76.  
  77. private void takePicture() {
  78. lockFocus();
  79. }
  80. private void lockFocus() {
  81. try {
  82. // This is how to tell the camera to lock focus.
  83. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
  84. CaptureRequest captureRequest = mPreviewRequestBuilder.build();
  85. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, null); // prevent CONTROL_AF_TRIGGER_START from calling over and over again
  86. mState = STATE_WAITING_LOCK;
  87. mCaptureSession.capture(captureRequest, mCaptureCallback, mBackgroundHandler);
  88.  
  89. // Tell #mCaptureCallback to wait for the lock.
  90.  
  91. } catch (CameraAccessException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. private void unlockFocus() {
  96. try {
  97. // Reset the auto-focus trigger
  98. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
  99. CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
  100. setAutoFlash(mPreviewRequestBuilder);
  101. mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
  102. mBackgroundHandler);
  103. // After this, the camera will go back to the normal state of preview.
  104. mState = STATE_PREVIEW;
  105. mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback,
  106. mBackgroundHandler);
  107. } catch (CameraAccessException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. private void runPrecaptureSequence() {
  112. try {
  113. // This is how to tell the camera to trigger.
  114. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
  115. CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
  116. // Tell #mCaptureCallback to wait for the precapture sequence to be set.
  117. mState = STATE_WAITING_PRECAPTURE;
  118. mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
  119. mBackgroundHandler);
  120. } catch (CameraAccessException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124.  
  125. private CameraCaptureSession.CaptureCallback mCaptureCallback
  126. = new CameraCaptureSession.CaptureCallback() {
  127.  
  128. private void process(CaptureResult result) {
  129. switch (mState) {
  130. case STATE_PREVIEW: {
  131.  
  132. // We have nothing to do when the camera preview is working normally.
  133. // TODO: handle auto focus
  134. Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
  135. if (afState != null && !afState.equals(mLastAfState)) {
  136. switch (afState) {
  137. case CaptureResult.CONTROL_AF_STATE_INACTIVE:
  138. Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_INACTIVE");
  139. lockAutoFocus();
  140. break;
  141. case CaptureResult.CONTROL_AF_STATE_ACTIVE_SCAN:
  142. Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_ACTIVE_SCAN");
  143. break;
  144. case CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED:
  145. Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED");
  146. mUiHandler.removeCallbacks(mLockAutoFocusRunnable);
  147. mUiHandler.postDelayed(mLockAutoFocusRunnable, LOCK_FOCUS_DELAY_ON_FOCUSED);
  148. break;
  149. case CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
  150. mUiHandler.removeCallbacks(mLockAutoFocusRunnable);
  151. mUiHandler.postDelayed(mLockAutoFocusRunnable, LOCK_FOCUS_DELAY_ON_UNFOCUSED);
  152. Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED");
  153. break;
  154. case CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED:
  155. mUiHandler.removeCallbacks(mLockAutoFocusRunnable);
  156. //mUiHandler.postDelayed(mLockAutoFocusRunnable, LOCK_FOCUS_DELAY_ON_UNFOCUSED);
  157. Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED");
  158. break;
  159. case CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN:
  160. Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN");
  161. break;
  162. case CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED:
  163. mUiHandler.removeCallbacks(mLockAutoFocusRunnable);
  164. //mUiHandler.postDelayed(mLockAutoFocusRunnable, LOCK_FOCUS_DELAY_ON_FOCUSED);
  165. Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED");
  166. break;
  167. }
  168. }
  169. mLastAfState = afState;
  170. break;
  171. }
  172. case STATE_WAITING_LOCK: {
  173. Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
  174. if (afState == null) {
  175. captureStillPicture();
  176. } else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
  177. //CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED == afState || CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED == afState ||
  178. CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
  179. // CONTROL_AE_STATE can be null on some devices
  180. Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
  181. if (aeState == null ||
  182. aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
  183. mState = STATE_PICTURE_TAKEN;
  184. captureStillPicture();
  185. } else {
  186. runPrecaptureSequence();
  187. }
  188. }
  189. break;
  190. }
  191. case STATE_WAITING_PRECAPTURE: {
  192. // CONTROL_AE_STATE can be null on some devices
  193. Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
  194. if (aeState == null ||
  195. aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
  196. aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
  197. mState = STATE_WAITING_NON_PRECAPTURE;
  198. }
  199. break;
  200. }
  201. case STATE_WAITING_NON_PRECAPTURE: {
  202. // CONTROL_AE_STATE can be null on some devices
  203. Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
  204. if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
  205. mState = STATE_PICTURE_TAKEN;
  206. captureStillPicture();
  207. }
  208. break;
  209. }
  210. }
  211. }
  212.  
  213. private void captureStillPicture() {
  214. try {
  215. final Activity activity = getActivity();
  216. if (null == activity || null == mCameraDevice) {
  217. return;
  218. }
  219. // This is the CaptureRequest.Builder that we use to take a picture.
  220. final CaptureRequest.Builder captureBuilder =
  221. mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
  222. captureBuilder.addTarget(mImageReader.getSurface());
  223.  
  224. // Use the same AE and AF modes as the preview.
  225. if (isAutoFocusSupported())
  226. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
  227. CaptureRequest.CONTROL_AF_MODE_AUTO);
  228. else
  229. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
  230. CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
  231.  
  232. setAutoFlash(captureBuilder);
  233.  
  234. // Orientation
  235. int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
  236. captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
  237.  
  238. CameraCaptureSession.CaptureCallback CaptureCallback
  239. = new CameraCaptureSession.CaptureCallback() {
  240.  
  241. @Override
  242. public void onCaptureCompleted(@NonNull CameraCaptureSession session,
  243. @NonNull CaptureRequest request,
  244. @NonNull TotalCaptureResult result) {
  245. Log.d(TAG, "takePicture - camera capture session");
  246. switchPanels(true);
  247. unlockFocus();
  248. progress.setVisibility(View.GONE);
  249. }
  250. };
  251.  
  252. mCaptureSession.stopRepeating();
  253. mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
  254. } catch (CameraAccessException e) {
  255. e.printStackTrace();
  256. }
  257. }
  258.  
  259. @Override
  260. public void onCaptureProgressed(@NonNull CameraCaptureSession session,
  261. @NonNull CaptureRequest request,
  262. @NonNull CaptureResult partialResult) {
  263. process(partialResult);
  264. }
  265.  
  266. @Override
  267. public void onCaptureCompleted(@NonNull CameraCaptureSession session,
  268. @NonNull CaptureRequest request,
  269. @NonNull TotalCaptureResult result) {
  270. //process(result);
  271. switchPanels(true);
  272. unlockFocus();
  273. progress.setVisibility(View.GONE);
  274. }
  275.  
  276. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement