为什么保存位图需要这么长时间?

Mic*_*bie 6 android fileoutputstream google-glass google-gdk

所以我在Google Glass上有一个应用程序拍照,然后将其转换为灰度并覆盖内存中的原始图像:

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

在使用Windows照片查看器的内存中,灰色位图不完全可见,直到Google Glass从调试计算机上拔下,然后重新插入.有时可以查看一半的灰度图像,但就是这样.我更改了代码以显示图像而不是将其保存到内部存储器中,这很快且成功,这使我认为问题在于将灰度图像读入内部存储器:

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();
            }
        }
Run Code Online (Sandbox Code Playgroud)

欢迎任何解释或建议.谢谢.

Hen*_*nry 12

我没有使用谷歌眼镜,但遇到了同样的问题.所以我将在这里记录我的发现.

我的位图需要很长时间才能保存.我花了将近4秒来保存大小为1200x1200的位图,最终保存的文件大小为2MB.但我不需要那种清晰度和文件大小.当我的意思是"保存"时,它意味着单独实际保存到磁盘而不是Android操作系统将其作为媒体项检测所花费的时间.


实际保存: 如果您发现位图保存缓慢,请尝试以下操作:

  1. 尝试使用JPEG格式并仅在绝对必要时使用PNG.使用bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
  2. 如果您正在创建位图并且您不关心透明度,那么请使用Bitmap.Config.RGB_565而不是Bitmap.Config.ARGB_8888.

    使用 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);


文件管理器中的检测:

将位图保存为File存储后,当您将位图连接到PC时,Android OS不会立即在文件管理器或资源管理器中显示此文件.为了快速执行此检测,您需要使用MediaScannerConnection.

MediaScannerConnection.scanFile(getActivity(), new String[]{file.toString()}, null, null);
Run Code Online (Sandbox Code Playgroud)