Closed Jasonczy closed 5 years ago
The code which controls rectangle box painting is in the source file IdCardRecognition/app/src/main/java/com/sc/idcardrecognition/MainActivity.java:
// 顶层的SurfaceView,根据idCard对象里面的信息显示矩形区域
topSurfaceView = findViewById(R.id.topSurfaceView);
topSurfaceView.setZOrderOnTop(true); // 在顶层
holder = topSurfaceView.getHolder();
holder.setFixedSize(Utility.WidthPixel, Utility.HeightPixel);
holder.setFormat(PixelFormat.TRANSPARENT); // 透明的
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
for (IdCard.Rect rect : Utility.idCard.getRects()) {
canvas.drawRect(rect.toRect(), paint);
}
holder.unlockCanvasAndPost(canvas);
}
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
@Override public void surfaceDestroyed(SurfaceHolder holder) { }
});
It works fine in my device. You could try to debug these code.
I already try debug, it still show like this.
It seems that your camera is not opened, what is your Android version? I forgot to add android runtime permissions to the code, this may cause the problem like unable to open the camera.
My android version is 26, but already change the permission in the setting. Actually, It does not ask permission for camera when the app opened.
Try to add code to ask for permissions. See the android runtime permissions here: android runtime permissions
Mind telling me where to add? Still a beginner, kind of no clue, sorry.
Well, that's OK. I'll add those code in the next several days. And I'll notify you when I'm done.
public class MainActivity extends AppCompatActivity { private static final int PERMISSION_REQUEST_CODE = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkPermission()) {
//main logic or main code
// . write your main code to execute, It will execute if the permission is already given.
} else {
requestPermission();
}
}
private boolean checkPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
return false;
}
return true;
}
private void requestPermission() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
PERMISSION_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
// main logic
} else {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
showMessageOKCancel("You need to allow access permissions",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermission();
}
}
});
}
}
}
break;
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
}
I already add these, after it request camera, still show the same thing.
I just added the code as well, and it worked fine in my device. You could:
Thank you very much, I remove params.setPreviewFrameRate(10); // 预览帧率 the camera worked. One more question, it snap the identity card by touching the screen, right? Because everytime I touch it, the app exit itself, is that how it works? Sorry if I frustrated you.
When you touch the screen, the camera takes the photo. If the app exit, you should make sure that you download trained data file and put it in a directory named 'tessdata'.
I create a new file called tesstwo and put it in SD card, but it still failed to work. Am I doing in wrong way? Thank you.
You shouldn't create a new file, you should download the file here: trained data file, and put it in a directory named 'tessdata'
Yes, I had download it. But I had no idea where is the tesstwo folder, so after I do some research in google and found suggested answer. So I give it a try.
Hi, just find out that I name the folder wrongly, and it work perfectly, thank you very much. You save my life.
hi, I just download your project and test run, it show the rectangle box with white background. How to fix it. Thank you.