嗨伙计们,我需要你的帮助,我正在尝试使用红色,绿色,蓝色的平均值将彩色图像转换为灰度.但它出现了错误,
这是我的代码
imgWidth = myBitmap.getWidth();
imgHeight = myBitmap.getHeight();
for(int i =0;i<imgWidth;i++) {
for(int j=0;j<imgHeight;j++) {
int s = myBitmap.getPixel(i, j)/3;
myBitmap.setPixel(i, j, s);
}
}
ImageView img = (ImageView)findViewById(R.id.image1);
img.setImageBitmap(myBitmap);
Run Code Online (Sandbox Code Playgroud)
但是当我在模拟器上运行我的应用程序时,它会强制关闭.任何的想法?
我已经用以下代码解决了我的问题:
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get one pixel color
pixel = src.getPixel(x, y);
// retrieve color of all channels
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// take conversion …Run Code Online (Sandbox Code Playgroud) 当我在android中向用户显示时,我想知道将彩色图像(我从网上下载)转换为黑白的方法.任何人都可以在你的任何Android工作中找到这个要求.请告诉我.
谢谢Lakshman
我正在尝试使用 android compose 将显示的彩色图像转换为黑白图像。
在视图系统中,我可以通过添加这样的过滤器将图像从彩色变为黑白
imageView.colorFilter = ColorMatrixColorFilter(ColorMatrix().apply { setSaturation(0f)})
Run Code Online (Sandbox Code Playgroud)
如本答案所示。
在 Android Compose 中,Image 可组合函数已经采用了颜色过滤器,但我在 compose 包中找不到等效的ColorMatrixColorFilter。
这是我想转换为灰度的图像代码
Image(
asset = vectorResource(id = R.drawable.xxx),
modifier = Modifier.clip(RectangleShape).size(36.dp, 26.dp),
alpha = alpha,
alignment = Alignment.Center,
contentScale = ContentScale.Fit
)
Run Code Online (Sandbox Code Playgroud) 我正在尝试将普通彩色图像转换为灰度图像。代码非常简单,但不知道为什么会出现错误。我只是逐个像素地更改颜色值,然后将其存储在新的位图中。错误即将到来我正在尝试将像素设置为新位图。
Bitmap c=BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/1.jpg");
int width=c.getWidth();
int height=c.getHeight();
int A,B,R,G;
int pixel;
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
// get one pixel color
// pixel = c.getPixel(x, y);
// retrieve color of all channels
// A = Color.alpha(c.getPixel(x, y));
R = Color.red(c.getPixel(x, y));
G = Color.green(c.getPixel(x, y));
B = Color.blue(c.getPixel(x, y));
// take conversion up to one single value
R = G = B = (int)(0.299 * …Run Code Online (Sandbox Code Playgroud) 我有一个对应于“灰度位图”(一个字节-> 一个像素)的字节数组,我需要为此图像创建一个 PNG 文件。
下面的方法有效,但创建的 png 很大,因为我使用的位图是 ARGB_8888 位图,每个像素需要 4 个字节而不是 1 个字节。
我无法让它与 ARGB_8888 不同的其他 Bitmap.Config 一起工作。也许 ALPHA_8 是我需要的,但我一直无法让它工作。
我也尝试过其他一些帖子中包含的 toGrayScale 方法(在 Android 中将位图转换为灰度),但我对大小有同样的问题。
public static boolean createPNGFromGrayScaledBytes(ByteBuffer grayBytes, int width,
int height,File pngFile) throws IOException{
if (grayBytes.remaining()!=width*height){
Logger.error(Tag, "Unexpected error: size mismatch [remaining:"+grayBytes.remaining()+"][width:"+width+"][height:"+height+"]", null);
return false;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// for each byte, I set it in three color channels.
int gray,color;
int x=0,y=0;
while(grayBytes.remaining()>0){
gray = grayBytes.get();
// integer may be …Run Code Online (Sandbox Code Playgroud)