简体   繁体   中英

How to set Texture readable from Image Capture by Camera in Android or IOS Unity?

I have try to build an app which need to use a camera to capture an image (Credit Card) and read the number from the image.

The number i need to read is card number, expire month, and year.

I use a free asset from here :

Native Camera For Android IOS

to capture an image.

Also i am using a google tesseract form designspark here :

Tesseract OCR Unity

to recognize a text from image.

But before trying to read a text from image i got a problem on reading a texture.

For detail my code below :

public void TakePicture(int maxSize)
    {
        if (NativeCamera.IsCameraBusy())
        {
            return;
        }
        else
        {
            NativeCamera.Permission permission = NativeCamera.TakePicture((path) =>
            {
                Debug.Log("Image path: " + path);
                if (path != null)
                {
                    // Create a Texture2D from the captured image                    
                    Texture2D texture = NativeCamera.LoadImageAtPath(path, maxSize);                          

                    if (texture == null)
                    {
                        Debug.Log("Couldn't load texture from " + path);
                        return;
                    }

                    TesseractWrapper_And tesseract = new TesseractWrapper_And();
                    string datapath = System.IO.Path.Combine(Application.persistentDataPath, "tessdata");
                    tesseract.Init("eng", datapath);

                    //Color32[] imageColors = texture.GetPixels32();
                    string result = tesseract.RecognizeFromTexture(texture, false);

                    //copy bufferColors to imageColors one by one with necessary logic.

                    Card_Number.text = result ?? "Error: " + tesseract.errorMsg;

                    // Assign texture to a temporary quad and destroy it after 5 seconds
                    GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
                    quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
                    quad.transform.forward = Camera.main.transform.forward;
                    quad.transform.localScale = new Vector3(1f, texture.height / (float)texture.width, 1f);

                    Material material = quad.GetComponent<Renderer>().material;
                    if (!material.shader.isSupported) // happens when Standard shader is not included in the build
                        material.shader = Shader.Find("Legacy Shaders/Diffuse");

                    material.mainTexture = texture;

                    Destroy(quad, 5f);

                    // If a procedural texture is not destroyed manually, 
                    // it will only be freed after a scene change
                    Destroy(texture, 5f);                   

                }
            }, maxSize);

            Debug.Log("Permission result: " + permission);
        }

    }

I got an error on the line :

string result = tesseract.RecognizeFromTexture(texture, false);

The error is :

AndroidPlayer(ADB@127.0.0.1:34999) UnityException: Texture '' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings. at (wrapper managed-to-native) UnityEngine.Texture2D.GetPixels(UnityEngine.Texture2D,int,int,int,int,int) at UnityEngine.Texture2D.GetPixels (System.Int32 miplevel) [0x0002b] in <004fc436a9154f7fab4df9679445af6c>:0 at UnityEngine.Texture2D.GetPixels () [0x00001] in <004fc436a9154f7fab4df9679445af6c>:0 at OCR_Test+<>c__DisplayClass6_0.b__0 (System.String path) [0x00040] in F:\\Github\\Tesseract_OCR\\Assets\\script\\OCR_Test.cs:80
at NativeCameraNamespace.NCCameraCallbackAndroid.MediaReceiveCallback (System.String path) [0x0001d] in F:\\Github\\Tesseract_OCR\\Assets\\Plugins\\NativeCamera\\Android\\NCCameraCallbackAndroid.cs:30 at NativeCameraNamespace.NCCameraCallbackAndroid+<>c__DisplayClass3_0.b__0 () [0x00000] in F:\\Github\\Tesseract_OCR\\Assets\\Plugins\\NativeCamera\\Android\\NCCameraCallbackAndroid.cs:19 at NativeCameraNamespace.NCCallbackHelper.Update () [0x0001d] in F:\\Github\\Tesseract_OCR\\Assets\\Plugins\\NativeCamera\\Android\\NCCallbackHelper.cs:21 (Filename: <004fc436a9154f7fab4df9679445af6c> Line: 0)

The texture is not readable.

This is how and where the image captured and save to temporary disk.

Texture2D texture = NativeCamera.LoadImageAtPath(path, maxSize);

and the file was found in here :

Image path: /data/user/0/com.Creativire.OCR/cache/IMG_camera.jpg

Question :

How to make the texture readable since the image is capture directly from the camera so we cannot set it from inspector ?

How to set the google tesseract from designspark just recognize the number ?

For note : i have already try the designspark tesseract ocr with a file image save in the unity and it worked, just do not work when capture from camera directly.

Any explanation from you is very appreciate.

Thank You

好吧终于想通了,你只需要将不可读的属性更改为 false

Texture2D texture = NativeCamera.LoadImageAtPath(path, maxSize,false,true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM