如何在Android或IOS Unity中将“相机捕获的图像”设置为可读的纹理?

时间:2019-03-22 00:42:01

标签: c# unity3d android-camera ocr tesseract

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

我需要读取的号码是卡号,到期的月份和年份。

我从这里使用免费的资产:

Native Camera For Android IOS

捕获图像。

我也在这里使用 google 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(纹理,假);

错误是:

  

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]   在<004fc436a9154f7fab4df9679445af6c>:0中   中的UnityEngine.Texture2D.GetPixels()[0x00001]   <004fc436a9154f7fab4df9679445af6c>:0在   OCR_Test + <> c__DisplayClass6_0.b__0(System.String路径)   F:\ Github \ Tesseract_OCR \ Assets \ script \ OCR_Test.cs:80中的[0x00040]
  在NativeCameraNamespace.NCCameraCallbackAndroid.MediaReceiveCallback   (系统字符串路径)[0x0001d]在   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在NativeCameraNamespace.NCCallbackHelper.Update()[0x0001d]中   F:\ Github \ Tesseract_OCR \ Assets \ Plugins \ NativeCamera \ Android \ NCCallbackHelper.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与一个文件图像保存为一体,并且可以正常工作,只是直接从相机捕获时不起作用。

非常感谢您的解释。

谢谢

1 个答案:

答案 0 :(得分:0)

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

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