在 Android 中保存位图

发布于 2024-12-18 05:48:38 字数 320 浏览 6 评论 0原文

我正在尝试为 Android 实现一个隐写术项目。我已经操纵了像素值并创建了一个新的位图。现在,当我使用 getPixels() 将位图存储到手机内存或存储卡中

//fo denotes File output Stream
Bitmap.compress(Bitmap.CompressFormat.JPEG,100,fo);
//OR
Bitmap.compress(Bitmap.CompressFormat.PNG,100,fo);   

并尝试访问像素时;

这些值将恢复为原始位图,即而不是操作后的位图。 有人能弄清楚这是为什么吗?

I am trying to implement a Steganography project for Android. I have manipulated the pixel values and created a new bitmap. Now when i store the bitmap into the phone memory or memory card using

//fo denotes File output Stream
Bitmap.compress(Bitmap.CompressFormat.JPEG,100,fo);
//OR
Bitmap.compress(Bitmap.CompressFormat.PNG,100,fo);   

and try accessing the pixels back using getPixels();

the values are reverted back to the original bitmap i.e. rather than the manipulated bitmap.
Can anybody figure why this is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗫嚅 2024-12-25 05:48:38
File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

看看这个答案 https://stackoverflow.com/a/7887114/964741

File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

look at this answer https://stackoverflow.com/a/7887114/964741

愚人国度 2024-12-25 05:48:38

JPEG 是有损的,压缩时会改变像素值。如果您想保留颜色,请使用 PNG。

JPEG is lossy, it can change pixel values when compressing. Use PNG if you want to preserve colors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文