This document assumes that the reader has basic image processing concepts.
This is a method just completed today. I have been searching for image fuzzy processing codes for Android on the Internet.
I found reference codes such as reflection and scaling, but none of them mentioned fuzzy processing.
At most, it is mentioned that blurmaskfilter is used for fuzzy processing.
For this harmful post, I wasted a whole afternoon and finally found that it can only be used to process the edge of the painting.
What we need to deal with is the entire image, so this is completely a lie.
Since I have previously processed images on VB. NET and written relevant code, VB. NET code was divided into two forms:
1. Negative calculation: calculate the average value of the color value of each pixel and the surrounding 8 points. setpixel () is calculated every time, which is very slow;
2. Read the memory data of the entire image, that is, the unit of the three primary colors. The data volume (color array) is twice as large as the previous method.
This method can calculate all colors and then re-write them into the memory, which is certainly more efficient than setpixel.
However, because the underlying functions are not used to operate on the memory, although the speed is increased, the effect is dumb compared with C ++.
The main thing is that Java and. NET are very similar (it is said that. Net architects are sun's past ).
Of course, you cannot directly operate on the memory. Of course, you cannot indirectly operate on the memory like. net. It's crazy!
You know, I am dead and won't use the first method to die!
So what should we do? I still want to start with the second method. Although it is slower, it is still tolerable.
Fortunately, Android provides the setpixels () method. It is clear that there is s behind pixel!
This method is actually the "batch" version of setpixel (), which allows us to fill multiple consecutive pixels at once.
In this case, we are half the distance from the second point of simulation, that is, to obtain an array containing the three primary colors. Check the code.
/* Set image blur */
Public static bitmap setblur (bitmap BMP source, int blur) // source bitmap, fuzzy intensity
{
Int pixels [] = new int [BMP source. getwidth () * BMP source. getheight ()]; // color array, one pixel corresponds to one element
Int pixelsrawsource [] = new int [BMP source. getwidth () * BMP source. getheight () * 3]; // an array of three primary colors. As metadata, it cannot be changed when each layer is blurred.
Int pixelsrawnew [] = new int [BMP source. getwidth () * BMP source. getheight () * 3]; // the three primary color array receives the calculated three primary color values.
BMP source. getpixels (pixels, 0, BMP source. getwidth (), 0, 0, BMP source. getwidth (), BMP source. getheight (); // obtain the pixel
// Blur intensity. The intensity of each cycle is increased once.
For (int K = 1; K & lt; = blur; k ++)
{
// Obtain the three primary colors of each pixel from the image
For (INT I = 0; I & lt; pixels. length; I ++)
{
Pixelsrawsource [I * 3 + 0] = color. Red (pixels [I]);
Pixelsrawsource [I * 3 + 1] = color. Green (pixels [I]);
Pixelsrawsource [I * 3 + 2] = color. Blue (pixels [I]);
}
// Take the average value of the top, bottom, and left points of each point as your own value
Int currentpixel = BMP source. getwidth () * 3 + 3; // The processed pixel starting from the vertex (2, 2 ).
For (INT I = 0; I & lt; BMP source. getheight ()-3; I ++) // height Loop
{
For (Int J = 0; J & lt; BMP source. getwidth () * 3; j ++) // width Loop
{
Currentpixel + = 1;
// Take the top, bottom, left, right, and average values
Int sumcolor = 0; // color and
Sumcolor = pixelsrawsource [currentpixel-BMP source. getwidth () * 3]; // previous
Sumcolor = sumcolor + pixelsrawsource [currentpixel-3]; // a little left
Sumcolor = sumcolor + pixelsrawsource [currentpixel + 3]; // right
Sumcolor = sumcolor + pixelsrawsource [currentpixel + BMP source. getwidth () * 3]; // next point
Pixelsrawnew [currentpixel] = math. Round (sumcolor/4); // sets the pixel
}
}
// Combine the new three primary colors into pixel colors
For (INT I = 0; I & lt; pixels. length; I ++)
{
Pixels [I] = color. RGB (pixelsrawnew [I * 3 + 0], pixelsrawnew [I * 3 + 1], pixelsrawnew [I * 3 + 2]);
}
}
// Apply to image
Bitmap BMP return = bitmap. createbitmap (BMP source. getwidth (), BMP source. getheight (), config. argb_8888 );
BMP return. setpixels (pixels, 0, BMP source. getwidth (), 0, 0, BMP source. getwidth (), BMP source. getheight (); // a new bitmap must be created and then filled. The source image cannot be filled directly; otherwise, an error is reported in the memory.
Return BMP return;
}
The code is actually very simple. In general, there are three steps:
1. Obtain the data corresponding to all the bytes of the bitmap;
2. Calculate the fuzzy Average value of each pixel;
3. Use the color array to redraw.
This is different from VB. NET. In VB. NET, we rewrite the memory, and here we apply for a new memory area.
In addition, there is a large loop, which repeats the preceding three steps multiple times. Before each loop, the last calculated average color array is used as the new source data, in this way, the blur effect can be obtained.
Note that this method requires a high performance for the device, on a 1 GHz processor (Samsung Galaxy S i9000) when the Blur intensity is 8, it takes about 20 seconds to process an image of 800x400.
The HTC magic 512 MHz processor is about 40 seconds, which can be said to be basically kept in a fixed proportion.