Android开发拍身份证带人像框和国徽框效果
Android开发拍身份证带人像框和国徽框效果
拍身份证时往往要带上外框辅助用户拍照,这也是很常见的需求。
一、思路
自定义Camera和自定义拍照的界面,把外框画上去,做个遮罩
二、效果图:
Android开发教程实战案例源码分享-拍身份证带人像框和国徽框效果
三、关键代码:
// 联系:893151960
public class SecondActivity extends AppCompatActivity {
private ImageView iv_result;
private Context mContext;
private ActivityResultLauncher<Intent> launcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
String photoPath = result.getData().getStringExtra(CameraActivity.KEY_CONTENT_TYPE);
Log.i("photoPath",photoPath);
if (!TextUtils.isEmpty(photoPath)) {
Glide.with(mContext)
.load(photoPath)
.into(iv_result);
}
}
});
iv_result = findViewById(R.id.iv_result);
findViewById(R.id.tv_photo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermission(mContext, new View.OnClickListener() {
@Override
public void onClick(View view) {
startCameraActivity(mContext,CameraActivity.CONTENT_TYPE_ID_CARD_FRONT,launcher);
}
}, checkSDK33(mContext) ? new String[]{
Manifest.permission.CAMERA,
Manifest.permission.READ_MEDIA_IMAGES
} : new String[]{
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
});
}
});
}
/**
* 身份证取景拍照
*/
public static void startCameraActivity(Context mContext,String type, ActivityResultLauncher<Intent> launcher) {
Intent intent = new Intent(mContext, CameraActivity.class);
//intent.putExtra(CameraActivity.KEY_CONTENT_TYPE,CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
intent.putExtra(CameraActivity.KEY_CONTENT_TYPE,type);
launcher.launch(intent);
}
public static boolean checkSDK33(Context context) {
ApplicationInfo applicationInfo = context.getApplicationInfo();
return Build.VERSION.SDK_INT >= 33
&& applicationInfo != null
&& applicationInfo.targetSdkVersion >= 33;
}
public static void requestPermission(final Context context, final View.OnClickListener successListener, String... permissions) {
if (PermissionCheckUtil.checkPermissions(context,permissions)){
if (null != successListener) {
successListener.onClick(null);
}
} else {
PermissionAppUtils.INSTANCE.requestPermission(context, permissions, new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Throwable {
if (aBoolean){
if (null != successListener) {
successListener.onClick(null);
}
}
}
});
}
}
}
四、项目demo源码结构图:
有问题或者需要源码的私信我