Skip to content

Commit ca27a07

Browse files
committed
采用非Activity的context预览方向异常导致启动摄像头失败
1 parent 2704cdc commit ca27a07

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

util/src/main/java/cn/readsense/module/camera1/CameraController.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,30 +139,34 @@ public void releaseCamera() {
139139

140140

141141
private int getCameraDisplayOrientation(Context context, int cameraId) {
142-
Camera.CameraInfo info = new Camera.CameraInfo();
143-
Camera.getCameraInfo(cameraId, info);
144-
int rotation = ((Activity) context).getWindowManager().getDefaultDisplay().getRotation();
145-
short degrees = 0;
146-
switch (rotation) {
147-
case 0:
148-
degrees = 0;
149-
break;
150-
case 1:
151-
degrees = 90;
152-
break;
153-
case 2:
154-
degrees = 180;
155-
break;
156-
case 3:
157-
degrees = 270;
158-
}
142+
int result = 0;
143+
try {
144+
Camera.CameraInfo info = new Camera.CameraInfo();
145+
Camera.getCameraInfo(cameraId, info);
146+
int rotation = ((Activity) context).getWindowManager().getDefaultDisplay().getRotation();
147+
short degrees = 0;
148+
switch (rotation) {
149+
case 0:
150+
degrees = 0;
151+
break;
152+
case 1:
153+
degrees = 90;
154+
break;
155+
case 2:
156+
degrees = 180;
157+
break;
158+
case 3:
159+
degrees = 270;
160+
}
159161

160-
int result;
161-
if (info.facing == 1) {
162-
result = (info.orientation + degrees) % 360;
163-
result = (360 - result) % 360;
164-
} else {
165-
result = (info.orientation - degrees + 360) % 360;
162+
if (info.facing == 1) {
163+
result = (info.orientation + degrees) % 360;
164+
result = (360 - result) % 360;
165+
} else {
166+
result = (info.orientation - degrees + 360) % 360;
167+
}
168+
} catch (Exception e) {
169+
e.printStackTrace();
166170
}
167171

168172
return result;

0 commit comments

Comments
 (0)