赞
踩
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
[]));
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。