Bitmap getpix () setpixt ()

Source: Internet
Author: User

Public class skanbitmap extends activity {
/** Called when the activity is first created .*/
Private bitmap bm; // the image to be scanned
Private bitmap PM; // The Scanned Image
Private int width, height; // the width and height of the image
Private button;
Private imageview;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button = (button) findviewbyid (R. Id. Button );
Imageview = (imageview) findviewbyid (R. Id. imageview );
Bm = bitmapfactory. decoderesource (getresources (), R. drawable. sssd); // obtain a bitmap from the resource file.
Width = BM. getwidth (); // obtain the width of the bitmap.
Height = BM. getheight (); // obtain the height of the bitmap.
Button. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view arg0 ){
// Todo auto-generated method stub
// Create a blank image with the width, height, and format rgb_565.
PM = bitmap. createbitmap (width, height, bitmap. config. rgb_565 );
Imageview. setimagebitmap (PM );
New thread (). Start (); // the thread that starts Scanning Images
}
});
}

// Scan the image thread
Class thread extends thread {
Public void run (){
Button. setclickable (false); // temporarily shields button click events to ensure that only one thread is enabled at a time.
Int [] colors = new int [width]; // retrieves the pixels of a row of images
For (INT I = 0; I // Add the pixels of row I of the bitmap BM to the array colors.
BM. getpixels (colors, 0, width, 0, I, width, 1 );
For (Int J = 0; j <colors. length; j ++ ){
// Reverse the RGB value in the color array, and 255 minus the current color value to obtain the reversed color of the current color.
Colors [J] = color. RGB (255-color.red (colors [J]),

255-color.green (colors [J]), 255-color.blue (colors [J]);
}
PM. setpixels (colors, 0, width, 0, I, width, 1); // After the color is reversed, add the pixel to the line I of PM.
Imageview. postinvalidate (); // refresh the image
}
Button. setclickable (true); // listens for Button clicking events again


}
}

}

BM. getpixels (colors, 0, width, 0, I, width, 1 );
Colors is an array used to store pixels. In the parameter, I is the pixel of row I, and the second width is the width pixel of each row. I am the pixel of the whole row, 1 is to select only one column.
Colors [J] = color. RGB (255-color.red (colors [J]), 255-color.green (colors [J]), 255-color.blue (colors [J]);
Obtain the R, G, and B channel values respectively, and then encapsulate them into a new color value.

In fact, the effects of this instance can be achieved in multiple ways, you can use getpixel (int x, int y) and setpixel (int x, int y, int color) to implement, you can also use getpixels (INT [] pixels, int offset, int stride, int X, int y, int width, int height) and setpixels (INT [] pixels, int offset, int stride, int X, int y, int width, int height.
It is time-consuming to retrieve and set pixels. I tried multiple methods and found that it takes a lot of time to retrieve and set pixels at one point and one point at a time. The best effect is to operate pixels in a row-by-row partition. In fact, I originally wanted to get and set pixels in a vertical column, but I found that using getpixels (INT [] pixels, int offset, int stride, int X, int y, int width, int height) and setpixels (INT [] pixels, int offset, int stride, int X, int y, int width,
Int height) does not apply to implementation. It cannot be similar to a row. I also wonder how this android location won't work, maybe I haven't found a correct method.

Color Android. Graphics. Color

This class defines some methods to create and convert color values. The color is expressed as an encapsulated value. The value consists of four bytes: Alpha, red, green, and blue. These values are not left-multiplied, that is to say, any transparency is only stored in the Alpha part, not in the color components. Each part is saved in the following sequence: (alpha <24) | (Red <16) | <green <8) | blue. the value range of each part is between 0 and 255. 0 means that this part cannot be used, and 100% indicates that takes effect. Therefore, the opaque black should be 0xff000000, And the opaque white should be 0 xffffffff.
The color class provides 12 constant values to represent different color values. In the development project, we can directly call these constant values to set the text, graphics, and other objects we want to modify.

Public int getpixel (int x, int y)

Returns the pixel color value at the specified position. If X or Y crosses the border (negative number, or each of them is greater than or equal to the width or height), an exception is thrown.

Parameters

X coordinate value (0-width-1) of the pixel to be returned ).

Y coordinate value of the pixel to be returned (0-height-1)

Return Value

The argb color value of the specified coordinate.

Exception

If X and Y are out of bounds, iilegalargumentexcepiton will be thrown.

Public void getpixels (INT [] pixels, int offset, int stride, int X, int y, int width, int height)

Copy the bitmap data to pixels. Each one is represented by an int value that represents the color value. The range parameter indicates the row spacing of the pixel array allowed by the caller. For the general filling result, you only need to pass the width value to the amplitude parameter.

Parameters

Pixels receives an array of Bitmap color values

Offset is the first pixel index value written to pixels [].

The row spacing value in stride pixels [] (must be greater than or equal to the bitmap width ). Cannot be negative

The X coordinate value of the first pixel read from the bitmap.

Y: The Y coordinate value of the first pixel read from the bitmap.

Width: The pixel width read from each row.

Number of rows read by height

Exception

Iilegalargumentexcepiton is thrown if the absolute values of X, Y, width, and height are out of bounds or stride is smaller than the bitmap width.

Arrayindexoutofboundsexception is thrown if the pixel array is too small to receive the pixel value of the specified book

Public void setpixel (int x, int y, int color)

Write the specified color to the position of the coordinate values of x and y in the in-place graph (assuming that the bitmap is variable ).

Parameters

X coordinates of the pixels to be replaced (0 to width-1 ).

Y coordinate of the pixel to be replaced (0 to height-1)

Color value of the color written to the in-place Graph

Throw

Iilegalstateexception if this bitmap cannot be changed

Iilegalargumentexception if the value of X and Y exceeds the boundary of the bitmap

Public void setpixels (INT [] pixels, int offset, int stride, int X, int y, int width, int height)

Replace the pixel value of the bitmap with the color value in the array. Each element in the array is a packaged integer, representing the color value.

Parameters

Color value written by pixels to bitmap

Index of the first color value read by offset from pixels []

The number of colors skipped between stride bitmap rows. This value is usually equal to the bitmap width, but it can be larger (or negative)

X is written to the X coordinate of the first pixel in the bitmap.

Y is written to the Y coordinate of the first pixel in the bitmap.

Width: the number of colors in each row copied from pixels []

The number of rows written by height to the in-place graph.

Exception

Iilegalstateexception if this bitmap cannot be changed

Iilegalargumentexception if the values of X, Y, width, and height exceed the boundary of the bitmap

Arrayindexoutofboundsexception if the pixel queue is too small to accept the specified number of pixels.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.