[英]How to set pixels to a Bitmap in android properly?
我想將pixelarray保存到位圖。 除了我的應用程序保存了位圖之外,我一切正常,但顏色與數組中的顏色略有不同。
這是我保存顏色數組的方式:
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
bitmap.setPixels(colors, 0, width, 0, 0, width, height);
OutputStream out = null;
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getParent(), "new.png");
try {
file.createNewFile();
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (Exception e) {
e.printStackTrace();
}
這是我為數組中的像素設置顏色的方式:
colors[0] = 0x287026;
怎么了? 壓縮為PNG並將其存儲到我的文檔中? 因為當我在手機上查看文檔並檢查創建的新PNG時,它有點模糊。 還是這段代碼有誤:Bitmap.Config.RGB_565? 我需要使用ARGB而不是RGB嗎?
希望有人能夠幫助我! :)
任何幫助表示贊賞!
喬里
顏色以int編碼,並且可以包含alpha通道。 我懷疑您的顏色確實包含alpha通道,因此當您的位圖格式要求使用RGB時,您正在使用ARGB顏色。 將圖像格式更改為ARGB_8888,這是默認和推薦的格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.