为什么保存位图花了这么长时间? [英] Why does saving a bitmap take so long?

查看:172
本文介绍了为什么保存位图花了这么长时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我对谷歌玻璃的应用程序,拍照,然后将其转换为灰度并覆盖原始图像存储:

So I have an app on Google Glass that takes a picture, then converts it to grayscale and overwrites the original image in memory:

private void rGBProcessing (final String picturePath, Mat image) {
//BitmapFactory Creates Bitmap objects from various sources,
//including files, streams, and byte-arrays
    Bitmap myBitmapPic = BitmapFactory.decodeFile(picturePath);
    image = new Mat(myBitmapPic.getWidth(), myBitmapPic.getHeight(), CvType.CV_8UC4);
    Mat imageTwo = new Mat(myBitmapPic.getWidth(), myBitmapPic.getHeight(), CvType.CV_8UC1);
    Utils.bitmapToMat(myBitmapPic, image);
    Imgproc.cvtColor(image, imageTwo, Imgproc.COLOR_RGBA2GRAY);
    Utils.matToBitmap(imageTwo, myBitmapPic);

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(picturePath);
        myBitmapPic.compress(Bitmap.CompressFormat.PNG, 100, out); 
    // PNG is a lossless format, the compression factor (100) is ignored
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
         } catch (IOException e) {
            e.printStackTrace();
        }
    }

使用Windows照片查看器,直到谷歌玻璃是从调试计算机拔下,然后插回去

灰色的位图不是在内存中完全可见,有时一半可以被看作灰度图像,但仅此而已。我改变了code来显示图像,而不是将其保存到内部存储器,这是快速和成功这让我觉得这个问题是与读取灰度图像到内部存储器:

The grey bitmap is not fully visible in memory using Windows Photo Viewer until the Google Glass is unplugged from the debugging computer and then plugged back in. Sometimes half of the grayscale image can be viewed but that's it. I altered the code to show the image rather than save it into internal memory and this was quick and successful which makes me think that the problem is with reading the grayscale image into internal memory:

FileOutputStream out = null;
        try {
            out = new FileOutputStream(picturePath);
            myBitmapPic.compress(Bitmap.CompressFormat.PNG, 100, out); 
        // PNG is a lossless format, the compression factor (100) is ignored
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
             } catch (IOException e) {
                e.printStackTrace();
            }
        }

任何解释或建议表示欢迎。谢谢你。

Any explanations or suggestions welcome. Thanks.

推荐答案

我没有使用谷歌的玻璃,但面临着同样的问题。所以,我要在这里记录我的发现。

I am not using the Google Glass, but faced this same problem. So I am going to document my findings here.

我的位图是花费很长的时间来保存。我花了将近4秒,以节省尺寸1200x1200的位图和最后保存的文件大小为2MB。但我并不需要的清晰度和文件大小级别。当我的意思是拯救,这意味着实际保存到磁盘单独,而不是采取Android操作系统的时间来检测它作为一个媒体项目。

My bitmap was taking a long time to save. It took me nearly 4 sec to save a bitmap of size 1200x1200 and the final saved file size was 2MB. But I didn't need that level of clarity and file size. When I mean "save", it means the actual saving to the disk alone and not the time taken by the Android OS to detect it as a media item.


实际保存: 请尝试以下,如果你发现保存位图是缓慢的:


ACTUAL SAVE: Try out the following if you find the saving of bitmap to be slow:

  1. 尝试使用 JPEG 格式,并使用 PNG 只有当它是绝对必要的。 使用 bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,出);
  2. 如果您正在创建位图,你不关心透明度,然后使用 Bitmap.Config.RGB_565 而不是 Bitmap.Config.ARGB_8888

  1. Try to use JPEG format and use PNG only when it is absolutely necessary. Use bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
  2. If you are creating the bitmap and you don't care about the transparency, then use Bitmap.Config.RGB_565 instead of Bitmap.Config.ARGB_8888.

使用位图位= Bitmap.createBitmap(宽度,高度,Bitmap.Config.RGB_565);


检测在文件管理器:

一旦保存了位图作为文件在存储方面,Android操作系统不会立即显示在文件管理器或当您连接浏览器这个文件到您的计算机。为了做到这一点检测速度快,你需要使用 MediaScannerConnection

Once you have saved the bitmap as a File in the Storage, the Android OS won't immediately show this file in the File Manager or in the explorer when you connect it to your PC. In order to do this detection fast, you need to use MediaScannerConnection.

MediaScannerConnection.scanFile(getActivity(), new String[]{file.toString()}, null, null);

这篇关于为什么保存位图花了这么长时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆