Skip to content

Commit

Permalink
提升整体稳定性
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiwean committed Jul 24, 2020
1 parent 8cdc1c1 commit 0f800d5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.annotation.SuppressLint;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;

import androidx.collection.SparseArrayCompat;
Expand Down Expand Up @@ -76,12 +77,9 @@ class Camera1 extends CameraViewImpl {

Camera1(Callback callback, PreviewImpl preview) {
super(callback, preview);
preview.setCallback(new PreviewImpl.Callback() {
@Override
public void onSurfaceChanged() {
if (mCamera != null) {
setUpPreview();
}
preview.setCallback(() -> {
if (mCamera != null) {
setUpPreview();
}
});
}
Expand All @@ -92,7 +90,8 @@ boolean start() {
if (mCameraId == INVALID_CAMERA_ID) {
return false;
}
openCamera();
if (!openCamera())
return false;
if (mPreview.isReady()) {
setUpPreview();
}
Expand All @@ -108,27 +107,32 @@ boolean start() {

@Override
void stop() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
synchronized (Camera1.class) {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
}
mShowingPreview = false;
releaseCamera();
}
mShowingPreview = false;
releaseCamera();
}

// Suppresses Camera#setPreviewTexture
@SuppressLint("NewApi")
void setUpPreview() {
try {
if (mPreview.getOutputClass() == SurfaceHolder.class) {
mCamera.setPreviewDisplay(mPreview.getSurfaceHolder());
} else {
mCamera.setPreviewTexture((SurfaceTexture) mPreview.getSurfaceTexture());
synchronized (Camera1.class) {
if (mCamera == null)
return;
try {
if (mPreview.getOutputClass() == SurfaceHolder.class) {
mCamera.setPreviewDisplay(mPreview.getSurfaceHolder());
} else {
mCamera.setPreviewTexture((SurfaceTexture) mPreview.getSurfaceTexture());
}
mCamera.setPreviewCallback((data, camera) ->
mCallback.onPreviewByte(data));
} catch (IOException ignored) {
}
mCamera.setPreviewCallback((data, camera) ->
mCallback.onPreviewByte(data));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -308,18 +312,18 @@ private void chooseCamera() {
mCameraId = INVALID_CAMERA_ID;
}

private void openCamera() {
private boolean openCamera() {
if (mCamera != null) {
releaseCamera();
}
try {
mCamera = Camera.open(mCameraId);
} catch (Exception e) {
return;
return false;
}

if (mCamera == null) {
return;
return false;
}

mCameraParameters = mCamera.getParameters();
Expand All @@ -340,9 +344,11 @@ private void openCamera() {
try {
adjustCameraParameters();
} catch (Exception ignored) {
return false;
}
mCamera.setDisplayOrientation(calcDisplayOrientation(mDisplayOrientation));
mCallback.onCameraOpened();
return true;
}

private AspectRatio chooseAspectRatio() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class Camera2 extends CameraViewImpl {

@Override
public void onOpened(@NonNull CameraDevice camera) {
Log.e("currentThread-Open", Thread.currentThread().getName());
mCamera = camera;
mCallback.onCameraOpened();
startCaptureSession();
Expand Down Expand Up @@ -156,21 +155,16 @@ public void onReady() {
};

private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {

@Override
public void onImageAvailable(ImageReader reader) {
try (Image image = reader.acquireNextImage()) {
Image.Plane[] planes = image.getPlanes();
if (planes.length > 0) {
ByteBuffer buffer = planes[0].getBuffer();
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
mCallback.onPictureTaken(data);
}
= reader -> {
try (Image image = reader.acquireNextImage()) {
Image.Plane[] planes = image.getPlanes();
if (planes.length > 0) {
ByteBuffer buffer = planes[0].getBuffer();
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
mCallback.onPictureTaken(data);
}
}

};


Expand Down Expand Up @@ -205,7 +199,7 @@ public void onImageAvailable(ImageReader reader) {
Camera2(Callback callback, PreviewImpl preview, Context context) {
super(callback, preview);
mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
mPreview.setCallback(() -> startCaptureSession());
mPreview.setCallback(this::startCaptureSession);
}

@Override
Expand All @@ -221,21 +215,23 @@ boolean start() {

@Override
void stop() {
if (mCamera != null) {
mCamera.close();
mCamera = null;
}
if (mCaptureSession != null) {
mCaptureSession.close();
mCaptureSession = null;
}
if (mImageReader != null) {
mImageReader.close();
mImageReader = null;
}
if (mYuvReader != null) {
mYuvReader.close();
mYuvReader = null;
synchronized (Camera2.class) {
if (mCamera != null) {
mCamera.close();
mCamera = null;
}
if (mCaptureSession != null) {
mCaptureSession.close();
mCaptureSession = null;
}
if (mImageReader != null) {
mImageReader.close();
mImageReader = null;
}
if (mYuvReader != null) {
mYuvReader.close();
mYuvReader = null;
}
}
}

Expand Down Expand Up @@ -531,23 +527,25 @@ private void startOpeningCamera() {
* <p>The result will be continuously processed in {@link #mSessionCallback}.</p>
*/
void startCaptureSession() {
if (!isCameraOpened() || !mPreview.isReady() || mImageReader == null) {
return;
}
Size previewSize = chooseOptimalSize();
mPreview.setBufferSize(previewSize.getWidth(), previewSize.getHeight());
Surface surface = mPreview.getSurface();
try {
mPreviewRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(surface);
mPreviewRequestBuilder.addTarget(mYuvReader.getSurface());
mCamera.createCaptureSession(Arrays.asList(surface
, mImageReader.getSurface()
, mYuvReader.getSurface()
),
mSessionCallback, null);
} catch (CameraAccessException e) {
throw new RuntimeException("Failed to start camera session");
synchronized (Camera2.class) {
if (!isCameraOpened() || !mPreview.isReady() || mImageReader == null) {
return;
}
Size previewSize = chooseOptimalSize();
mPreview.setBufferSize(previewSize.getWidth(), previewSize.getHeight());
Surface surface = mPreview.getSurface();
try {
mPreviewRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(surface);
mPreviewRequestBuilder.addTarget(mYuvReader.getSurface());
mCamera.createCaptureSession(Arrays.asList(surface
, mImageReader.getSurface()
, mYuvReader.getSurface()
),
mSessionCallback, null);
} catch (CameraAccessException e) {
throw new RuntimeException("Failed to start camera session");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
}

protected val cameraHandler by lazy {
val handlerThread: HandlerThread = HandlerThread(System.currentTimeMillis().toString())
val handlerThread = HandlerThread(System.currentTimeMillis().toString())
handlerThread.start()
Handler(handlerThread.looper)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,6 @@ protected void onRestoreInstanceState(Parcelable state) {
* Open a camera device and start showing camera preview. This is typically called from
*/
public void start() {

Log.e("currentThread", Thread.currentThread().getName());

if (!isCameraOpened() && !mImpl.start()) {
//store the state ,and restore this state after fall back o Camera1
Parcelable state = onSaveInstanceState();
Expand Down

0 comments on commit 0f800d5

Please sign in to comment.