Work encountered in the picture to Gray degree group needs, after research and the guidance of the great God, finally get the following two methods, can realize the bitmap to Gray degree group
The simple bitmap to Gray degree group is: to get each pixel in the bitmap, and then based on the pixel points to get the RGB value, and finally the RGB value, according to Gray-scale algorithm to get gray value can be
/* such as a picture of a 480*800, eventually get a BYTE[480*800/2] of the gray degree group, because the function of each two adjacent high pixel grayscale into a grayscale */
Private byte[] Java_convertimg2greyarray (Bitmap img) {
byte[] thebytes = null;
/* Get the width height of the bitmap */
int width = img.getwidth ();
int height = img.getheight ();
/* Get pixel dots for bitmaps */
int[] pixels = new int[width * height];
Img.getpixels (pixels, 0, width, 0, 0, width, height);
/* Define the result data array */
Thebytes = new byte[width * HEIGHT/2];
/* Define the variables used in the loop, saving memory and time */
int x, y, K;
int pixel, r, G, B;
for (y = 0; y < height; y++) {
for (x = 0, k = 0; × < width; x + +) {
Get pixel points in turn
Pixel = pixels[y * width + x];
Get RGB
r = (pixel >>) & 0xFF;
g = (pixel >> 8) & 0xFF;
b = pixel & 0xFF;
/* Save every two lines as a row */
if (x% 2 = = 1) {
Thebytes[k + y * width/2] = (byte) (Thebytes[k + y
* WIDTH/2] | ((R * 299 + G * 587 + b * + +)/+) & 0xf0);
k++;
} else {
Thebytes[k + y * width/2] = (byte) (Thebytes[k + y
* WIDTH/2] | (((R * 299 + G * 587 + b * + +)/+) >> 4) & 0x0f);
}
}
}
return thebytes;
}
/* Grayscale conversion such as: 480 * 800 finally get a byte[480*800] of the gray degree Group */
private void Java_convertimgtogreyarray (Bitmap img) {
Boolean usedebug = true;
if (Debugimage = = NULL | | debuguihandler = = NULL)
Usedebug = false;
int width = img.getwidth (); Gets the width of the bitmap
int height = img.getheight (); Gets the height of the bitmap
Thebytes = null;
Thebytes = new byte[width * height];
int[] pixels = new int[width * height]; Create a group of pixel points by the size of the bitmap
Img.getpixels (pixels, 0, width, 0, 0, width, height);
for (int i = 0; i < height; i++) {
for (int j = 0; J < width; j + +) {
int coloratpixel = Pixels[width * i + j];
int alpha = (Coloratpixel >>) & 0xFF;
int red = (coloratpixel >>) & 0xFF;
int green = (Coloratpixel >> 8) & 0xFF;
int blue = coloratpixel & 0xFF;
Thebytes[width * i + j] = (byte) (red + green + Blue)/3);
int theb = (0xFF & Thebytes[width * i + j]);
Pixels[width * i + j] = Alpha << 24 | Theb << 16 | Theb << 8
| Theb;
}
}
BMO = img;
Bmo.setpixels (pixels, 0, width, 0, 0, width, height);
pixels = null;
if (Debuguihandler! = null)
Debuguihandler.post (New Runnable () {
@Override
public void Run () {
if (debugimage! = NULL && BMO! = null)
Debugimage.setimagebitmap (BMO);
}
});
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bitmap to grayscale byte array byte[]