Android 为什么保存位图需要这么长时间?
因此,我在谷歌眼镜上有一个应用程序,它可以拍摄一张照片,然后将其转换为灰度并覆盖内存中的原始图像:Android 为什么保存位图需要这么长时间?,android,google-glass,google-gdk,fileoutputstream,Android,Google Glass,Google Gdk,Fileoutputstream,因此,我在谷歌眼镜上有一个应用程序,它可以拍摄一张照片,然后将其转换为灰度并覆盖内存中的原始图像: private void rGBProcessing (final String picturePath, Mat image) { //BitmapFactory Creates Bitmap objects from various sources, //including files, streams, and byte-arrays Bitmap myBitmapPic = Bit
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();
}
}
在从调试计算机上拔下Google Glass并重新插入之前,使用Windows Photo Viewer无法在内存中完全看到灰色位图。有时可以看到一半的灰度图像,但仅此而已。我修改了代码以显示图像,而不是将其保存到内存中,这是快速而成功的,这使我认为问题在于将灰度图像读取到内存中:
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();
}
}
欢迎任何解释或建议。谢谢。我没有使用谷歌眼镜,但面临着同样的问题。所以我将在这里记录我的发现 我的位图需要很长时间才能保存。我花了将近4秒的时间来保存大小为1200x1200的位图,最终保存的文件大小为2MB。但我不需要那种清晰度和文件大小。当我指的是“保存”时,它指的是单独实际保存到磁盘,而不是Android操作系统将其检测为媒体项所花费的时间
实际保存: 如果您发现位图保存缓慢,请尝试以下操作:
bitmap.compress(bitmap.CompressFormat.JPEG,100,out)代码>
bitmap.Config.RGB_565
而不是bitmap.Config.ARGB_8888
使用Bitmap Bitmap=Bitmap.createBitmap(宽度、高度、Bitmap.Config.RGB_565)代码>
在文件管理器中检测: 一旦将位图保存为存储中的
文件
,当您将其连接到PC时,Android操作系统将不会立即在文件管理器或浏览器中显示该文件。为了快速执行此检测,您需要使用MediaScannerConnection
MediaScannerConnection.scanFile(getActivity(), new String[]{file.toString()}, null, null);
“为什么保存位图需要这么长时间?”--使用Traceview并找出原因。“在从调试计算机上拔下Google Glass并重新插入之前,使用Windows Photo Viewer无法在内存中完全看到灰色位图”--使用
MediaScannerConnection
和scanFile()
获取由MediaStore
索引的文件。“有时可以查看灰度图像的一半,但仅此而已”--在调用flush()
后调用close()
之前,在FileOutputStream
上调用getFD().sync()
。