SSSxCCC / IdCardRecognition

Android id card recognition based on OCR. 安卓基于OCR的身份证识别。
MIT License
53 stars 16 forks source link

White background #1

Closed Jasonczy closed 5 years ago

Jasonczy commented 5 years ago

hi, I just download your project and test run, it show the rectangle box with white background. How to fix it. Thank you.

SSSxCCC commented 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.

Jasonczy commented 5 years ago

I already try debug, it still show like this. screenshot_20190225-191000

SSSxCCC commented 5 years ago

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.

Jasonczy commented 5 years ago

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.

SSSxCCC commented 5 years ago

Try to add code to ask for permissions. See the android runtime permissions here: android runtime permissions

Jasonczy commented 5 years ago

Mind telling me where to add? Still a beginner, kind of no clue, sorry.

SSSxCCC commented 5 years ago

Well, that's OK. I'll add those code in the next several days. And I'll notify you when I'm done.

Jasonczy commented 5 years ago

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();
}

}

Jasonczy commented 5 years ago

I already add these, after it request camera, still show the same thing.

SSSxCCC commented 5 years ago

I just added the code as well, and it worked fine in my device. You could:

  1. change a device to run.
  2. find the error message.
  3. adjust the camera settings, current camera settings may not fit your camera.
Jasonczy commented 5 years ago

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.

SSSxCCC commented 5 years ago

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'.

Jasonczy commented 5 years ago

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.

SSSxCCC commented 5 years ago

You shouldn't create a new file, you should download the file here: trained data file, and put it in a directory named 'tessdata'

Jasonczy commented 5 years ago

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.

Jasonczy commented 5 years ago

Hi, just find out that I name the folder wrongly, and it work perfectly, thank you very much. You save my life.