android - 将位图保存到 PNG 文件随机变黑
问题描述
我正在从相机预览中处理大约 4 张图像/秒,将其保存为 png 文件并将其发送到服务器。
我还无法确定原因,但当前会话中有 1 次会将 .png 存储为全黑图片。文件大小也非常小(3kb 与通常的 700kb)。
这是我将位图存储为 PNG 文件的方法:
public static File SaveImagePNG(Bitmap subimg, String path, String filename) {
FileOutputStream out = null;
File sd = new File(path);
boolean success = true;
if (!sd.exists()) {
success = sd.mkdir();
}
if (success) {
File dest = new File(sd, filename);
try {
out = new FileOutputStream(dest);
if (subimg != null) {
// PNG is a lossless format, the compression factor (100) is ignored
subimg.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
}
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, e.getMessage());
} finally {
try {
if (out != null) {
out.close();
Log.d(TAG, "OK!!");
}
} catch (IOException e) {
Log.d(TAG, e.getMessage() + "Error");
e.printStackTrace();
}
}
return dest;
}
return null;
}
这就是我所说的:
val pathFilePNG = Environment.getExternalStorageDirectory().toString() + "/images"
requestParams[0]?.bitmapRGB?.setHasAlpha(false)
val imageFaceRGBFile = BitmapUtils.SaveImagePNG(requestParams[0]?.bitmapRGB, pathFilePNG, "faceRGB.png")
解决方案
推荐阅读
- machine-learning - 使用 yolov3 进行自定义对象检测的预训练权重
- angular - 使用 NGRX 在 API 调用中更新后将项目添加到状态
- ios - 如何从目标 c 中的自定义按钮调用函数?[以编程方式]
- php - 用 JSON 中的 MYSQL 表示 PHP 中的数据
- javascript - Reactjs,源数组在组件重新加载时变为空白
- typescript - 打字稿对象中的等号后面是什么
- javascript - 为什么 Selenium 中的所有 getX() 调用都需要这么长时间?
- apache-kafka-connect - kafka-connector 启动期间的秘密管理
- reactjs - I18n:缺少翻译
- c++ - 在 glUseProgram 中设置 GL_TEXTURE_CUBE_MAP 会导致 GL_INVALID_OPERATION