unity 截图上传生成二维码

该代码段展示了如何在Unity3D中捕获摄像头图像,并将其转换为字节数组,然后上传到指定URL。之后,从服务器返回的数据用于创建二维码。如果图片文件超过PHP限制,则会显示错误信息。最后,生成的二维码会被显示在游戏对象上。
摘要由CSDN通过智能技术生成

public void CaptureScreen(Camera c, Rect r)

    {

        //捕抓摄像机图像并转换成字符数组

        RenderTexture rt = new RenderTexture((int)r.width, (int)r.height, 0);

        c.targetTexture = rt;

        c.Render();

 

        RenderTexture.active = rt;

        Texture2D screenShot = new Texture2D((int)r.width, (int)r.height, TextureFormat.RGB24, false);

        screenShot.ReadPixels(r, 0, 0);

        screenShot.Apply();

 

        c.targetTexture = null;

        RenderTexture.active = null;

        GameObject.Destroy(rt);

 

        byte[] bytes = screenShot.EncodeToPNG();

        string url = "http://cus.szqicnt.com/whzq/put.php";//要上传到的地址

 

        WWWForm form = new WWWForm();

        form.AddField("name""test.png");

        form.AddBinaryData("file", bytes);//把图片流上传

 

        WWW www = new WWW(url, form);

        StartCoroutine(PostData(www));//启动子线程

        Destroy(screenShot);//销毁

    }

 

IEnumerator PostData(WWW www)

    {

        yield return www;

        string returnStr = www.text.Trim();

        Debug.Log(returnStr);

        if(returnStr == "1")

        {

            Debug.Log("图片文件容量超过php.ini限制");

        }

 

 

        Texture2D qr = ZXingUtil.instance.QrCreat(returnStr);

        if(qr!=null && this.QRCode!=null)

        {

            this.QRCode.texture = qr;

        }

    }

 

 

using System;

using System.Collections;

using System.Collections.Generic;

using System.Drawing;

using System.IO;

using UnityEngine;

using ZXing;

using ZXing.Common;

 

public class ZXingUtil : MonoBehaviour {

 

    private int _width = 256;

    private int _height = 256;

    private Texture2D _texure2d;

    private bool _success;

    public static ZXingUtil instance;

    EncodingOptions options = null;

    BarcodeWriter writer = null;

 

    void Awake()

    {

        instance = this;

    }

 

    void Start()

    {

        _texure2d = new Texture2D(_width, _height);

 

        options = new EncodingOptions

        {

            Width = _width,

            Height = _height

        };

        writer = new BarcodeWriter();

        writer.Format = BarcodeFormat.QR_CODE;

        writer.Options = options;

    }

    /// <summary>

    /// 传入条形码内容 返回条码图,条形码须为偶数位

    /// </summary>

    /// <param name="barcode"></param>

    /// <returns></returns>

    public Texture2D QrCreat(string barcode)

    {

        try

        {

            Bitmap bitmap = writer.Write(barcode);

 

            Image image = writer.Write(barcode);

            //将PNG / JPG图像字节数组加载到纹理中

            _texure2d.LoadImage(ImageToByte(image));

            return _texure2d;

        }

        catch (Exception e)

        {

            Debug.LogError("z xing error:"+e);

            return null;

        }

    }

 

    public Texture2D QrCreat(string barcode, Vector2 imageVec)

    {

        _texure2d = new Texture2D((int)imageVec.x, (int)imageVec.y);

        Bitmap bitmap = writer.Write(barcode);

        _texure2d.LoadImage(ImageToByte(bitmap));

        return _texure2d;

    }

 

    //将image类型转化为byte[]类型

    private byte[] ImageToByte(Image img)

    {

        ImageConverter converter = new ImageConverter();

        return (byte[])converter.ConvertTo(img, typeof(byte[]));

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值