alimertozdemir / EPassportNFCReader

ID Card and E-Passport Reader NFC Android Application - Sample Project with MLKit
127 stars 48 forks source link

Fail to connect to camera service #2

Closed muklah closed 4 years ago

muklah commented 4 years ago

Hi there! There is a problem with the project while I click on any bottom to start scan I just get this error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.alimert.passportreader, PID: 4257 java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.(Camera.java:519) at android.hardware.Camera.open(Camera.java:364) at com.alimert.passportreader.mlkit.camera.CameraSource.createCamera(CameraSource.java:259) at com.alimert.passportreader.mlkit.camera.CameraSource.start(CameraSource.java:172) at com.alimert.passportreader.mlkit.camera.CameraSourcePreview.startIfReady(CameraSourcePreview.java:74) at com.alimert.passportreader.mlkit.camera.CameraSourcePreview.access$200(CameraSourcePreview.java:18) at com.alimert.passportreader.mlkit.camera.CameraSourcePreview$SurfaceCallback.surfaceCreated(CameraSourcePreview.java:97) at android.view.SurfaceView.updateWindow(SurfaceView.java:656) at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:172) at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1013) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2506) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1515) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7091) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927) at android.view.Choreographer.doCallbacks(Choreographer.java:702) at android.view.Choreographer.doFrame(Choreographer.java:638) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

Can you tell me how to fix this please?

alimertozdemir commented 4 years ago

Hi @muklah I forgot to add Android Runtime Permission for camera usage on API23 and above. I have push permission fix to master branch. Please pull the latest version of master branch. Could you please confirm it is working fine after getting latest code? Thank you.

muklah commented 4 years ago

Hi @alimertozdemir, well the error of the camera is gone now but there is another problem, when I scan passport it always says invalid mrz:

D/MIDemoApp:CameraSource: Process an image D/MIDemoApp:CameraSource: Process an image D/Camera: app passed NULL surface D/com.alimert.passportreader.mlkit.text.TextRecognitionProcessor: MRZ DATA is not valid D/com.alimert.passportreader.mlkit.text.TextRecognitionProcessor: MRZ DATA is not valid D/com.alimert.passportreader.mlkit.text.TextRecognitionProcessor: MRZ DATA is not valid D/com.alimert.passportreader.mlkit.text.TextRecognitionProcessor: MRZ DATA is not valid D/SemPersonaManager: isNFCAllowed D/ViewRootImpl@c6893e9[MainActivity]: Relayout returned: oldFrame=[0,0][1440,2560] newFrame=[0,0][1440,2560] result=0x7 surface={isValid=true 488536130048} surfaceGenerationChanged=true D/ViewRootImpl@c6893e9[MainActivity]: mHardwareRenderer.initialize() mSurface={isValid=true 488536130048} hwInitialized=true D/mali_winsys: EGLint new_window_surface(egl_winsys_display, void, EGLSurface, EGLConfig, egl_winsys_surface*, egl_color_buffer_format, EGLBoolean) returns 0x3000, [1440x2560]-format:1 D/ViewRootImpl@c6893e9[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 488536130048} V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@e9e084d nm : com.alimert.passportreader ic=null I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus D/InputTransport: Input channel constructed: fd=79 Input channel destroyed: fd=86 D/SurfaceView: Relayout returned: oldFrame=[0,-192][2560,1728] newFrame=[0,-192][2560,1728] result=0x5 surface={Surface(name=null)/@0x568ff30 isValid=false 0} D/ViewRootImpl@e118e39[CaptureActivity]: mHardwareRenderer.destroy()#1 D/ViewRootImpl@e118e39[CaptureActivity]: Relayout returned: oldFrame=[0,0][2560,1440] newFrame=[0,0][2560,1440] result=0x5 surface={isValid=false 0} surfaceGenerationChanged=true MSG_WINDOW_FOCUS_CHANGED 0 D/Graph: removeVertex() : insertDummyVertex, because there is no ancestor. D/ViewRootImpl@e118e39[CaptureActivity]: mHardwareRenderer.destroy()#4 dispatchDetachedFromWindow D/InputTransport: Input channel destroyed: fd=72

did you check it recently if it works fine while scanning? help me with this please And sure I will confirm it's working!

alimertozdemir commented 4 years ago

Are you sure that scanned passport has one the ICAO's MRZ format? https://en.wikipedia.org/wiki/Machine-readable_passport If so, could you provide your passport MRZ Data with changing personal data (like name, surname, birthdate, expireDate to i.e. John Doe, 01.01.1980, etc..)? It is important, please do not send your/someone's any personal info.

muklah commented 4 years ago

Yeah I'm sure, here is the MRZ:

P<IRLOSULLIVAN<<LAURENA<<<<<<<<<<<<<<<<<<<<<< NX50042152IRL9105049F2409154<<<<<<<<<<<<<<<6

I got a passport image from a tutorial and it works with other apps so if you would like me to send this image then I will to try it by yourself

alimertozdemir commented 4 years ago

P<IRLOSULLIVAN<<LAURENA<<<<<<<<<<<<<<<<<<<<<< NX50042152IRL9105049F2409154<<<<<<<<<<<<<<<6 https://www.consilium.europa.eu/prado/en/IRL-AD-05001/image-264971.html With above MRZ data and image from url, it works fine. I think you have to wait moderate time to complete scan. MLKit scans all text without knowing text format. While text detection has finished, it checks it is correct MRZData with jmrtd library. Could you please add log to TextRecognitionProcessor.java line 152 -> isMrzValid method in order to notify read MRZ text. Or you can send sample image of passport (with sample data), then I can check on my own.

muklah commented 4 years ago

This is exactly the same image that I use to check the app and it's not working with me I add log on method and I get this:

D/com.alimert.passportreader.mlkit.text.TextRecognitionProcessor: isMrzValid: P<IRLOSULLIVAN<<LAUREN<<<<<<<<<<<<<<<<<<<<<K XN500421<62IRL880504<F2309154<<<<<<<<<<<<<<<

Sorry I know that I get a lot of your time

alimertozdemir commented 4 years ago

Nop. I think the problem is MLKit text recognition API. Image processing depends on your device camera quality, camera and object distance and sunlight. Could you please follow these steps below, I hope it helps.

  1. Press SCAN PASSPORT (opens camera horizontally)
  2. Hold viewfinder (white rectangle) over MRZ field on passport.
  3. Wait until MRZData validation has been succeed and camera close.
  4. You have to see Contactless symbol and description (Tap ID Card / Passport via NFC...)
  5. If you will continue to tap passport or id card in order to get more personal info from chip, make sure that MRZ scanned passport/id card must be same with read card by NFC.

I am sorry, i don't know your device specific info (like camera type, processors, rams etc.), so I couldn't help much more. But the problem is text detection on MLKit. I have no solution about Google MLKit text recognition, but please wait for scanning text until validation succeed.

muklah commented 4 years ago

Thanks man I will try these steps and I hope the problem will be fixed

akeilox commented 4 years ago

In regards to the premature timeout issue discussed in this thread perhaps increasing timeout will help, some documents appear to take longer with some phones NFC. It was also discussed in similar projects (tananev etc.) For instance, set timeout of 5000 in ReadTask;


        private ReadTask(IsoDep isoDep, BACKeySpec bacKey) {
            //Increase timeout 
            isoDep.setTimeout(5000);

            this.isoDep = isoDep;
            this.bacKey = bacKey;
        }
alimertozdemir commented 4 years ago

Hi @akeilox, Thank you very much for your contribution. However the problem above was related for text recognition on camera for getting MRZ data. Your solution is fine about reading too much data (takes some time) from passport on nfc.