AutoFocusManager.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.zxing.client.android.camera;
  17. import android.content.Context;
  18. import android.hardware.Camera;
  19. import android.os.AsyncTask;
  20. import android.util.Log;
  21. import me.yoqi.qrcode.Config;
  22. import java.util.ArrayList;
  23. import java.util.Collection;
  24. import com.google.zxing.client.android.common.executor.AsyncTaskExecInterface;
  25. import com.google.zxing.client.android.common.executor.AsyncTaskExecManager;
  26. final class AutoFocusManager implements Camera.AutoFocusCallback {
  27. private static final String TAG = AutoFocusManager.class.getSimpleName();
  28. private static final long AUTO_FOCUS_INTERVAL_MS = 2500L;
  29. private static final Collection<String> FOCUS_MODES_CALLING_AF;
  30. static {
  31. FOCUS_MODES_CALLING_AF = new ArrayList<String>(2);
  32. FOCUS_MODES_CALLING_AF.add(Camera.Parameters.FOCUS_MODE_AUTO);
  33. FOCUS_MODES_CALLING_AF.add(Camera.Parameters.FOCUS_MODE_MACRO);
  34. }
  35. private boolean active;
  36. private final boolean useAutoFocus;
  37. private final Camera camera;
  38. private AutoFocusTask outstandingTask;
  39. private final AsyncTaskExecInterface taskExec;
  40. AutoFocusManager(Context context, Camera camera) {
  41. this.camera = camera;
  42. taskExec = new AsyncTaskExecManager().build();
  43. String currentFocusMode = camera.getParameters().getFocusMode();
  44. useAutoFocus =Config.AOTO_FOCUS && FOCUS_MODES_CALLING_AF.contains(currentFocusMode);
  45. Log.i(TAG, "Current focus mode '" + currentFocusMode + "'; use auto focus? " + useAutoFocus);
  46. start();
  47. }
  48. @Override
  49. public synchronized void onAutoFocus(boolean success, Camera theCamera) {
  50. if (active) {
  51. Log.i(TAG, "onAutoFocus hit");
  52. outstandingTask = new AutoFocusTask();
  53. taskExec.execute(outstandingTask);
  54. }
  55. }
  56. synchronized void start() {
  57. if (useAutoFocus) {
  58. active = true;
  59. try {
  60. camera.autoFocus(this);
  61. } catch (RuntimeException re) {
  62. // Have heard RuntimeException reported in Android 4.0.x+; continue?
  63. Log.w(TAG, "Unexpected exception while focusing", re);
  64. }
  65. }
  66. }
  67. synchronized void stop() {
  68. if (useAutoFocus) {
  69. try {
  70. camera.cancelAutoFocus();
  71. } catch (RuntimeException re) {
  72. // Have heard RuntimeException reported in Android 4.0.x+; continue?
  73. Log.w(TAG, "Unexpected exception while cancelling focusing", re);
  74. }
  75. }
  76. if (outstandingTask != null) {
  77. outstandingTask.cancel(true);
  78. outstandingTask = null;
  79. }
  80. active = false;
  81. }
  82. private final class AutoFocusTask extends AsyncTask<Object,Object,Object> {
  83. @Override
  84. protected Object doInBackground(Object... voids) {
  85. try {
  86. Thread.sleep(AUTO_FOCUS_INTERVAL_MS);
  87. } catch (InterruptedException e) {
  88. // continue
  89. }
  90. synchronized (AutoFocusManager.this) {
  91. if (active) {
  92. start();
  93. }
  94. }
  95. return null;
  96. }
  97. }
  98. }