改变android中的像素颜色。

时间:2022-09-10 19:06:48
testButton.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) 
        {
            Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);for (int i = 0; i < mutableBitmap.getWidth(); i++) 
                {
                    for (int j = 0; j < mutableBitmap.getHeight(); j++) 
                    {
                        int pixel = mutableBitmap.getPixel(i, j);
                        // get red color value
                        int red = Color.red(pixel);
                        int color = Color.argb(0xFF, red, 0, 0);
                        mutableBitmap.setPixel(i, j, color);
                        imageView.setImageBitmap(mutableBitmap);
                    }
                }
        }


        });

I am trying to change the pixel color with only red value. Therefore i get the red value from a given pixel and then tried to replace the same pixel with only that red value. The program runs successfully but when i click the button it crashes out. Can anyone tell me what i am doing wrong

我试图改变像素颜色只有红色的值。因此,我从给定的像素中得到红色的值,然后试图用红色的值替换相同的像素。程序运行成功,但当我点击按钮时,它就崩溃了。有人能告诉我我做错了什么吗?

LOGCAT error

LOGCAT错误

12-03 15:16:57.228: E/AndroidRuntime(5103): FATAL EXCEPTION: main
12-03 15:16:57.228: E/AndroidRuntime(5103): java.lang.IllegalStateException
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.graphics.Bitmap.setPixel(Bitmap.java:1002)
12-03 15:16:57.228: E/AndroidRuntime(5103): at com.example.imaging.AndroidCamera$7.onClick(AndroidCamera.java:216)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.view.View.performClick(View.java:3511)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.view.View$PerformClick.run(View.java:14109)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.os.Handler.handleCallback(Handler.java:605)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.os.Looper.loop(Looper.java:137)
12-03 15:16:57.228: E/AndroidRuntime(5103): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-03 15:16:57.228: E/AndroidRuntime(5103): at java.lang.reflect.Method.invokeNative(Native Method
12-03 15:16:57.228: E/AndroidRuntime(5103): at java.lang.reflect.Method.invoke(Method.java:511)
12-03 15:16:57.228: E/AndroidRuntime(5103): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-03 15:16:57.228: E/AndroidRuntime(5103): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-03 15:16:57.228: E/AndroidRuntime(5103): at dalvik.system.NativeStart.main(Native Method)

3 个解决方案

#1


8  

You are trying to modify pixels of a Immutable bitmap.You can not modify the pixels of a immutable Bitmap. if you try it will throw IllegalStateException.

您试图修改不可变位图的像素。您不能修改不可变位图的像素。如果你尝试,它将抛出IllegalStateException。

use below method to get Mutable Bitmap from Resources

使用下面的方法从资源获取可变位图。

public static Bitmap getMutableBitmap(Resources resources,int resId) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inMutable = true;
    return BitmapFactory.decodeResource(resources, resId, options);
}

or use

或使用

    Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

to get mutable Bitmap from immutable bitmap

从不可变位图获取可变位图。

#2


1  

Reading the documentation, you see that this method might throw an IllegalStateException if you try to set the Pixel in an immutable bitmap.

阅读文档时,您会发现,如果试图在不可变的位图中设置像素,该方法可能会抛出IllegalStateException。

public void setPixel (int x, int y, int color)

Added in API level 1
Write the specified Color into the bitmap (assuming it is mutable) at the x,y coordinate. The color must be a non-premultiplied ARGB value.

Parameters
x   The x coordinate of the pixel to replace (0...width-1)
y   The y coordinate of the pixel to replace (0...height-1)
color   The ARGB color to write into the bitmap
Throws
IllegalStateException   if the bitmap is not mutable
IllegalArgumentException    if x, y are outside of the bitmap's bounds.

If you are using one of those createBitmap methods, most of them return immutable bitmaps, so either use a different creator method, or get a mutable copy of the bitmap you get.

如果您使用的是createBitmap方法之一,它们中的大多数都返回不可变的位图,所以要么使用不同的创建者方法,要么得到您得到的位图的可变副本。

#3


0  

Check this to create the immutable bitmap...

检查这个以创建不可变位图…

testButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Config config = null;
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            if (bitmap.getConfig() == null) {
                config = Config.ARGB_8888;
            } else {
                config = bitmap.getConfig();
            }
            int pixel;
            // Here you ll get the immutable Bitmap
            Bitmap copyBitmap = bitmap.copy(config, true);
            // scan through all pixels
            for (int x = 0; x < width; ++x) {
                for (int y = 0; y < height; ++y) {
                    // get pixel color
                    pixel = bitmap.getPixel(x, y);
                    if (x == 400 && y == 200) {
                        copyBitmap.setPixel(x, y,
                                Color.argb(0xFF, Color.red(pixel), 0, 0));
                    } else {
                        copyBitmap.setPixel(x, y, pixel);
                    }
                }
            }
            imageView.setImageBitmap(copyBitmap);
        }
});

#1


8  

You are trying to modify pixels of a Immutable bitmap.You can not modify the pixels of a immutable Bitmap. if you try it will throw IllegalStateException.

您试图修改不可变位图的像素。您不能修改不可变位图的像素。如果你尝试,它将抛出IllegalStateException。

use below method to get Mutable Bitmap from Resources

使用下面的方法从资源获取可变位图。

public static Bitmap getMutableBitmap(Resources resources,int resId) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inMutable = true;
    return BitmapFactory.decodeResource(resources, resId, options);
}

or use

或使用

    Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

to get mutable Bitmap from immutable bitmap

从不可变位图获取可变位图。

#2


1  

Reading the documentation, you see that this method might throw an IllegalStateException if you try to set the Pixel in an immutable bitmap.

阅读文档时,您会发现,如果试图在不可变的位图中设置像素,该方法可能会抛出IllegalStateException。

public void setPixel (int x, int y, int color)

Added in API level 1
Write the specified Color into the bitmap (assuming it is mutable) at the x,y coordinate. The color must be a non-premultiplied ARGB value.

Parameters
x   The x coordinate of the pixel to replace (0...width-1)
y   The y coordinate of the pixel to replace (0...height-1)
color   The ARGB color to write into the bitmap
Throws
IllegalStateException   if the bitmap is not mutable
IllegalArgumentException    if x, y are outside of the bitmap's bounds.

If you are using one of those createBitmap methods, most of them return immutable bitmaps, so either use a different creator method, or get a mutable copy of the bitmap you get.

如果您使用的是createBitmap方法之一,它们中的大多数都返回不可变的位图,所以要么使用不同的创建者方法,要么得到您得到的位图的可变副本。

#3


0  

Check this to create the immutable bitmap...

检查这个以创建不可变位图…

testButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Config config = null;
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            if (bitmap.getConfig() == null) {
                config = Config.ARGB_8888;
            } else {
                config = bitmap.getConfig();
            }
            int pixel;
            // Here you ll get the immutable Bitmap
            Bitmap copyBitmap = bitmap.copy(config, true);
            // scan through all pixels
            for (int x = 0; x < width; ++x) {
                for (int y = 0; y < height; ++y) {
                    // get pixel color
                    pixel = bitmap.getPixel(x, y);
                    if (x == 400 && y == 200) {
                        copyBitmap.setPixel(x, y,
                                Color.argb(0xFF, Color.red(pixel), 0, 0));
                    } else {
                        copyBitmap.setPixel(x, y, pixel);
                    }
                }
            }
            imageView.setImageBitmap(copyBitmap);
        }
});