关于unity中的截图快慢问题

1.  unity自带API;完成时间

1秒不到

Debug.Log("开始截图,时间是" + System.DateTime.Now);
            ScreenCapture.CaptureScreenshot(Application.persistentDataPath + "/onMobileSavedScreen.png");
            Debug.Log("截图完成,时间是:" + System.DateTime.Now);

2.  使用Texture2D截图; 完成用时

1秒

 Debug.Log("开始截图,时间是" + System.DateTime.Now);
            //ScreenCapture.CaptureScreenshot(Application.persistentDataPath + "/onMobileSavedScreen.png");
            Rect rect = new Rect(0,0, Screen.width, Screen.height);
            // 创建一个屏幕大小的纹理,RGB24 位格(24位格没有透明通道,32位的有)
            Texture2D tex = new Texture2D ((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
            // 读取屏幕内容到我们自定义的纹理图片中
            tex.ReadPixels (rect, 0, 0);
            // 保存前面对纹理的修改
            tex.Apply ();
            // 编码纹理为PNG格式
            byte[] bytes = tex.EncodeToPNG();
            // 销毁吴永的图片纹理
            //Destroy (tex);
                
            if (Application.isMobilePlatform == true) {
                
                // 这个路径会将图片保存到手机的沙盒中,这样就可以在手机上对其进行读写操作了
                File.WriteAllBytes(Application.persistentDataPath + "/onMobileSavedScreen.png", bytes);
                Debug.Log("截图完成,时间是:" + System.DateTime.Now);

3.   截完图微信分享

压缩图片用时开始2018-07-27 21:45:13
07-27 21:46:14.943 20368-20389/com.shunyuan3d.hz I/System.out: 截完2018-07-27 21:46:14

日志显示一分钟

String path = filepath + "/" + "onMobileSavedScreen.png";
Bitmap bmp = BitmapFactory.decodeFile(path);
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
int options = 100;
while (output.toByteArray().length > IMAGE_SIZE && options != 0) {
    output.reset(); //清空baos
    bmp.compress(Bitmap.CompressFormat.PNG, options, output);//这里压缩options%,把压缩后的数据存放到baos中
    options -= 5;
}
ByteArrayInputStream isBm = new ByteArrayInputStream(output.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = false;
newOpts.inSampleSize = bmp.getWidth() / 120;
Bitmap thumbBitMap = BitmapFactory.decodeStream(isBm, null, newOpts);

不压缩图片耗时3秒

WXImageObject imgObj = new WXImageObject();
imgObj.setImagePath(filepath + "/onMobileSavedScreen.png");
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
Bitmap bmp = BitmapFactory.decodeFile(filepath + "/onMobileSavedScreen.png");
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
msg.setThumbImage(thumbBmp);
bmp.recycle();

猜你喜欢

转载自blog.csdn.net/Happy_zailing/article/details/81254725