├── .gitignore
├── .idea
├── caches
│ └── build_file_checksums.ser
├── codeStyles
│ └── Project.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── io
│ │ └── github
│ │ └── dltech21
│ │ └── zxingcamera
│ │ ├── App.java
│ │ ├── CameraActivity.java
│ │ └── MainActivity.java
│ └── res
│ ├── drawable-v24
│ └── ic_launcher_foreground.xml
│ ├── drawable-xhdpi
│ └── paizhaoanniu2.png
│ ├── drawable
│ └── ic_launcher_background.xml
│ ├── layout
│ ├── activity_camera.xml
│ └── activity_main.xml
│ ├── mipmap-anydpi-v26
│ ├── ic_launcher.xml
│ └── ic_launcher_round.xml
│ ├── mipmap-hdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-mdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── zcamera
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
└── main
├── AndroidManifest.xml
├── java
└── com
│ └── dl
│ └── sdk
│ └── zcamera
│ └── camera
│ ├── AmbientLightManager.java
│ ├── AutoFocusManager.java
│ ├── CameraConfigurationManager.java
│ ├── CameraConfigurationUtils.java
│ ├── CameraManager.java
│ ├── FrontLightMode.java
│ ├── InactivityTimer.java
│ ├── PreviewCallback.java
│ └── open
│ ├── CameraFacing.java
│ ├── OpenCamera.java
│ └── OpenCameraInterface.java
└── res
└── values
└── strings.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
--------------------------------------------------------------------------------
/.idea/caches/build_file_checksums.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ZXingCamera
2 |
3 | 欢迎赞赏,以支持服务器、域名等费用
4 |
5 | 
6 |
7 | Camera for Android
8 |
9 | * 1.利用zxing的camera实现拍照
10 |
11 | 只需要引用库,查看demo中的CameraActivity
12 |
13 | ```java
14 | compile 'io.github.dltech21:zcamera:1.0.3'
15 | ```
16 |
17 |
18 | * 2.实时身份证ocr拍照,ocr准确率不太高,只是用于拍照识别,后续会做拍照后采用face++做一次校验
19 |
20 | 只需要引用库idcard_ocr,不再需要zcamera,查看demo,
21 | ```java
22 | compile 'io.github.dltech21:idcard_ocr:1.1.1'
23 | ```
24 |
25 | 支持armv7的包
26 | ```java
27 | compile 'io.github.dltech21:idcard_ocr:1.1.3'
28 | ```
29 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | buildToolsVersion rootProject.ext.buildToolsVersion
6 | defaultConfig {
7 | applicationId "io.github.dltech21.zxingcamera"
8 | minSdkVersion 15
9 | targetSdkVersion 27
10 | versionCode 1
11 | versionName "1.0"
12 | ndk {
13 | // 设置支持的 SO 库构架,一般而言,取你所有的库支持的构架的`交集`。
14 | abiFilters 'armeabi-v7a'// 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'
15 | }
16 | }
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | compile fileTree(include: ['*.jar'], dir: 'libs')
27 | compile 'com.android.support:appcompat-v7:27.1.1'
28 | compile 'io.github.dltech21:idcard_ocr:1.1.3'
29 | compile 'com.yanzhenjie:permission:2.0.0-rc4'
30 | implementation 'com.github.bumptech.glide:glide:4.7.1'
31 | annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
32 | }
33 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
19 |
22 |
23 |
26 |
27 |
35 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/app/src/main/java/io/github/dltech21/zxingcamera/App.java:
--------------------------------------------------------------------------------
1 | package io.github.dltech21.zxingcamera;
2 |
3 | import android.app.Application;
4 |
5 | public class App extends Application{
6 | @Override
7 | public void onCreate() {
8 | super.onCreate();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/java/io/github/dltech21/zxingcamera/CameraActivity.java:
--------------------------------------------------------------------------------
1 | package io.github.dltech21.zxingcamera;
2 |
3 | import android.app.Activity;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.hardware.Camera;
7 | import android.os.Bundle;
8 | import android.os.Environment;
9 | import android.os.Handler;
10 | import android.os.Message;
11 | import android.view.KeyEvent;
12 | import android.view.SurfaceHolder;
13 | import android.view.SurfaceView;
14 | import android.view.View;
15 | import android.view.Window;
16 | import android.view.WindowManager;
17 | import android.widget.ImageView;
18 |
19 | import com.bumptech.glide.Glide;
20 | import com.dl.sdk.zcamera.camera.AmbientLightManager;
21 | import com.dl.sdk.zcamera.camera.CameraManager;
22 | import com.dl.sdk.zcamera.camera.InactivityTimer;
23 |
24 | import java.io.File;
25 | import java.io.FileOutputStream;
26 | import java.io.IOException;
27 | import java.io.OutputStream;
28 |
29 | import io.github.dltech21.iddetect.util.BmpUtil;
30 |
31 |
32 | /**
33 | * Created by Donal on 2017/8/15.
34 | */
35 |
36 | public class CameraActivity extends Activity implements SurfaceHolder.Callback {
37 |
38 | private CameraManager cameraManager;
39 | private boolean hasSurface;
40 | private InactivityTimer inactivityTimer;
41 | private AmbientLightManager ambientLightManager;
42 |
43 | private ImageView imgPre;
44 |
45 | @Override
46 | public void onCreate(Bundle icicle) {
47 | super.onCreate(icicle);
48 | Window window = getWindow();
49 | window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
50 | setContentView(R.layout.activity_camera);
51 |
52 | hasSurface = false;
53 | inactivityTimer = new InactivityTimer(this);
54 | ambientLightManager = new AmbientLightManager(this);
55 |
56 | initView();
57 | }
58 |
59 | public void initView() {
60 | imgPre = (ImageView) findViewById(R.id.img_pre);
61 | }
62 |
63 | @Override
64 | protected void onResume() {
65 | super.onResume();
66 |
67 | // CameraManager must be initialized here, not in onCreate(). This is necessary because we don't
68 | // want to open the camera driver and measure the screen size if we're going to show the help on
69 | // first launch. That led to bugs where the scanning rectangle was the wrong size and partially
70 | // off screen.
71 | cameraManager = new CameraManager(getApplication());
72 |
73 | ambientLightManager.start(cameraManager);
74 |
75 | inactivityTimer.onResume();
76 |
77 |
78 | SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
79 | SurfaceHolder surfaceHolder = surfaceView.getHolder();
80 | if (hasSurface) {
81 | // The activity was paused but not stopped, so the surface still exists. Therefore
82 | // surfaceCreated() won't be called, so init the camera here.
83 | initCamera(surfaceHolder);
84 | } else {
85 | // Install the callback and wait for surfaceCreated() to init the camera.
86 | surfaceHolder.addCallback(this);
87 | }
88 | }
89 |
90 | @Override
91 | protected void onPause() {
92 | cameraManager.stopPreview();
93 | inactivityTimer.onPause();
94 | ambientLightManager.stop();
95 | cameraManager.closeDriver();
96 | if (!hasSurface) {
97 | SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
98 | SurfaceHolder surfaceHolder = surfaceView.getHolder();
99 | surfaceHolder.removeCallback(this);
100 | }
101 | super.onPause();
102 |
103 | }
104 |
105 | @Override
106 | protected void onDestroy() {
107 | inactivityTimer.shutdown();
108 | super.onDestroy();
109 | }
110 |
111 | @Override
112 | public boolean onKeyDown(int keyCode, KeyEvent event) {
113 | switch (keyCode) {
114 | // Use volume up/down to turn on light
115 | case KeyEvent.KEYCODE_VOLUME_DOWN:
116 | cameraManager.setTorch(false);
117 | return true;
118 | case KeyEvent.KEYCODE_VOLUME_UP:
119 | cameraManager.setTorch(true);
120 | return true;
121 | }
122 | return super.onKeyDown(keyCode, event);
123 | }
124 |
125 | @Override
126 | public void surfaceCreated(SurfaceHolder holder) {
127 | if (holder == null) {
128 | System.out.println("*** WARNING *** surfaceCreated() gave us a null surface!");
129 | }
130 | if (!hasSurface) {
131 | hasSurface = true;
132 | initCamera(holder);
133 | }
134 | }
135 |
136 | @Override
137 | public void surfaceDestroyed(SurfaceHolder holder) {
138 | hasSurface = false;
139 | }
140 |
141 | @Override
142 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
143 |
144 | }
145 |
146 | private void initCamera(SurfaceHolder surfaceHolder) {
147 | if (surfaceHolder == null) {
148 | throw new IllegalStateException("No SurfaceHolder provided");
149 | }
150 | if (cameraManager.isOpen()) {
151 | System.out.println("initCamera() while already open -- late SurfaceView callback?");
152 | return;
153 | }
154 | try {
155 | cameraManager.openDriver(surfaceHolder);
156 | cameraManager.startPreview();
157 | cameraManager.setPictureCallback(pictureCallback);
158 | } catch (IOException ioe) {
159 | System.out.println(ioe);
160 | } catch (RuntimeException e) {
161 | System.out.println("Unexpected error initializing camera" + e);
162 | }
163 | }
164 |
165 |
166 | private final Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
167 | @Override
168 | public void onPictureTaken(final byte[] data, Camera camera) {
169 | new SavePicTask(data, camera).start();
170 | }
171 | };
172 |
173 | public void takePicture(View view) {
174 | cameraManager.takePicture();
175 | }
176 |
177 | String photoPath;
178 |
179 | private class SavePicTask extends Thread {
180 | private byte[] data;
181 | private Camera camera;
182 |
183 | SavePicTask(byte[] data, Camera camera) {
184 | this.data = data;
185 | this.camera = camera;
186 | }
187 |
188 | @Override
189 | public void run() {
190 | super.run();
191 | Message msg = handler.obtainMessage();
192 | if (saveToSDCard(data)) {
193 | msg.obj = camera;
194 | msg.what = 1;
195 | } else {
196 | msg.what = 0;
197 | }
198 | handler.sendMessage(msg);
199 | }
200 |
201 | private boolean saveToSDCard(byte[] data) {
202 | String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/zxingcamera";
203 | File file = new File(dir);
204 | if (!file.exists()) {
205 | file.mkdirs();
206 | }
207 | photoPath = dir + File.separator + System.currentTimeMillis() + ".png";
208 | saveOriginal(data, photoPath);
209 | try {
210 | Bitmap bitmap = BmpUtil.getRotateBitmap(BitmapFactory.decodeFile(photoPath), 90.0f, true);
211 | BmpUtil.saveBmpFile(bitmap, photoPath);
212 | } catch (Exception e) {
213 | e.printStackTrace();
214 | }
215 | return true;
216 | }
217 |
218 | private void saveOriginal(byte[] data, String path) {
219 | File file = new File(path);
220 | OutputStream os = null;
221 | try {
222 | os = new FileOutputStream(file);
223 | os.write(data);
224 | os.close();
225 | } catch (IOException e) {
226 | e.printStackTrace();
227 | } finally {
228 | if (os != null) {
229 | try {
230 | os.close();
231 | } catch (IOException e) {
232 | e.printStackTrace();
233 | }
234 | }
235 | }
236 | }
237 |
238 | }
239 |
240 | private Handler handler = new Handler() {
241 |
242 | @Override
243 | public void handleMessage(Message msg) {
244 | if (msg.what == 1) {
245 | Camera camera = (Camera) msg.obj;
246 | if (camera != null) {
247 | Glide.with(CameraActivity.this).load("file://" + photoPath).into((ImageView) findViewById(R.id.img_pre));
248 | camera.startPreview();
249 | if (cameraManager.getAutoFocusManager() != null) {
250 | cameraManager.getAutoFocusManager().start();
251 | }
252 | }
253 |
254 | }
255 | }
256 | };
257 |
258 |
259 | }
--------------------------------------------------------------------------------
/app/src/main/java/io/github/dltech21/zxingcamera/MainActivity.java:
--------------------------------------------------------------------------------
1 | package io.github.dltech21.zxingcamera;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.graphics.BitmapFactory;
6 | import android.os.Bundle;
7 | import android.support.annotation.Nullable;
8 | import android.view.View;
9 | import android.widget.ImageView;
10 | import android.widget.TextView;
11 |
12 | import com.yanzhenjie.permission.Action;
13 | import com.yanzhenjie.permission.AndPermission;
14 | import com.yanzhenjie.permission.Permission;
15 |
16 | import java.util.List;
17 |
18 | import io.github.dltech21.ocr.IDCardEnum;
19 | import io.github.dltech21.ocr.IdentityInfo;
20 | import io.github.dltech21.ocr.OcrCameraActivity;
21 | import io.github.dltech21.ocr.OcrConfig;
22 |
23 | public class MainActivity extends Activity {
24 |
25 | @Override
26 | protected void onCreate(@Nullable Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_main);
29 |
30 | AndPermission.with(this)
31 | .permission(new String[]{Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE, Permission.CAMERA})
32 | .onGranted(new Action() {
33 | @Override
34 | public void onAction(List permissions) {
35 | }
36 | })
37 | .start();
38 |
39 | findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {
40 | @Override
41 | public void onClick(View v) {
42 | startActivity(new Intent(MainActivity.this, CameraActivity.class));
43 | }
44 | });
45 |
46 | findViewById(R.id.bt2).setOnClickListener(new View.OnClickListener() {
47 | @Override
48 | public void onClick(View v) {
49 | OcrCameraActivity.open(MainActivity.this, IDCardEnum.FaceEmblem, 1001);
50 | }
51 | });
52 |
53 | findViewById(R.id.bt3).setOnClickListener(new View.OnClickListener() {
54 | @Override
55 | public void onClick(View v) {
56 | OcrCameraActivity.open(MainActivity.this, IDCardEnum.NationalEmblem, 1001);
57 | }
58 | });
59 | }
60 |
61 | @Override
62 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
63 | super.onActivityResult(requestCode, resultCode, data);
64 | if (resultCode == RESULT_OK && requestCode == 1001) {
65 | String filepath = data.getStringExtra(OcrConfig.OCR_PHOTO_PATH);
66 | IdentityInfo identityInfo = (IdentityInfo) data.getSerializableExtra(OcrConfig.OCR_IDENTITYINFO);
67 | StringBuffer localStringBuffer = new StringBuffer();
68 | localStringBuffer.append("姓名:").append(identityInfo.getName()).append("\n");
69 | localStringBuffer.append("身份号码:").append(identityInfo.getCertid()).append("\n");
70 | localStringBuffer.append("性别:").append(identityInfo.getSex()).append("\n");
71 | localStringBuffer.append("民族:").append(identityInfo.getFork()).append("\n");
72 | localStringBuffer.append("出生:").append(identityInfo.getBirthday()).append("\n");
73 | localStringBuffer.append("住址:").append(identityInfo.getAddress()).append("\n");
74 | localStringBuffer.append("签发机关:").append(identityInfo.getIssue_authority()).append("\n");
75 | localStringBuffer.append("有效期限:").append(identityInfo.getVaild_priod()).append("\n");
76 | localStringBuffer.append(identityInfo.getType() == IDCardEnum.FaceEmblem ? "人像面" : "国徽面").append("\n");
77 | ((TextView) findViewById(R.id.idresult)).setText(localStringBuffer.toString());
78 | ((ImageView) findViewById(R.id.idimgview)).setImageBitmap(BitmapFactory.decodeFile(filepath));
79 | }
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/paizhaoanniu2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/drawable-xhdpi/paizhaoanniu2.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_camera.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
19 |
20 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
20 |
21 |
27 |
28 |
33 |
34 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ZXingCamera
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | maven {
7 | url 'https://maven.google.com/'
8 | name 'Google'
9 | }
10 | jcenter()
11 | }
12 | dependencies {
13 | classpath 'com.android.tools.build:gradle:3.0.1'
14 | classpath 'com.novoda:bintray-release:0.8.1'
15 |
16 | // NOTE: Do not place your application dependencies here; they belong
17 | // in the individual module build.gradle files
18 | }
19 | }
20 |
21 | allprojects {
22 | repositories {
23 | maven {
24 | url 'https://maven.google.com/'
25 | name 'Google'
26 | }
27 | jcenter()
28 | }
29 | }
30 |
31 | task clean(type: Delete) {
32 | delete rootProject.buildDir
33 | }
34 |
35 |
36 |
37 | ext {
38 | targetSdkVersion = 27
39 | compileSdkVersion = 27
40 | buildToolsVersion = '27.0.3'
41 | supportVersion = '27.1.1'
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/DLTech21/ZXingCamera/c290ad70791f244d3ecce59de7d8368b82339f40/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Jun 28 13:45:24 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.3-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | include ':zcamera'
3 |
--------------------------------------------------------------------------------
/zcamera/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/zcamera/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.novoda.bintray-release'
3 | android {
4 | compileSdkVersion rootProject.ext.compileSdkVersion
5 | buildToolsVersion rootProject.ext.buildToolsVersion
6 |
7 | defaultConfig {
8 | minSdkVersion 15
9 | targetSdkVersion rootProject.ext.targetSdkVersion
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | compile fileTree(dir: 'libs', include: ['*.jar'])
26 | }
27 |
28 | publish {
29 | userOrg = 'dltech21'
30 | groupId = 'io.github.dltech21'
31 | artifactId = 'zcamera'
32 | publishVersion = '1.0.3'
33 | desc = 'camera for Android'
34 | website = 'https://github.com/DLTech21/ZXingCamera'
35 | }
--------------------------------------------------------------------------------
/zcamera/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/Donal/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/zcamera/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/zcamera/src/main/java/com/dl/sdk/zcamera/camera/AmbientLightManager.java:
--------------------------------------------------------------------------------
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 |
17 | package com.dl.sdk.zcamera.camera;
18 |
19 | import android.content.Context;
20 | import android.content.SharedPreferences;
21 | import android.hardware.Sensor;
22 | import android.hardware.SensorEvent;
23 | import android.hardware.SensorEventListener;
24 | import android.hardware.SensorManager;
25 | import android.preference.PreferenceManager;
26 |
27 | /**
28 | * Detects ambient light and switches on the front light when very dark, and off again when sufficiently light.
29 | *
30 | * @author Sean Owen
31 | * @author Nikolaus Huber
32 | */
33 | public class AmbientLightManager implements SensorEventListener {
34 |
35 | private static final float TOO_DARK_LUX = 45.0f;
36 | private static final float BRIGHT_ENOUGH_LUX = 450.0f;
37 |
38 | private final Context context;
39 | private CameraManager cameraManager;
40 | private Sensor lightSensor;
41 |
42 | public AmbientLightManager(Context context) {
43 | this.context = context;
44 | }
45 |
46 | public void start(CameraManager cameraManager) {
47 | this.cameraManager = cameraManager;
48 | SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
49 | if (FrontLightMode.readPref(sharedPrefs) == FrontLightMode.AUTO) {
50 | SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
51 | lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
52 | if (lightSensor != null) {
53 | sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
54 | }
55 | }
56 | }
57 |
58 | public void stop() {
59 | if (lightSensor != null) {
60 | SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
61 | sensorManager.unregisterListener(this);
62 | cameraManager = null;
63 | lightSensor = null;
64 | }
65 | }
66 |
67 | @Override
68 | public void onSensorChanged(SensorEvent sensorEvent) {
69 | float ambientLightLux = sensorEvent.values[0];
70 | if (cameraManager != null) {
71 | if (ambientLightLux <= TOO_DARK_LUX) {
72 | cameraManager.setTorch(true);
73 | } else if (ambientLightLux >= BRIGHT_ENOUGH_LUX) {
74 | cameraManager.setTorch(false);
75 | }
76 | }
77 | }
78 |
79 | @Override
80 | public void onAccuracyChanged(Sensor sensor, int accuracy) {
81 | // do nothing
82 | }
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/zcamera/src/main/java/com/dl/sdk/zcamera/camera/AutoFocusManager.java:
--------------------------------------------------------------------------------
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 |
17 | package com.dl.sdk.zcamera.camera;
18 |
19 | import android.content.Context;
20 | import android.content.SharedPreferences;
21 | import android.graphics.Point;
22 | import android.graphics.Rect;
23 | import android.hardware.Camera;
24 | import android.os.AsyncTask;
25 | import android.os.Build;
26 | import android.preference.PreferenceManager;
27 | import android.util.Log;
28 |
29 | import java.util.ArrayList;
30 | import java.util.Collection;
31 | import java.util.List;
32 | import java.util.concurrent.RejectedExecutionException;
33 |
34 | @SuppressWarnings("deprecation") // camera APIs
35 | public final class AutoFocusManager implements Camera.AutoFocusCallback {
36 |
37 | private static final String TAG = AutoFocusManager.class.getSimpleName();
38 |
39 | private static final long AUTO_FOCUS_INTERVAL_MS = 2000L;
40 | private static final Collection FOCUS_MODES_CALLING_AF;
41 |
42 | static {
43 | FOCUS_MODES_CALLING_AF = new ArrayList<>(2);
44 | FOCUS_MODES_CALLING_AF.add(Camera.Parameters.FOCUS_MODE_AUTO);
45 | FOCUS_MODES_CALLING_AF.add(Camera.Parameters.FOCUS_MODE_MACRO);
46 | }
47 |
48 | private boolean stopped;
49 | private boolean focusing;
50 | private final boolean useAutoFocus;
51 | private final Camera camera;
52 | private AsyncTask, ?, ?> outstandingTask;
53 | private Context context;
54 |
55 | AutoFocusManager(Context context, Camera camera) {
56 | this.camera = camera;
57 | this.context = context;
58 | SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
59 | String currentFocusMode = camera.getParameters().getFocusMode();
60 | useAutoFocus =
61 | // sharedPrefs.getBoolean(PreferencesActivity.KEY_AUTO_FOCUS, true) &&
62 | FOCUS_MODES_CALLING_AF.contains(currentFocusMode);
63 | Log.i(TAG, "Current focus mode '" + currentFocusMode + "'; use auto focus? " + useAutoFocus);
64 | start();
65 | }
66 |
67 | @Override
68 | public synchronized void onAutoFocus(boolean success, Camera theCamera) {
69 | focusing = false;
70 | autoFocusAgainLater();
71 | }
72 |
73 | private synchronized void autoFocusAgainLater() {
74 | if (!stopped && outstandingTask == null) {
75 | AutoFocusTask newTask = new AutoFocusTask();
76 | try {
77 | newTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
78 | outstandingTask = newTask;
79 | } catch (RejectedExecutionException ree) {
80 | Log.w(TAG, "Could not request auto focus", ree);
81 | }
82 | }
83 | }
84 |
85 | public synchronized void start() {
86 | if (useAutoFocus) {
87 | outstandingTask = null;
88 | if (!stopped && !focusing) {
89 | try {
90 | // DisplayMetrics mDisplayMetrics = context.getResources().getDisplayMetrics();
91 | // int screenWidth = mDisplayMetrics.widthPixels;
92 | // Point point = new Point(screenWidth / 2, screenWidth / 2);
93 | // focusing = onFocus(point, this);
94 | if (camera.getParameters().getFocusMode().equals(Camera.Parameters.FOCUS_MODE_AUTO)) {
95 | camera.autoFocus(this);
96 | }
97 | focusing = true;
98 | } catch (RuntimeException re) {
99 | // Have heard RuntimeException reported in Android 4.0.x+; continue?
100 | Log.w(TAG, "Unexpected exception while focusing", re);
101 | // Try again later to keep cycle going
102 | autoFocusAgainLater();
103 | }
104 | }
105 | }
106 | }
107 |
108 | /**
109 | * 手动聚焦
110 | *
111 | * @param point 触屏坐标
112 | */
113 | protected boolean onFocus(Point point, Camera.AutoFocusCallback callback) {
114 | if (camera == null) {
115 | return false;
116 | }
117 |
118 | Camera.Parameters parameters = null;
119 | try {
120 | parameters = camera.getParameters();
121 | } catch (Exception e) {
122 | e.printStackTrace();
123 | return false;
124 | }
125 | //不支持设置自定义聚焦,则使用自动聚焦,返回
126 |
127 | if (Build.VERSION.SDK_INT >= 14) {
128 |
129 | if (parameters.getMaxNumFocusAreas() <= 0) {
130 | return focus(callback);
131 | }
132 |
133 | Log.i(TAG, "onCameraFocus:" + point.x + "," + point.y);
134 |
135 | List areas = new ArrayList();
136 | int left = point.x - 300;
137 | int top = point.y - 300;
138 | int right = point.x + 300;
139 | int bottom = point.y + 300;
140 | left = left < -1000 ? -1000 : left;
141 | top = top < -1000 ? -1000 : top;
142 | right = right > 1000 ? 1000 : right;
143 | bottom = bottom > 1000 ? 1000 : bottom;
144 | areas.add(new Camera.Area(new Rect(left, top, right, bottom), 100));
145 | parameters.setFocusAreas(areas);
146 | try {
147 | //本人使用的小米手机在设置聚焦区域的时候经常会出异常,看日志发现是框架层的字符串转int的时候出错了,
148 | //目测是小米修改了框架层代码导致,在此try掉,对实际聚焦效果没影响
149 | camera.setParameters(parameters);
150 | } catch (Exception e) {
151 | // TODO: handle exception
152 | e.printStackTrace();
153 | return false;
154 | }
155 | }
156 |
157 | return focus(callback);
158 | }
159 |
160 | private boolean focus(Camera.AutoFocusCallback callback) {
161 | try {
162 | camera.autoFocus(callback);
163 | } catch (Exception e) {
164 | e.printStackTrace();
165 | return false;
166 | }
167 | return true;
168 | }
169 |
170 | private synchronized void cancelOutstandingTask() {
171 | if (outstandingTask != null) {
172 | if (outstandingTask.getStatus() != AsyncTask.Status.FINISHED) {
173 | outstandingTask.cancel(true);
174 | }
175 | outstandingTask = null;
176 | }
177 | }
178 |
179 | synchronized void stop() {
180 | stopped = true;
181 | if (useAutoFocus) {
182 | cancelOutstandingTask();
183 | // Doesn't hurt to call this even if not focusing
184 | try {
185 | camera.cancelAutoFocus();
186 | } catch (RuntimeException re) {
187 | // Have heard RuntimeException reported in Android 4.0.x+; continue?
188 | Log.w(TAG, "Unexpected exception while cancelling focusing", re);
189 | }
190 | }
191 | }
192 |
193 | private final class AutoFocusTask extends AsyncTask