├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── business_android_client │ │ └── croppercammer │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── business_android_client │ │ │ └── croppercammer │ │ │ ├── ClipCamera.java │ │ │ ├── MainActivity.java │ │ │ ├── PreView.java │ │ │ └── view │ │ │ └── shadow.java │ └── res │ │ ├── drawable │ │ ├── 1.webp │ │ ├── 2.jpg │ │ ├── 3.jpg │ │ ├── cancle.png │ │ ├── cancle_wite.png │ │ ├── sure.png │ │ └── sure_wite.png │ │ ├── layout │ │ ├── activity_main.xml │ │ └── activity_pre_view.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 │ └── test │ └── java │ └── business_android_client │ └── croppercammer │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CropperCammer/Android自定义身份证拍照相机 2 | 专业拍摄身份证20年!! 3 | 自定义拍照相机,拍完照片可以直接获取到裁剪好的图片,主要针对身份证拍照裁剪,宽高比例为1.6 4 | ![image](https://github.com/tianyalian/CropperCammer/blob/master/app/src/main/res/drawable/2.jpg) 5 | ![image](https://github.com/tianyalian/CropperCammer/blob/master/app/src/main/res/drawable/3.jpg) 6 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | defaultConfig { 7 | applicationId "business_android_client.croppercammer" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.1' 28 | compile 'com.android.support.constraint:constraint-layout:1.0.0' 29 | testCompile 'junit:junit:4.12' 30 | } 31 | -------------------------------------------------------------------------------- /app/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 D:\ake\AndroidStusio\AndroidSdk/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 | -------------------------------------------------------------------------------- /app/src/androidTest/java/business_android_client/croppercammer/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package business_android_client.croppercammer; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("business_android_client.croppercammer", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/business_android_client/croppercammer/ClipCamera.java: -------------------------------------------------------------------------------- 1 | package business_android_client.croppercammer; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.graphics.Bitmap; 6 | import android.graphics.BitmapFactory; 7 | import android.graphics.Matrix; 8 | import android.hardware.Camera; 9 | import android.net.Uri; 10 | import android.os.Environment; 11 | import android.util.AttributeSet; 12 | import android.util.DisplayMetrics; 13 | import android.util.Log; 14 | import android.view.MotionEvent; 15 | import android.view.SurfaceHolder; 16 | import android.view.SurfaceView; 17 | import android.view.WindowManager; 18 | import android.widget.RelativeLayout; 19 | import android.widget.Toast; 20 | 21 | import java.io.BufferedOutputStream; 22 | import java.io.File; 23 | import java.io.FileOutputStream; 24 | import java.io.IOException; 25 | import java.util.List; 26 | 27 | /** 28 | * Created by seeker on 2017/5/24. 29 | */ 30 | 31 | public class ClipCamera extends SurfaceView implements SurfaceHolder.Callback, Camera.AutoFocusCallback { 32 | private static final String TAG = "ClipCamera:"; 33 | private int mScreenWidth; 34 | private int mScreenHeight; 35 | Context ctx; 36 | private SurfaceHolder holder; 37 | private Camera mCamera; 38 | 39 | public ClipCamera(Context context) { 40 | this(context,null); 41 | } 42 | 43 | public ClipCamera(Context context, AttributeSet attrs) { 44 | this(context, attrs,0); 45 | } 46 | 47 | public ClipCamera(Context context, AttributeSet attrs, int defStyleAttr) { 48 | super(context, attrs, defStyleAttr); 49 | ctx = context; 50 | initView(); 51 | } 52 | 53 | 54 | private void initView() { 55 | getScreenMetrix(ctx); 56 | holder = getHolder();//获得surfaceHolder引用 57 | holder.addCallback(this); 58 | holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置类型 59 | } 60 | 61 | /** 62 | * 获取屏幕的宽高 63 | * @param context 64 | */ 65 | private void getScreenMetrix(Context context) { 66 | WindowManager WM = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 67 | DisplayMetrics outMetrics = new DisplayMetrics(); 68 | WM.getDefaultDisplay().getMetrics(outMetrics); 69 | mScreenWidth = outMetrics.widthPixels; 70 | mScreenHeight = outMetrics.heightPixels; 71 | } 72 | 73 | 74 | @Override 75 | public void surfaceCreated(SurfaceHolder holder) { 76 | if (mCamera == null) { 77 | mCamera = Camera.open();//开启相机 78 | setCameraParams(mCamera,mScreenWidth,mScreenHeight); 79 | try { 80 | mCamera.setPreviewDisplay(holder);//摄像头画面显示在Surface上 81 | } catch (IOException e) { 82 | e.printStackTrace(); 83 | } 84 | } 85 | } 86 | 87 | @Override 88 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 89 | mCamera.startPreview(); 90 | 91 | } 92 | 93 | @Override 94 | public void surfaceDestroyed(SurfaceHolder holder) { 95 | mCamera.stopPreview();//停止预览 96 | mCamera.release();//释放相机资源 97 | mCamera = null; 98 | holder = null; 99 | } 100 | 101 | @Override 102 | public void onAutoFocus(boolean success, Camera camera) { 103 | if (success) { 104 | Log.i(TAG, "onAutoFocus success="+success); 105 | } 106 | } 107 | 108 | private void setCameraParams(Camera camera, int width, int height) { 109 | Log.i(TAG,"setCameraParams width="+width+" height="+height); 110 | Camera.Parameters parameters = mCamera.getParameters(); 111 | // 获取摄像头支持的PictureSize列表 112 | List pictureSizeList = parameters.getSupportedPictureSizes(); 113 | for (Camera.Size size : pictureSizeList) { 114 | Log.i(TAG, "pictureSizeList size.width=" + size.width + " size.height=" + size.height); 115 | } 116 | /**从列表中选取合适的分辨率*/ 117 | Camera.Size picSize = getProperSize(pictureSizeList, ((float) height / width)); 118 | if (null == picSize) { 119 | Log.i(TAG, "null == picSize"); 120 | picSize = parameters.getPictureSize(); 121 | } 122 | Log.i(TAG, "picSize.width=" + picSize.width + " picSize.height=" + picSize.height); 123 | // 根据选出的PictureSize重新设置SurfaceView大小 124 | float w = picSize.width; 125 | float h = picSize.height; 126 | parameters.setPictureSize(picSize.width,picSize.height); 127 | this.setLayoutParams(new RelativeLayout.LayoutParams((int) (height*(h/w)), height)); 128 | 129 | // 获取摄像头支持的PreviewSize列表 130 | List previewSizeList = parameters.getSupportedPreviewSizes(); 131 | 132 | for (Camera.Size size : previewSizeList) { 133 | Log.i(TAG, "previewSizeList size.width=" + size.width + " size.height=" + size.height); 134 | } 135 | Camera.Size preSize = getProperSize(previewSizeList, ((float) height) / width); 136 | if (null != preSize) { 137 | Log.i(TAG, "preSize.width=" + preSize.width + " preSize.height=" + preSize.height); 138 | parameters.setPreviewSize(preSize.width, preSize.height); 139 | } 140 | 141 | parameters.setJpegQuality(100); // 设置照片质量 142 | if (parameters.getSupportedFocusModes().contains(android.hardware.Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { 143 | parameters.setFocusMode(android.hardware.Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 连续对焦模式 144 | } 145 | 146 | mCamera.cancelAutoFocus();//自动对焦。 147 | mCamera.setDisplayOrientation(90);// 设置PreviewDisplay的方向,效果就是将捕获的画面旋转多少度显示 148 | mCamera.setParameters(parameters); 149 | 150 | } 151 | 152 | /** 153 | * 从列表中选取合适的分辨率 154 | * 默认w:h = 4:3 155 | *

注意:这里的w对应屏幕的height 156 | * h对应屏幕的width

157 | */ 158 | private Camera.Size getProperSize(List pictureSizeList, float screenRatio) { 159 | Log.i(TAG, "screenRatio=" + screenRatio); 160 | Camera.Size result = null; 161 | for (Camera.Size size : pictureSizeList) { 162 | float currentRatio = ((float) size.width) / size.height; 163 | if (currentRatio - screenRatio == 0) { 164 | result = size; 165 | break; 166 | } 167 | } 168 | 169 | if (null == result) { 170 | for (Camera.Size size : pictureSizeList) { 171 | float curRatio = ((float) size.width) / size.height; 172 | if (curRatio == 4f / 3) {// 默认w:h = 4:3 173 | result = size; 174 | break; 175 | } 176 | } 177 | } 178 | 179 | return result; 180 | } 181 | 182 | 183 | //创建jpeg图片回调数据对象 184 | private Camera.PictureCallback jpeg = new Camera.PictureCallback() { 185 | 186 | @Override 187 | public void onPictureTaken(byte[] data, Camera Camera) { 188 | BufferedOutputStream bos = null; 189 | Bitmap bm = null; 190 | try { 191 | // 获得图片 192 | bm = BitmapFactory.decodeByteArray(data, 0, data.length); 193 | int pic_width = bm.getWidth();//1280 194 | int pic_height= bm.getHeight();//720 195 | int height,width,x_center,y_center; 196 | height = (int) (pic_height * 0.8);//屏幕宽的0.8,拍照取景框的宽为屏幕的0.8 197 | width = (int) (height * 1.6); 198 | x_center=pic_width/2; 199 | y_center=pic_height/2; 200 | 201 | 202 | Matrix matrix = new Matrix(); 203 | matrix.postRotate(360,pic_width/2,pic_height/2); 204 | bm = Bitmap.createBitmap(bm, x_center - (width / 2) , 205 | y_center - (height / 2) , 206 | (int) (pic_height*0.8*1.6), 207 | (int) (pic_height*0.8), 208 | matrix,false); 209 | 210 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 211 | Log.i(TAG, "Environment.getExternalStorageDirectory()="+Environment.getExternalStorageDirectory()); 212 | // String filePath = "/sdcard/dyk"+System.currentTimeMillis()+".jpg";//照片保存路径 213 | File file = new File(savePath); 214 | if (!file.exists()){ 215 | file.createNewFile(); 216 | } 217 | bos = new BufferedOutputStream(new FileOutputStream(file)); 218 | bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);//将图片压缩到流中 219 | 220 | }else{ 221 | Toast.makeText(ctx,"没有检测到内存卡", Toast.LENGTH_SHORT).show(); 222 | } 223 | } catch (Exception e) { 224 | e.printStackTrace(); 225 | } finally { 226 | try { 227 | bos.flush();//输出 228 | bos.close();//关闭 229 | bm.recycle();// 回收bitmap空间 230 | mCamera.stopPreview();// 关闭预览 231 | Uri uri = Uri.fromFile(new File(savePath)); 232 | Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 233 | intent.setData(uri); 234 | ctx.sendBroadcast(intent); 235 | ctx.startActivity(intent.setClass(ctx,PreView.class)); 236 | } catch (IOException e) { 237 | e.printStackTrace(); 238 | 239 | 240 | 241 | 242 | } 243 | } 244 | 245 | } 246 | }; 247 | 248 | String savePath; 249 | public void takePicture(String savePath){ 250 | this.savePath=savePath; 251 | //设置参数,并拍照 252 | setCameraParams(mCamera, mScreenWidth, mScreenHeight); 253 | // 当调用camera.takePiture方法后,camera关闭了预览,这时需要调用startPreview()来重新开启预览 254 | mCamera.takePicture(null, null, jpeg); 255 | } 256 | 257 | private IAutoFocus mIAutoFocus; 258 | 259 | /** 聚焦的回调接口 */ 260 | public interface IAutoFocus{ 261 | void autoFocus(); 262 | } 263 | 264 | public void setIAutoFocus(IAutoFocus mIAutoFocus) { 265 | this.mIAutoFocus = mIAutoFocus; 266 | } 267 | 268 | public void setAutoFocus(){ 269 | mCamera.autoFocus(this); 270 | } 271 | 272 | @Override 273 | public boolean onTouchEvent(MotionEvent event) { 274 | if (mIAutoFocus != null){ 275 | mIAutoFocus.autoFocus(); 276 | } 277 | return super.onTouchEvent(event); 278 | } 279 | 280 | } 281 | -------------------------------------------------------------------------------- /app/src/main/java/business_android_client/croppercammer/MainActivity.java: -------------------------------------------------------------------------------- 1 | package business_android_client.croppercammer; 2 | 3 | import android.Manifest; 4 | import android.content.pm.PackageManager; 5 | import android.os.Build; 6 | import android.os.Bundle; 7 | import android.os.Environment; 8 | import android.support.annotation.NonNull; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.view.View; 11 | import android.view.WindowManager; 12 | import android.widget.Button; 13 | 14 | public class MainActivity extends AppCompatActivity implements View.OnClickListener, ClipCamera.IAutoFocus { 15 | 16 | private ClipCamera camera; 17 | private Button btn_shoot; 18 | private Button btn_cancle; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 24 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 25 | requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 22); 26 | } else { 27 | setContentView(R.layout.activity_main); 28 | initView(); 29 | } 30 | 31 | } 32 | 33 | private void initView() { 34 | camera = (ClipCamera) findViewById(R.id.surface_view); 35 | btn_shoot = (Button) findViewById(R.id.btn_shoot); 36 | btn_cancle = (Button) findViewById(R.id.btn_cancle); 37 | btn_shoot.setOnClickListener(this); 38 | btn_cancle.setOnClickListener(this); 39 | camera.setIAutoFocus(this); 40 | } 41 | 42 | @Override 43 | public void onClick(View v) { 44 | switch (v.getId()) { 45 | case R.id.btn_shoot: 46 | takePhoto(); 47 | break; 48 | case R.id.btn_cancle: 49 | finish(); 50 | break; 51 | } 52 | } 53 | 54 | public void takePhoto() { 55 | String path = Environment.getExternalStorageDirectory().getPath() + "/test.jpg"; 56 | camera.takePicture(path); 57 | } 58 | 59 | 60 | @Override 61 | public void autoFocus() { 62 | camera.setAutoFocus(); 63 | } 64 | 65 | @Override 66 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 67 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 68 | if (requestCode == 22) { 69 | for (int i=0;i< permissions.length;i++) { 70 | String s = permissions[i]; 71 | if (s.equals(Manifest.permission.CAMERA) && grantResults[i]== PackageManager.PERMISSION_GRANTED) { 72 | setContentView(R.layout.activity_main); 73 | initView(); 74 | } 75 | } 76 | // if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) { 77 | // setContentView(R.layout.activity_main); 78 | // initView(); 79 | // } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /app/src/main/java/business_android_client/croppercammer/PreView.java: -------------------------------------------------------------------------------- 1 | package business_android_client.croppercammer; 2 | 3 | import android.graphics.BitmapFactory; 4 | import android.os.Bundle; 5 | import android.os.Environment; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.widget.ImageView; 8 | 9 | public class PreView extends AppCompatActivity { 10 | 11 | private ImageView imageView; 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_pre_view); 17 | initView(); 18 | } 19 | 20 | private void initView() { 21 | String path = Environment.getExternalStorageDirectory().getPath() + "/test.jpg"; 22 | imageView = (ImageView) findViewById(R.id.imageView); 23 | imageView.setImageBitmap(BitmapFactory.decodeFile(path)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/business_android_client/croppercammer/view/shadow.java: -------------------------------------------------------------------------------- 1 | package business_android_client.croppercammer.view; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Rect; 6 | import android.graphics.Region; 7 | import android.support.annotation.Nullable; 8 | import android.util.AttributeSet; 9 | import android.view.View; 10 | 11 | /** 12 | * Created by seeker on 2017/5/23. 13 | */ 14 | 15 | public class shadow extends View { 16 | 17 | private int screenHeitht; 18 | private int screenWidth; 19 | 20 | public shadow(Context context) { 21 | this(context,null); 22 | } 23 | 24 | public shadow(Context context, @Nullable AttributeSet attrs) { 25 | this(context, attrs,0); 26 | } 27 | 28 | public shadow(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 29 | super(context, attrs, defStyleAttr); 30 | initView(); 31 | } 32 | 33 | private void initView() { 34 | 35 | } 36 | 37 | @Override 38 | protected void onDraw(Canvas canvas) { 39 | super.onDraw(canvas); 40 | 41 | canvas.clipRect(0, 0, screenWidth, screenHeitht); 42 | canvas.clipRect(getShadowRegionRect(), Region.Op.DIFFERENCE); 43 | canvas.drawColor(0x60000000); 44 | canvas.save(); 45 | canvas.restore(); 46 | } 47 | 48 | @Override 49 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 50 | super.onLayout(changed, left, top, right, bottom); 51 | } 52 | 53 | @Override 54 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 55 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 56 | screenHeitht = getMeasuredHeight(); 57 | screenWidth = getMeasuredWidth(); 58 | } 59 | 60 | /** 61 | * 获取身份证取景框的矩形 62 | * @return 63 | */ 64 | private Rect getShadowRegionRect(){ 65 | int height = (int) (screenWidth * 0.8);//拍照的阴影框的高度为屏幕宽度的80% 0.8 66 | int width = (int) (height * 1.6);//身份证宽高比例为1.6 67 | // int height= (int) (screenWidth/1.6); 68 | int x_center=screenWidth/2; 69 | int y_center=screenHeitht/2; 70 | // return new Rect(0, y_center - (height / 2), screenWidth, height/2 + y_center); 71 | return new Rect(x_center-(height/2), y_center - (width / 2), x_center+(height/2), (width/2)+ y_center); 72 | 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/1.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianyalian/CropperCammer/64fcab32403de546438225db98541903540dcbe9/app/src/main/res/drawable/1.webp -------------------------------------------------------------------------------- /app/src/main/res/drawable/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianyalian/CropperCammer/64fcab32403de546438225db98541903540dcbe9/app/src/main/res/drawable/2.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianyalian/CropperCammer/64fcab32403de546438225db98541903540dcbe9/app/src/main/res/drawable/3.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/cancle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianyalian/CropperCammer/64fcab32403de546438225db98541903540dcbe9/app/src/main/res/drawable/cancle.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/cancle_wite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianyalian/CropperCammer/64fcab32403de546438225db98541903540dcbe9/app/src/main/res/drawable/cancle_wite.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/sure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianyalian/CropperCammer/64fcab32403de546438225db98541903540dcbe9/app/src/main/res/drawable/sure.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/sure_wite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tianyalian/CropperCammer/64fcab32403de546438225db98541903540dcbe9/app/src/main/res/drawable/sure_wite.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 16 | 17 | 21 | 22 | 23 | 32 | 33 | 34 |