繁体   English   中英

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

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

我尝试构建一个需要使用相机捕获图像(信用卡)并从图像中读取数字的应用程序。

我需要阅读的数字卡号、到期月份和年份。

我从这里使用免费资产:

适用于 Android IOS 的本机相机

捕捉图像。

另外我在这里使用谷歌tesseract表单designspark:

Tesseract OCR Unity

从图像中识别文本。

但是在尝试从图像中读取文本之前,我在读取纹理时遇到了问题。

有关详细信息,我的代码如下:

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

    }

我在线上出错:

字符串结果 = tesseract.RecognizeFromTexture(texture, false);

错误是:

AndroidPlayer(ADB@127.0.0.1:34999) UnityException: 纹理 '' 不可读,无法从脚本访问纹理内存。 您可以在纹理导入设置中使纹理可读。 在(包装管理到本机)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
在 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)

纹理不可读。

这是捕获图像并保存到临时磁盘的方式和位置。

Texture2D 纹理 = NativeCamera.LoadImageAtPath(path, maxSize);

该文件是在这里找到的:

图片路径:/data/user/0/com.Creativire.OCR/cache/IMG_camera.jpg

题 :

由于图像是直接从相机捕获的,因此我们无法从检查器中设置它,因此如何使纹理可读?

如何从designspark设置google tesseract只识别数字?

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

非常感谢您的任何解释。

谢谢你

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

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

暂无
暂无

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

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