Skip to content

Commit

Permalink
修复camera1低版本机型横竖屏切换画面拉伸问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiwean committed Oct 23, 2020
1 parent 8cb1239 commit 3aad5da
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/android/NBZxing/CusScanView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CusScanView @JvmOverloads constructor(context: Context, attributeSet: Attr
}

override fun provideAspectRatio(): AspectRatio {
return AspectRatio.of(1, 1)
return AspectRatio.of(16, 9)
}

override fun resultBackFile(content: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Camera1 extends CameraViewImpl {

private final AtomicBoolean isPictureCaptureInProgress = new AtomicBoolean(false);

Camera mCamera;
volatile Camera mCamera;

private Camera.Parameters mCameraParameters;

Expand Down Expand Up @@ -158,9 +158,7 @@ void setUpPreview() {

@Override
boolean isCameraOpened() {
synchronized (Camera1.class) {
return mCamera != null;
}
return mCamera != null;
}

@Override
Expand Down Expand Up @@ -293,15 +291,17 @@ void setDisplayOrientation(int displayOrientation) {
if (mDisplayOrientation == displayOrientation) {
return;
}
mDisplayOrientation = displayOrientation;
try {
if (isCameraOpened()) {
mCameraParameters.setRotation(calcCameraRotation(displayOrientation));
mCamera.setParameters(mCameraParameters);
mCamera.setDisplayOrientation(calcDisplayOrientation(displayOrientation));
}
} catch (Exception ignored) {
}
if (mPreview != null)
mPreview.setDisplayOrientation(displayOrientation);
// mDisplayOrientation = displayOrientation;
// try {
// if (isCameraOpened()) {
// mCameraParameters.setRotation(calcCameraRotation(displayOrientation));
// mCamera.setParameters(mCameraParameters);
// mCamera.setDisplayOrientation(calcDisplayOrientation(displayOrientation));
// }
// } catch (Exception ignored) {
// }
}

@Override
Expand Down Expand Up @@ -477,8 +477,9 @@ private Size chooseOptimalSize(SortedSet<Size> sizes) {

private void releaseCamera() {
if (mCamera != null) {
mCamera.release();
Camera temp = mCamera;
mCamera = null;
temp.release();
mCallback.onCameraClosed();
}
}
Expand Down Expand Up @@ -606,7 +607,10 @@ protected void rectMeteringWithFocus() {
List<Camera.Area> areas = Collections.singletonList(new Camera.Area(realRect, 1000));
mCameraParameters.setFocusAreas(areas);
mCameraParameters.setMeteringAreas(areas);
mCamera.setParameters(mCameraParameters);
try {
mCamera.setParameters(mCameraParameters);
} catch (Exception ignored) {
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void onReady() {

private CameraCharacteristics mCameraCharacteristics;

CameraDevice mCamera;
volatile CameraDevice mCamera;

CameraCaptureSession mCaptureSession;

Expand Down Expand Up @@ -253,9 +253,7 @@ void stop() {

@Override
boolean isCameraOpened() {
synchronized (Camera2.class) {
return mCamera != null;
}
return mCamera != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,16 @@ void setSize(int width, int height) {
mWidth = width;
mHeight = height;
View v = (View) getView().getParent();
getView().setTranslationX(0);
getView().setTranslationY(0);
if (v == null)
return;
if (width > v.getMeasuredWidth())
getView().setTranslationX((float) -(width - v.getMeasuredWidth()) / 2);
if (height > v.getMeasuredHeight())
getView().setTranslationY((float) -(height - v.getMeasuredHeight()) / 2);

v.post(() -> {
getView().setTranslationX(0);
getView().setTranslationY(0);
if (v == null)
return;
if (width > v.getMeasuredWidth())
getView().setTranslationX((float) -(width - v.getMeasuredWidth()) / 2);
if (height > v.getMeasuredHeight())
getView().setTranslationY((float) -(height - v.getMeasuredHeight()) / 2);
});
if (cameraHandler != null &&
cameraHandler.getLooper().getThread().getState() == Thread.State.TIMED_WAITING)
cameraHandler.getLooper().getThread().interrupt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
if (isCameraOpened) {
closeCameraBefore()
openCameraBefore()
cameraHandler.removeCallbacksAndMessages(null)
cameraHandler.post {
stop()
start()
Expand Down

0 comments on commit 3aad5da

Please sign in to comment.