AutoFocusManager.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 cn.jymf.scan.camera;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import android.content.Context;
  20. import android.content.SharedPreferences;
  21. import android.hardware.Camera;
  22. import android.os.AsyncTask;
  23. import android.preference.PreferenceManager;
  24. import android.util.Log;
  25. import cn.jymf.scan.common.Runnable;
  26. import cn.jymf.scan.config.Config;
  27. /**
  28. * 由于对焦不是一次性完成的任务(手抖),而系统提供的对焦仅有Camera.autoFocus()方法,
  29. * 因此需要一个线程来不断调用Camera.autoFocus()直到用户满意按下快门为止
  30. */
  31. final class AutoFocusManager implements Camera.AutoFocusCallback {
  32. private static final String TAG = AutoFocusManager.class.getSimpleName();
  33. private static final long AUTO_FOCUS_INTERVAL_MS = 2000L;
  34. private static final Collection<String> FOCUS_MODES_CALLING_AF;
  35. static {
  36. FOCUS_MODES_CALLING_AF = new ArrayList<String>(2);
  37. FOCUS_MODES_CALLING_AF.add(Camera.Parameters.FOCUS_MODE_AUTO);
  38. FOCUS_MODES_CALLING_AF.add(Camera.Parameters.FOCUS_MODE_MACRO);
  39. }
  40. private boolean active;
  41. private final boolean useAutoFocus;
  42. private final Camera camera;
  43. private AsyncTask<?, ?, ?> outstandingTask;
  44. AutoFocusManager(Context context, Camera camera) {
  45. this.camera = camera;
  46. SharedPreferences sharedPrefs = PreferenceManager
  47. .getDefaultSharedPreferences(context);
  48. String currentFocusMode = camera.getParameters().getFocusMode();
  49. useAutoFocus = sharedPrefs.getBoolean(Config.KEY_AUTO_FOCUS, true)
  50. && FOCUS_MODES_CALLING_AF.contains(currentFocusMode);
  51. Log.i(TAG, "Current focus mode '" + currentFocusMode
  52. + "'; use auto focus? " + useAutoFocus);
  53. start();
  54. }
  55. @Override
  56. public synchronized void onAutoFocus(boolean success, Camera theCamera) {
  57. if (active) {
  58. outstandingTask = new AutoFocusTask();
  59. Runnable.execAsync(outstandingTask);
  60. }
  61. }
  62. synchronized void start() {
  63. if (useAutoFocus) {
  64. active = true;
  65. try {
  66. camera.autoFocus(this);
  67. }
  68. catch (RuntimeException re) {
  69. // Have heard RuntimeException reported in Android 4.0.x+;
  70. // continue?
  71. Log.w(TAG, "Unexpected exception while focusing", re);
  72. }
  73. }
  74. }
  75. synchronized void stop() {
  76. if (useAutoFocus) {
  77. try {
  78. camera.cancelAutoFocus();
  79. }
  80. catch (RuntimeException re) {
  81. // Have heard RuntimeException reported in Android 4.0.x+;
  82. // continue?
  83. Log.w(TAG, "Unexpected exception while cancelling focusing", re);
  84. }
  85. }
  86. if (outstandingTask != null) {
  87. outstandingTask.cancel(true);
  88. outstandingTask = null;
  89. }
  90. active = false;
  91. }
  92. private final class AutoFocusTask extends AsyncTask<Object, Object, Object> {
  93. @Override
  94. protected Object doInBackground(Object... voids) {
  95. try {
  96. Thread.sleep(AUTO_FOCUS_INTERVAL_MS);
  97. }
  98. catch (InterruptedException e) {
  99. // continue
  100. }
  101. synchronized (AutoFocusManager.this) {
  102. if (active) {
  103. start();
  104. }
  105. }
  106. return null;
  107. }
  108. }
  109. }