Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: media/capture/video/android/java/src/org/chromium/media/VideoCaptureCamera.java

Issue 2826453002: Image Capture Android: only restore preview parameters after photo is taken (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.media; 5 package org.chromium.media;
6 6
7 import android.annotation.TargetApi; 7 import android.annotation.TargetApi;
8 import android.content.Context; 8 import android.content.Context;
9 import android.graphics.ImageFormat; 9 import android.graphics.ImageFormat;
10 import android.graphics.Rect; 10 import android.graphics.Rect;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 } 77 }
78 } 78 }
79 return ImageFormat.YV12; 79 return ImageFormat.YV12;
80 } 80 }
81 } 81 }
82 82
83 private int mExpectedFrameSize; 83 private int mExpectedFrameSize;
84 private final Object mPhotoTakenCallbackLock = new Object(); 84 private final Object mPhotoTakenCallbackLock = new Object();
85 85
86 // Storage of takePicture() callback Id. There can be one such request in fl ight at most, and 86 // Storage of takePicture() callback Id. There can be one such request in fl ight at most, and
87 // needs to be exercised either in case of error or sucess. 87 // needs to be exercised either in case of error or success.
88 private long mPhotoTakenCallbackId; 88 private long mPhotoTakenCallbackId;
89 private int mPhotoWidth; 89
90 private int mPhotoHeight; 90 private int mPhotoWidth = 0;
91 private int mPhotoHeight = 0;
91 private android.hardware.Camera.Area mAreaOfInterest; 92 private android.hardware.Camera.Area mAreaOfInterest;
93 private android.hardware.Camera.Parameters mPreviewParameters = null;
Reilly Grant (use Gerrit) 2017/04/18 00:32:12 In Java everything starts zero-initialized.
mcasas 2017/04/18 01:09:38 Oops too much C++ lately. Done.
92 94
93 private android.hardware.Camera mCamera; 95 private android.hardware.Camera mCamera;
94 // Lock to mutually exclude execution of OnPreviewFrame() and {start/stop}Ca pture(). 96 // Lock to mutually exclude execution of OnPreviewFrame() and {start/stop}Ca pture().
95 private ReentrantLock mPreviewBufferLock = new ReentrantLock(); 97 private ReentrantLock mPreviewBufferLock = new ReentrantLock();
96 // True when native code has started capture. 98 // True when native code has started capture.
97 private boolean mIsRunning; 99 private boolean mIsRunning;
98 100
99 private int[] mGlTextures; 101 private int[] mGlTextures;
100 private SurfaceTexture mSurfaceTexture; 102 private SurfaceTexture mSurfaceTexture;
101 103
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 private class CrPictureCallback implements android.hardware.Camera.PictureCa llback { 155 private class CrPictureCallback implements android.hardware.Camera.PictureCa llback {
154 @Override 156 @Override
155 public void onPictureTaken(byte[] data, android.hardware.Camera camera) { 157 public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
156 synchronized (mPhotoTakenCallbackLock) { 158 synchronized (mPhotoTakenCallbackLock) {
157 if (mPhotoTakenCallbackId != 0) { 159 if (mPhotoTakenCallbackId != 0) {
158 nativeOnPhotoTaken( 160 nativeOnPhotoTaken(
159 mNativeVideoCaptureDeviceAndroid, mPhotoTakenCallbac kId, data); 161 mNativeVideoCaptureDeviceAndroid, mPhotoTakenCallbac kId, data);
160 } 162 }
161 mPhotoTakenCallbackId = 0; 163 mPhotoTakenCallbackId = 0;
162 } 164 }
163 android.hardware.Camera.Parameters parameters = getCameraParameters( mCamera); 165 try {
164 parameters.setRotation(0); 166 Log.d(TAG, "|mPreviewParameters|: %s", mPreviewParameters.flatte n());
165 mCamera.setParameters(parameters); 167 camera.setParameters(mPreviewParameters);
166 camera.startPreview(); 168 } catch (RuntimeException ex) {
169 Log.e(TAG, "onPictureTaken, setParameters() " + ex);
170 }
171 try {
172 camera.startPreview();
173 } catch (RuntimeException ex) {
174 Log.e(TAG, "onPictureTaken, startPreview() " + ex);
175 }
Reilly Grant (use Gerrit) 2017/04/18 00:32:12 I think we should do these steps before resetting
mcasas 2017/04/18 01:09:38 Done.
167 } 176 }
168 }; 177 };
169 178
170 static int getNumberOfCameras() { 179 static int getNumberOfCameras() {
171 return android.hardware.Camera.getNumberOfCameras(); 180 return android.hardware.Camera.getNumberOfCameras();
172 } 181 }
173 182
174 static int getCaptureApiType(int id) { 183 static int getCaptureApiType(int id) {
175 if (VideoCaptureCamera.getCameraInfo(id) == null) { 184 if (VideoCaptureCamera.getCameraInfo(id) == null) {
176 return VideoCaptureApi.UNKNOWN; 185 return VideoCaptureApi.UNKNOWN;
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 if (mCamera == null || !mIsRunning) { 753 if (mCamera == null || !mIsRunning) {
745 Log.e(TAG, "takePhoto: mCamera is null or is not running"); 754 Log.e(TAG, "takePhoto: mCamera is null or is not running");
746 return false; 755 return false;
747 } 756 }
748 757
749 // Only one picture can be taken at once. 758 // Only one picture can be taken at once.
750 synchronized (mPhotoTakenCallbackLock) { 759 synchronized (mPhotoTakenCallbackLock) {
751 if (mPhotoTakenCallbackId != 0) return false; 760 if (mPhotoTakenCallbackId != 0) return false;
752 mPhotoTakenCallbackId = callbackId; 761 mPhotoTakenCallbackId = callbackId;
753 } 762 }
763 mPreviewParameters = getCameraParameters(mCamera);
754 764
755 android.hardware.Camera.Parameters parameters = getCameraParameters(mCam era); 765 android.hardware.Camera.Parameters photoParameters = getCameraParameters (mCamera);
756 parameters.setRotation(getCameraRotation()); 766 photoParameters.setRotation(getCameraRotation());
757 final android.hardware.Camera.Size original_size = parameters.getPicture Size();
758 767
759 List<android.hardware.Camera.Size> supportedSizes = parameters.getSuppor tedPictureSizes(); 768 if (mPhotoWidth > 0 || mPhotoHeight > 0) {
760 android.hardware.Camera.Size closestSize = null; 769 // |mPhotoWidth| and |mPhotoHeight| can only be set immediately befo re takePicture(),
Reilly Grant (use Gerrit) 2017/04/18 00:32:12 This comment is confusing because |mPhotoWidth| an
mcasas 2017/04/18 01:09:38 Yeah, it was a leftover of an intermediate step. A
761 int minDiff = Integer.MAX_VALUE; 770 // otherwise they interfere with the preview format.
762 for (android.hardware.Camera.Size size : supportedSizes) { 771 final List<android.hardware.Camera.Size> supportedSizes =
763 final int diff = ((mPhotoWidth > 0) ? Math.abs(size.width - mPhotoWi dth) : 0) 772 photoParameters.getSupportedPictureSizes();
764 + ((mPhotoHeight > 0) ? Math.abs(size.height - mPhotoHeight) : 0); 773 android.hardware.Camera.Size closestSize = null;
765 if (diff < minDiff) { 774 int minDiff = Integer.MAX_VALUE;
766 minDiff = diff; 775 for (android.hardware.Camera.Size size : supportedSizes) {
767 closestSize = size; 776 final int diff = ((mPhotoWidth > 0) ? Math.abs(size.width - mPho toWidth) : 0)
777 + ((mPhotoHeight > 0) ? Math.abs(size.height - mPhotoHei ght) : 0);
778 if (diff < minDiff) {
779 minDiff = diff;
780 closestSize = size;
781 }
768 } 782 }
769 } 783 if (minDiff != Integer.MAX_VALUE) {
770 Log.d(TAG, "requested resolution: (%dx%d)", mPhotoWidth, mPhotoHeight); 784 Log.d(TAG, "requested resolution: (%dx%d); matched (%dx%d)", mPh otoWidth,
771 if (minDiff != Integer.MAX_VALUE) { 785 mPhotoHeight, closestSize.width, closestSize.height);
772 Log.d(TAG, " matched (%dx%d)", closestSize.width, closestSize.height ); 786 photoParameters.setPictureSize(closestSize.width, closestSize.he ight);
773 parameters.setPictureSize(closestSize.width, closestSize.height); 787 }
774 } 788 }
775 789
776 try { 790 try {
777 mCamera.setParameters(parameters); 791 Log.d(TAG, "|photoParameters|: %s", photoParameters.flatten());
778 mCamera.takePicture(null, null, null, new CrPictureCallback()); 792 mCamera.setParameters(photoParameters);
779 } catch (RuntimeException ex) { 793 } catch (RuntimeException ex) {
780 Log.e(TAG, "takePicture ", ex); 794 Log.e(TAG, "setParameters " + ex);
781 return false; 795 return false;
782 } 796 }
783 797
784 // Restore original parameters. 798 mCamera.takePicture(null, null, null, new CrPictureCallback());
785 parameters.setPictureSize(original_size.width, original_size.height);
786 try {
787 mCamera.setParameters(parameters);
788 } catch (RuntimeException ex) {
789 Log.e(TAG, "takePicture ", ex);
790 return false;
791 }
792 return true; 799 return true;
793 } 800 }
794 801
795 @Override 802 @Override
796 public void deallocate() { 803 public void deallocate() {
797 if (mCamera == null) return; 804 if (mCamera == null) return;
798 805
799 stopCapture(); 806 stopCapture();
800 try { 807 try {
801 mCamera.setPreviewTexture(null); 808 mCamera.setPreviewTexture(null);
(...skipping 23 matching lines...) Expand all
825 getCameraRotation()); 832 getCameraRotation());
826 } 833 }
827 } finally { 834 } finally {
828 mPreviewBufferLock.unlock(); 835 mPreviewBufferLock.unlock();
829 if (camera != null) { 836 if (camera != null) {
830 camera.addCallbackBuffer(data); 837 camera.addCallbackBuffer(data);
831 } 838 }
832 } 839 }
833 } 840 }
834 } 841 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698