简体   繁体   English

如何在 Android 或 IOS Unity 中设置从 Image Capture by Camera 读取的纹理?

[英]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 适用于 Android IOS 的本机相机

to capture an image.捕捉图像。

Also i am using a google tesseract form designspark here :另外我在这里使用谷歌tesseract表单designspark:

Tesseract OCR Unity 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);字符串结果 = 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. AndroidPlayer(ADB@127.0.0.1:34999) UnityException: 纹理 '' 不可读,无法从脚本访问纹理内存。 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在(包装管理到本机)UnityEngine.Texture2D.GetPixels(UnityEngine.Texture2D,int,int,int,int,int) 在 UnityEngine.Texture2D.GetPixels (System.Int32 miplevel) [0x0002b] 在 <004fc436a9154f7fab445967在 UnityEngine.Texture2D.GetPixels () [0x00001] 在 <004fc436a9154f7fab4df9679445af6c> 中:0 在 OCR_Test+<>c__DisplayClass6_0.b__0(System.String 路径)[0x00040] 在 F:\\CRASSERGIST0C\\TesserGithubs
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)在 NativeCameraNamespace.NCCameraCallbackAndroid.MediaReceiveCallback(System.String 路径)[0x0001d] 在 F:\\Github\\Tesseract_OCR\\Assets\\Plugins\\NativeCamera\\Android\\NCCameraCallbackAndroid.cs:30 在 NativeCameraNamespace.NCCameraCallbackAndroid+<>0.__0x00_0) [0x0001d]在 F:\\Github\\Tesseract_OCR\\Assets\\Plugins\\NativeCamera\\Android\\NCCameraCallbackAndroid.cs:19 at NativeCameraNamespace.NCCallbackHelper.Update () [0x0001d] 在 F:\\Github\\Tesseract_OCR\\Assets\\Plugins\\NativeNCCamera\\Android cs:21(文件名:<004fc436a9154f7fab4df9679445af6c> 行: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); Texture2D 纹理 = NativeCamera.LoadImageAtPath(path, maxSize);

and the file was found in here :该文件是在这里找到的:

Image path: /data/user/0/com.Creativire.OCR/cache/IMG_camera.jpg图片路径:/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 ?如何从designspark设置google tesseract只识别数字?

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.请注意:我已经尝试了designspark tesseract ocr,并将文件图像保存在统一中并且它有效,只是在直接从相机捕获时不起作用。

Any explanation from you is very appreciate.非常感谢您的任何解释。

Thank You谢谢你

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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