Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void onResume() {
- super.onResume();
- startBackgroundThread();
- // When the screen is turned off and turned back on, the SurfaceTexture is already
- // available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
- // a camera and start preview from here (otherwise, we wait until the surface is ready in
- // the SurfaceTextureListener).
- if (mTextureView.isAvailable()) {
- openCamera(mTextureView.getWidth(), mTextureView.getHeight());
- } else {
- mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
- }
- if(null != takenPictureBytes && takenPictureBytes.length > 0){
- switchPanels(true);
- }else{
- switchPanels(false);
- }
- }
- private final TextureView.SurfaceTextureListener mSurfaceTextureListener
- = new TextureView.SurfaceTextureListener() {
- @Override
- public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
- openCamera(width, height);
- }
- @Override
- public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {
- configureTransform(width, height);
- }
- @Override
- public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) {
- return true;
- }
- @Override
- public void onSurfaceTextureUpdated(SurfaceTexture texture) {
- }
- };
- private void openCamera(int width, int height) {
- if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
- != PackageManager.PERMISSION_GRANTED) {
- requestCameraPermission();
- return;
- }
- setUpCameraOutputs(width, height);
- configureTransform(width, height);
- Activity activity = getActivity();
- CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
- try {
- if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
- throw new RuntimeException("Time out waiting to lock camera opening.");
- }
- manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
- } catch (CameraAccessException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
- }
- }
- @OnClick(R.id.take_picture_button)
- public void takePictureClick() {
- takePictureButton.setEnabled(false);
- EventsTracker.photoCaptureClicked(getContext());
- EventsTracker.photoCaptureClicked(getContext());
- Log.d(TAG, "take picture clicked");
- progress.setVisibility(View.VISIBLE);
- stopMultipleClicks();
- takePicture();
- }
- private void takePicture() {
- lockFocus();
- }
- private void lockFocus() {
- try {
- // This is how to tell the camera to lock focus.
- mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
- CaptureRequest captureRequest = mPreviewRequestBuilder.build();
- mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, null); // prevent CONTROL_AF_TRIGGER_START from calling over and over again
- mState = STATE_WAITING_LOCK;
- mCaptureSession.capture(captureRequest, mCaptureCallback, mBackgroundHandler);
- // Tell #mCaptureCallback to wait for the lock.
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
- private void unlockFocus() {
- try {
- // Reset the auto-focus trigger
- mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
- CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
- setAutoFlash(mPreviewRequestBuilder);
- mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
- mBackgroundHandler);
- // After this, the camera will go back to the normal state of preview.
- mState = STATE_PREVIEW;
- mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback,
- mBackgroundHandler);
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
- private void runPrecaptureSequence() {
- try {
- // This is how to tell the camera to trigger.
- mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
- CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START);
- // Tell #mCaptureCallback to wait for the precapture sequence to be set.
- mState = STATE_WAITING_PRECAPTURE;
- mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
- mBackgroundHandler);
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
- private CameraCaptureSession.CaptureCallback mCaptureCallback
- = new CameraCaptureSession.CaptureCallback() {
- private void process(CaptureResult result) {
- switch (mState) {
- case STATE_PREVIEW: {
- // We have nothing to do when the camera preview is working normally.
- // TODO: handle auto focus
- Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
- if (afState != null && !afState.equals(mLastAfState)) {
- switch (afState) {
- case CaptureResult.CONTROL_AF_STATE_INACTIVE:
- Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_INACTIVE");
- lockAutoFocus();
- break;
- case CaptureResult.CONTROL_AF_STATE_ACTIVE_SCAN:
- Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_ACTIVE_SCAN");
- break;
- case CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED:
- Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED");
- mUiHandler.removeCallbacks(mLockAutoFocusRunnable);
- mUiHandler.postDelayed(mLockAutoFocusRunnable, LOCK_FOCUS_DELAY_ON_FOCUSED);
- break;
- case CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
- mUiHandler.removeCallbacks(mLockAutoFocusRunnable);
- mUiHandler.postDelayed(mLockAutoFocusRunnable, LOCK_FOCUS_DELAY_ON_UNFOCUSED);
- Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED");
- break;
- case CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED:
- mUiHandler.removeCallbacks(mLockAutoFocusRunnable);
- //mUiHandler.postDelayed(mLockAutoFocusRunnable, LOCK_FOCUS_DELAY_ON_UNFOCUSED);
- Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED");
- break;
- case CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN:
- Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN");
- break;
- case CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED:
- mUiHandler.removeCallbacks(mLockAutoFocusRunnable);
- //mUiHandler.postDelayed(mLockAutoFocusRunnable, LOCK_FOCUS_DELAY_ON_FOCUSED);
- Log.d(TAG, "CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED");
- break;
- }
- }
- mLastAfState = afState;
- break;
- }
- case STATE_WAITING_LOCK: {
- Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
- if (afState == null) {
- captureStillPicture();
- } else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
- //CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED == afState || CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED == afState ||
- CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
- // CONTROL_AE_STATE can be null on some devices
- Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
- if (aeState == null ||
- aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
- mState = STATE_PICTURE_TAKEN;
- captureStillPicture();
- } else {
- runPrecaptureSequence();
- }
- }
- break;
- }
- case STATE_WAITING_PRECAPTURE: {
- // CONTROL_AE_STATE can be null on some devices
- Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
- if (aeState == null ||
- aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
- aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
- mState = STATE_WAITING_NON_PRECAPTURE;
- }
- break;
- }
- case STATE_WAITING_NON_PRECAPTURE: {
- // CONTROL_AE_STATE can be null on some devices
- Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
- if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
- mState = STATE_PICTURE_TAKEN;
- captureStillPicture();
- }
- break;
- }
- }
- }
- private void captureStillPicture() {
- try {
- final Activity activity = getActivity();
- if (null == activity || null == mCameraDevice) {
- return;
- }
- // This is the CaptureRequest.Builder that we use to take a picture.
- final CaptureRequest.Builder captureBuilder =
- mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
- captureBuilder.addTarget(mImageReader.getSurface());
- // Use the same AE and AF modes as the preview.
- if (isAutoFocusSupported())
- mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
- CaptureRequest.CONTROL_AF_MODE_AUTO);
- else
- mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
- CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
- setAutoFlash(captureBuilder);
- // Orientation
- int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
- captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
- CameraCaptureSession.CaptureCallback CaptureCallback
- = new CameraCaptureSession.CaptureCallback() {
- @Override
- public void onCaptureCompleted(@NonNull CameraCaptureSession session,
- @NonNull CaptureRequest request,
- @NonNull TotalCaptureResult result) {
- Log.d(TAG, "takePicture - camera capture session");
- switchPanels(true);
- unlockFocus();
- progress.setVisibility(View.GONE);
- }
- };
- mCaptureSession.stopRepeating();
- mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onCaptureProgressed(@NonNull CameraCaptureSession session,
- @NonNull CaptureRequest request,
- @NonNull CaptureResult partialResult) {
- process(partialResult);
- }
- @Override
- public void onCaptureCompleted(@NonNull CameraCaptureSession session,
- @NonNull CaptureRequest request,
- @NonNull TotalCaptureResult result) {
- //process(result);
- switchPanels(true);
- unlockFocus();
- progress.setVisibility(View.GONE);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement