简体   繁体   English

如何正确设置像素到Android中的位图?

[英]How to set pixels to a Bitmap in android properly?

I want to save a pixelarray to a Bitmap. 我想将pixelarray保存到位图。 I got everything working except: my app saves the bitmap, but the colors are a little bit different as they are in my array. 除了我的应用程序保存了位图之外,我一切正常,但颜色与数组中的颜色略有不同。

This is how I save the colors array: 这是我保存颜色数组的方式:

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();
}

This is how I set a color to a pixel in the array: 这是我为数组中的像素设置颜色的方式:

colors[0] = 0x287026;

What goes wrong? 怎么了? The compression to a PNG and store it into my documents? 压缩为PNG并将其存储到我的文档中? Because when I go to my documents on my phone and check the new PNG created, it's a little bit blurry. 因为当我在手机上查看文档并检查创建的新PNG时,它有点模糊。 Or is this bit of code wrong: Bitmap.Config.RGB_565? 还是这段代码有误:Bitmap.Config.RGB_565? Do I need to use ARGB instead of just RGB? 我需要使用ARGB而不是RGB吗?

Hope someone is able to help me! 希望有人能够帮助我! :) :)
Any help is appreciated! 任何帮助表示赞赏!

Joeri 乔里

Colors are encoded ints, and they can include the alpha channel. 颜色以int编码,并且可以包含alpha通道。 I suspect your colors do include the alpha channel so you are using ARGB colors when your Bitmap format calls for RGB. 我怀疑您的颜色确实包含alpha通道,因此当您的位图格式要求使用RGB时,您正在使用ARGB颜色。 Change your image format to ARGB_8888 which is the default and recommended format. 将图像格式更改为ARGB_8888,这是默认和推荐的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM