0

更新:我的印象是 display.getRotation 得到 0、90、180 或 270 之一。情况并非如此,根据大小写返回 0-3 之一。

我一直在尝试让 android 在任何方向上工作。让应用程序更新它在屏幕上绘制的方式并不难,但相机显示屏仍然安装在一侧。即:如果您旋转相机屏幕刷新,布局旋转以匹配,但显示的实际相机馈送仍然是横向的。尝试以纵向修复 Zxing 的人已成功修复显示方向以匹配,但是我希望更新此显示方向(不一定在方向更改时,但肯定在应用程序启动时),以便可以在所有应用程序中使用方向。

通过在CameraConfigurationManager.javacamera.setDisplayOrientation(int);内部添加,可以更改应用程序以匹配任何方向setDesiredCameraParameters

我尝试使用以下代码检测方向,然后将 int with 设置setDisplayOrientation为旋转;然而,一旦扫描活动开始,这就会导致崩溃。这两段代码并没有独立导致崩溃。

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

我这样设置代码:

void setDesiredCameraParameters(Camera camera, boolean safeMode) {
Camera.Parameters parameters = camera.getParameters();
//changes start here
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

camera.setDisplayOrientation(rotation);
//changes end here
if (parameters == null) {
  Log.w(TAG, "Device error: no camera parameters are available. Proceeding without configuration.");
  return;
}

Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

if (safeMode) {
  Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
}

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

initializeTorch(parameters, prefs, safeMode);

String focusMode = null;
if (prefs.getBoolean(PreferencesActivity.KEY_AUTO_FOCUS, true)) {
  if (safeMode || prefs.getBoolean(PreferencesActivity.KEY_DISABLE_CONTINUOUS_FOCUS, false)) {
    focusMode = findSettableValue(parameters.getSupportedFocusModes(),
                                  Camera.Parameters.FOCUS_MODE_AUTO);
  } else {
    focusMode = findSettableValue(parameters.getSupportedFocusModes(),
                                  "continuous-picture", // Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE in 4.0+
                                  "continuous-video",   // Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO in 4.0+
                                  Camera.Parameters.FOCUS_MODE_AUTO);
  }
}
// Maybe selected auto-focus but not available, so fall through here:
if (!safeMode && focusMode == null) {
  focusMode = findSettableValue(parameters.getSupportedFocusModes(),
                                Camera.Parameters.FOCUS_MODE_MACRO,
                                "edof"); // Camera.Parameters.FOCUS_MODE_EDOF in 2.2+
}
if (focusMode != null) {
  parameters.setFocusMode(focusMode);
}
//camera.setDisplayOrientation(180);
parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
camera.setParameters(parameters);
}

崩溃时的 logcat 给出以下信息:

09-07 15:40:51.540: E/AndroidRuntime(10989): 致命异常: Thread-33664 09-07 15:40:51.540: E/AndroidRuntime(10989): java.lang.ArrayIndexOutOfBoundsException: src.length=115200 srcPos =146970 dst.length=540 dstPos=0 长度=540 09-07 15:40:51.540:E/AndroidRuntime(10989):在 java.lang.System.arraycopy(本机方法)09-07 15:40:51.540: E/AndroidRuntime(10989): 在 com.google.zxing.PlanarYUVLuminanceSource.getRow(PlanarYUVLuminanceSource.java:71) 09-07 15:40:51.540: E/AndroidRuntime(10989): 在 com.google.zxing.common.GlobalHistogramBinarizer .getBlackRow(GlobalHistogramBinarizer.java:62) 09-07 15:40:51.540: E/AndroidRuntime(10989): at com.google.zxing.BinaryBitmap.getBlackRow(BinaryBitmap.java:65) 09-07 15:40:51.540 : E/AndroidRuntime(10989): at com.google.zxing.oned.OneDReader.doDecode(OneDReader.java:135) 09-07 15:40:51。540: E/AndroidRuntime(10989): at com.google.zxing.oned.OneDReader.decode(OneDReader.java:57) 09-07 15:40:51.540: E/AndroidRuntime(10989): at com.google.zxing .MultiFormatReader.decodeInternal(MultiFormatReader.java:170) 09-07 15:40:51.540: E/AndroidRuntime(10989): at com.google.zxing.MultiFormatReader.decodeWithState(MultiFormatReader.java:85) 09-07 15:40 :51.540: E/AndroidRuntime(10989): at com.google.zxing.client.android.DecodeHandler.decode(DecodeHandler.java:86) 09-07 15:40:51.540: E/AndroidRuntime(10989): at com. google.zxing.client.android.DecodeHandler.handleMessage(DecodeHandler.java:59) 09-07 15:40:51.540: E/AndroidRuntime(10989): 在 android.os.Handler.dispatchMessage(Handler.java:99) 09 -07 15:40:51.540: E/AndroidRuntime(10989): 在 android.os.Looper.loop(Looper.java:137) 09-07 15:40:51.540:E/AndroidRuntime(10989): 在 com.google.zxing.client.android.DecodeThread.run(DecodeThread.java:94) 09-07 15:40:52.610: E/ActivityThread(10989): 活动 com.example.bilisattendancerecorder .Main 已泄露 ServiceConnection android.speech.tts.TextToSpeech$Connection@422b16b8 最初绑定在这里 09-07 15:40:52.610: E/ActivityThread(10989): android.app.ServiceConnectionLeaked: Activity com.example.bilisattendancerecorder。 Main 泄露了 ServiceConnection android.speech.tts.TextToSpeech$Connection@422b16b8 最初绑定在这里 09-07 15:40:52.610: E/ActivityThread(10989): at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java: 974) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868) 09-07 15:40:52.610: E/ActivityThread(10989): 在安卓应用程序。ContextImpl.bindServiceAsUser(ContextImpl.java:1452) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.app.ContextImpl.bindService(ContextImpl.java:1440) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.content.ContextWrapper.bindService(ContextWrapper.java:496) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.speech.tts.TextToSpeech.connectToEngine(TextToSpeech .java:685) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:655) 09-07 15:40:52.610: E/ActivityThread (10989): 在 android.speech.tts.TextToSpeech.(TextToSpeech.java:608) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.speech.tts.TextToSpeech.(TextToSpeech.java: 582) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.speech.tts.TextToSpeech.(TextToSpeech.java:567) 09-07 15:40:52.610: E/ActivityThread(10989): 在 com.example.bilisattendancerecorder.Main.onActivityResult(Main.java:526) 09-07 15:40:52.610: E/ActivityThread(10989) : 在 android.app.Activity.dispatchActivityResult(Activity.java:5322) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3363) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3410) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.app.ActivityThread .access$1100(ActivityThread.java:141) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304) 09-07 15:40: 52.610: E/ActivityThread(10989): 在 android.os.Handler.dispatchMessage(Handler.java:99) 09-07 15:40:52.610:E/ActivityThread(10989): 在 android.os.Looper.loop(Looper.java:137) 09-07 15:40:52.610: E/ActivityThread(10989): 在 android.app.ActivityThread.main(ActivityThread.java :5103) 09-07 15:40:52.610: E/ActivityThread(10989): at java.lang.reflect.Method.invokeNative(Native Method) 09-07 15:40:52.610: E/ActivityThread(10989): at java.lang.reflect.Method.invoke(Method.java:525) 09-07 15:40:52.610: E/ActivityThread(10989): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java :737) 09-07 15:40:52.610: E/ActivityThread(10989): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-07 15:40:52.610: E/ActivityThread (10989):在 dalvik.system.NativeStart.main(本机方法)在 android.app.ActivityThread.main(ActivityThread.java:5103) 09-07 15:40:52.610: E/ActivityThread(10989): 在 java.lang.reflect.Method.invokeNative(Native Method) 09-07 15: 40:52.610: E/ActivityThread(10989): 在 java.lang.reflect.Method.invoke(Method.java:525) 09-07 15:40:52.610: E/ActivityThread(10989): 在 com.android.internal .os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 09-07 15:40:52.610: E/ActivityThread(10989): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-07 15:40:52.610: E/ActivityThread(10989): at dalvik.system.NativeStart.main(Native Method)在 android.app.ActivityThread.main(ActivityThread.java:5103) 09-07 15:40:52.610: E/ActivityThread(10989): 在 java.lang.reflect.Method.invokeNative(Native Method) 09-07 15: 40:52.610: E/ActivityThread(10989): 在 java.lang.reflect.Method.invoke(Method.java:525) 09-07 15:40:52.610: E/ActivityThread(10989): 在 com.android.internal .os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 09-07 15:40:52.610: E/ActivityThread(10989): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-07 15:40:52.610: E/ActivityThread(10989): at dalvik.system.NativeStart.main(Native Method)调用(Method.java:525)09-07 15:40:52.610:E/ActivityThread(10989):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)09-07 15: 40:52.610: E/ActivityThread(10989): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-07 15:40:52.610: E/ActivityThread(10989): 在 dalvik.system .NativeStart.main(本机方法)调用(Method.java:525)09-07 15:40:52.610:E/ActivityThread(10989):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)09-07 15: 40:52.610: E/ActivityThread(10989): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-07 15:40:52.610: E/ActivityThread(10989): 在 dalvik.system .NativeStart.main(本机方法)

任何帮助将不胜感激,并且可以提供更多信息。

谢谢。

4

0 回答 0