getPixel returns a negative value in Android

Friends , am new to android , and am working with a project where i want some Image processing s. And when I tried to use

int pixels=myImageBitmap.getPixel(x,y);

all I am getting is some negetive values in x Also I am trying to set some values for that pixel using

result.setPixel(x,y,pixels);

My application stopping suddenly and , after that , my log cat is looks like

03-06 19:52:01.731: W/dalvikvm(29185): threadid=1: thread exiting with uncaught exception (group=0x41036378)
03-06 19:52:01.731: E/AndroidRuntime(29185): FATAL EXCEPTION: main
03-06 19:52:01.731: E/AndroidRuntime(29185): java.lang.IllegalStateException
03-06 19:52:01.731: E/AndroidRuntime(29185):    at android.graphics.Bitmap.setPixel(Bitmap.java:1045)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at com.example.testpro.EncodeActivity$3.onClick(EncodeActivity.java:90)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at android.view.View.performClick(View.java:4147)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at android.view.View$PerformClick.run(View.java:17161)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at android.os.Handler.handleCallback(Handler.java:615)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at android.os.Looper.loop(Looper.java:213)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at android.app.ActivityThread.main(ActivityThread.java:4787)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at java.lang.reflect.Method.invokeNative(Native Method)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at java.lang.reflect.Method.invoke(Method.java:511)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
03-06 19:52:01.731: E/AndroidRuntime(29185):    at dalvik.system.NativeStart.main(Native Method)

Somebody please help me to get out of this error.please Thanks in advance

Answers


java.lang.IllegalStateException at android.graphics.Bitmap.setPixel(Bitmap.java:1045)

This exception is thrown if the bitmap you are using is immutable. As per document

Reason :

result.setPixel(x,y); 

Here you are trying to make change in the pixels of the bitmap. Since the bitmap is immutable,it is not allowed hence it is throwing above exception.

Solution: Make a mutable copy of the bitmap.And use it.

myImageBitmap = myImageBitmap.copy(Bitmap.Config.ARGB_8888, true);

For more information Refer Android- convert imutable bitmap to mutable

Update :

when i compare the pixel values of the two bitmap images , first one and the copy , i am getting different values for same pixel , why?

copy(Bitmap.Config config, boolean isMutable)

Tries to make a new bitmap based on the dimensions of the bitmap, setting the new bitmap's config to the one specified, and then copying the bitmap's pixels into the new bitmap. If the conversion is not supported, or the allocator fails, then this returns NULL. Refer this

The bitmap configuration (Bitmap.Config) specified in the method, describes how pixels are stored. This affects the quality (color depth) as well as the ability to display transparent/translucent colors.

And if you look at the documentation of the getPixel() , it returns an integer which is a colour value.

So , if the Bitmap config used for original and copied image is different, you get different values for the same pixel.

One more reason that might lead to difference in pixel value is Pre-multiplication/Non-premultiplication of pixels. Refer this nice information from Chet Hasse.


Need Your Help

Creating my own strcmp () function in C

c strcmp

I was assigned by my teacher to write my own strcmp() function in C. I did create my own version of said function, and I was hoping to get some feedback.

Redis SLAVE flushes data when MASTER goes down

redis replication

I have commented the "save" commands in both my master and slave as I want to do only in memory caching and not persist to the file. This works fine but as soon as the Master goes down and before S...