├── .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 | 15 | 16 | 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 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ZXingCamera 2 | 3 | 欢迎赞赏,以支持服务器、域名等费用 4 | 5 | ![](https://wx4.sinaimg.cn/mw690/668b990agy1g3fuiv1qx6j20fu0dpq57.jpg) 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 |