dongshengyin0147 2019-03-30 10:25
浏览 166
已采纳

Unity将图像上载到服务器,但Codeigniter不起作用

I want to upload an image jpg to my server. I have try but it didn't work.

I don't what cause the upload failed.

Below the detail code.

Unity C#

public void TakePicture(int maxSize)
    {
        if (NativeCamera.IsCameraBusy())
        {
            Debug.Log("Camera Busy");
            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);
                    //snap.SetPixels(tex.GetPixels());                    

                    byte[] bytes = File.ReadAllBytes(path);
                    Debug.Log("Bytes:" + bytes);
                    Destroy(texture);

                    StartCoroutine(upload_ocr_image(bytes));

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

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

    }

    IEnumerator upload_ocr_image(byte[] bytes)
    {
        // UtilityScript.GetComponent<utility>().Sand_Clock_Loading(true);

        // yield return new WaitForSeconds(2.5f);

        WWWForm formDate = new WWWForm();

        formDate.AddField("frameCount", Time.frameCount.ToString());
        formDate.AddBinaryData("ocr_image", bytes, "ocr.jpg", "image/jpg");

        formDate.AddField("phone_code", "+61");
        formDate.AddField("phone_number", "434599859");                     

        using (UnityWebRequest www = UnityWebRequest.Post(web_url.url_upload_ocr_image, formDate))
        {
            yield return www.Send();

            //UtilityScript.GetComponent<utility>().Sand_Clock_Loading(false);

            if (www.isNetworkError)
            {
                Debug.Log(www.error);
               // UtilityScript.GetComponent<utility>().MessageBox_Check_Connection();
            }
            else
            {
                Debug.Log(www.downloadHandler.text);                
            }
        }
    }

And this the code in server codeigniter php.

function upload_ocr_image()
    {           
        $phone_code = $this->input->post('phone_code', true);
        $phone_number = $this->input->post('phone_number', true);

        $allowedType = array(IMAGETYPE_GIF,IMAGETYPE_JPEG,IMAGETYPE_PNG);
        $imgType = exif_imagetype($_FILES['ocr_image']['tmp_name']);
        if(!in_array($imgType,$allowedType))
        {
            echo "Images Type Error. Images Type Only : GIF , JPEG, PNG";
            exit;
        }
        else
        {
            //upload original size front end slider
            $config['upload_path'] = './assets/ocr_image/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['file_name'] = $phone_code.$phone_number;
            $config['overwrite'] = FALSE;
            $config['max_size'] = '8096';
            $config['max_width']  = '6000';
            $config['max_height']  = '6000';

            $this->load->library('upload', $config);

            if(!$this->upload->do_upload("ocr_image"))
            {
                echo "Maximum File Size Only 2 Mb Or Max Width = 2000 , Height = 2000";
                exit;
            }
            else
            {
                $img_data = $this->upload->data();

                // Create Thumbnail
                /*
                $magicianObj = new imageLib("assets/ocr_image/".$img_data["file_name"].$phone_code.$phone_number);
                $magicianObj -> resizeImage(80, 80, 0, true);
                $magicianObj -> saveImage("assets/admin/img/photos/".$img_data["raw_name"]."_thumb".$img_data["file_ext"], 100);

                $next_id = $next_id.$img_data["file_ext"];
                $thumb_name = $img_data["raw_name"]."_thumb".$img_data["file_ext"];             

                $data = array("photo_name"=>$next_id,
                              "thumb_photo_name"=>$thumb_name,
                              "create_date"=>date("Y-m-d H:i:s"));

                $this->db->insert("gallery_tbl",$data);
                */

            }
        }   
    }

No error found. But it always failed.

This line of code in codeigniter PHP :

if(!$this->upload->do_upload("ocr_image"))

Always return true.

How it work ?, How to upload the picture with proper way ?

Edited :

I use an asset native camera android and ios from unity to take a picture from camera.

Thank You

  • 写回答

2条回答 默认 最新

  • dongluxin6711 2019-04-01 00:54
    关注

    Finally i found the problem.

    Just set allowed type extension to except all extension in codeigniter upload file.

    change :

    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    

    to

    $config['allowed_types'] = '*';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置