从充电器上拔下插头时,Android摄像头自动对焦停止

从充电器上拔下插头时,Android摄像头自动对焦停止,android,camera,zxing,autofocus,Android,Camera,Zxing,Autofocus,我在应用程序中使用条形码扫描仪库时遇到以下错误:只有当设备插入充电器时,相机自动对焦才能工作。我一拔掉设备插头,相机就会停止连续自动对焦,但一旦再次拔掉设备插头,自动对焦就会立即恢复(图像每2000毫秒对焦一次)。测试装置的电池已充满电 应该注意的是,XZing的示例代码工作正常,但由于我在我的应用程序中使用了其中的一部分,所以我遇到了上述问题 以下是AutoFocusManager类的代码: /*Copyright (C) 2012 ZXing authors * * Licensed u

我在应用程序中使用条形码扫描仪库时遇到以下错误:只有当设备插入充电器时,相机自动对焦才能工作。我一拔掉设备插头,相机就会停止连续自动对焦,但一旦再次拔掉设备插头,自动对焦就会立即恢复(图像每2000毫秒对焦一次)。测试装置的电池已充满电

应该注意的是,XZing的示例代码工作正常,但由于我在我的应用程序中使用了其中的一部分,所以我遇到了上述问题

以下是AutoFocusManager类的代码:

/*Copyright (C) 2012 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.myapp.utils.barcode.camera;

import android.content.Context;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collection;

import com.myapp.utils.barcode.main.PreferencesActivity;
import com.myapp.utils.barcode.common.executor.AsyncTaskExecInterface;
import com.myapp.utils.barcode.common.executor.AsyncTaskExecManager;

final class AutoFocusManager implements Camera.AutoFocusCallback {

  private static final String TAG = AutoFocusManager.class.getSimpleName();

  private static final long AUTO_FOCUS_INTERVAL_MS = 2000L;
  private static final Collection<String> FOCUS_MODES_CALLING_AF;
  static {
    FOCUS_MODES_CALLING_AF = new ArrayList<String>(2);
    FOCUS_MODES_CALLING_AF.add(Camera.Parameters.FOCUS_MODE_AUTO);
    FOCUS_MODES_CALLING_AF.add(Camera.Parameters.FOCUS_MODE_MACRO);
  }

  private boolean active;
  private final boolean useAutoFocus;
  private final Camera camera;
  private AutoFocusTask outstandingTask;
  private final AsyncTaskExecInterface taskExec;

  AutoFocusManager(Context context, Camera camera) {
    this.camera = camera;
    taskExec = new AsyncTaskExecManager().build();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    String currentFocusMode = camera.getParameters().getFocusMode();
    useAutoFocus =
        sharedPrefs.getBoolean(PreferencesActivity.KEY_AUTO_FOCUS, true) &&
        FOCUS_MODES_CALLING_AF.contains(currentFocusMode);
    Log.i(TAG, "Current focus mode '" + currentFocusMode + "'; use auto focus? " + useAutoFocus);
    start();
  }

  @Override
  public synchronized void onAutoFocus(boolean success, Camera theCamera) {
    if (active) {
      outstandingTask = new AutoFocusTask();
      taskExec.execute(outstandingTask);
    }
  }

  synchronized void start() {
    if (useAutoFocus) {
      active = true;
      try {
        camera.autoFocus(this);
      } catch (RuntimeException re) {
        // Have heard RuntimeException reported in Android 4.0.x+; continue?
        Log.w(TAG, "Unexpected exception while focusing", re);
      }
    }
  }

  synchronized void stop() {
    if (useAutoFocus) {
      try {
        camera.cancelAutoFocus();
      } catch (RuntimeException re) {
        // Have heard RuntimeException reported in Android 4.0.x+; continue?
        Log.w(TAG, "Unexpected exception while cancelling focusing", re);
      }
    }
    if (outstandingTask != null) {
      outstandingTask.cancel(true);
      outstandingTask = null;
    }
    active = false;
  }

  private final class AutoFocusTask extends AsyncTask<Object,Object,Object> {
    @Override
    protected Object doInBackground(Object... voids) {
      try {
        Thread.sleep(AUTO_FOCUS_INTERVAL_MS);
      } catch (InterruptedException e) {
        // continue
      }
      synchronized (AutoFocusManager.this) {
        if (active) {
          start();
        }
      }
      return null;
    }
  }
}
测试设备是HTC sensation,OS-Cyanogenmod 10(安卓4.1.2)

你有什么建议这种行为的原因是什么


谢谢。

类似的情况也发生在我的zxing 2.1上

autoFocusThread X: AF fail
三星Galaxy(GT-P6200)搭载安卓4.0.4和Sonyericcson Xperia Neo/安卓4.0.4

ZXing 2.0没有出现错误。


试试看,我希望也能解决您的自动对焦问题。

如果在尝试扫描之前先拔掉设备插头,它是否仍然不能自动对焦?或者这只是当它开始插入,然后在相机工作时拔下插头时才发生的?只有当电源线插入时,自动对焦才会工作。所以,如果我在不插电源的情况下开始扫描,对焦就不起作用了。当我连接电源线时,它会按计划开始工作(每2秒一次),但一旦我拔下电源线,它就会停止工作。同样的行为是,如果它开始插入电源。所以,插上电源线似乎在这里起到了触发的作用。我的建议是设备进入另一种状态,相机的一些选项变得不可用。你认为,这有可能吗?但另一方面,原始代码工作正常。。谢谢。我真的不知道,我想说你对它的评估似乎是合理的,对我来说,你的特定设备和/或ROM听起来像是一个奇怪的怪癖。我刚刚在HTC Desire HD和三星Galaxy Note 2(库存版)上对它进行了测试问题仍然存在。如果您使用从市场下载的股票条形码扫描仪应用程序,您会这样做吗?
autoFocusThread X: AF fail