Introduction to Android image processing instances (figure)

Source: Internet
Author: User

1. Image Processing


1. rounded corner Image

Copy codeThe Code is as follows :/**
* Convert to rounded corner
*
* @ Param bmp
* @ Param roundPx
* @ Return
*/
Public static Bitmap convertToRoundedCorner (Bitmap bmp, float roundPx ){

Bitmap newBmp = Bitmap. createBitmap (bmp. getWidth (), bmp. getHeight (),
Config. ARGB_8888 );
// Get the canvas
Canvas canvas = new Canvas (newBmp );

Final int color = 0xff0000242;
Final Paint paint = new Paint ();
Final Rect rect = new Rect (0, 0, bmp. getWidth (), bmp. getHeight ());
Final RectF rectF = new RectF (rect );

Paint. setAntiAlias (true );
Canvas. drawARGB (0, 0, 0, 0 );
Paint. setColor (color );
// If the second and third parameters are the same, they are the corner of the positive circle. Otherwise, they are the corner of the elliptic.
Canvas. drawRoundRect (rectF, roundPx, roundPx, paint );

Paint. setXfermode (new porterduxfermode (Mode. SRC_IN ));
Canvas. drawBitmap (bmp, rect, rect, paint );

Return newBmp;
}


2. filter effect

1. Black/white Effect

Copy codeThe Code is as follows :/**
* Convert a color chart to a black or white chart
*
* @ Param bitmap
* @ Return returns the converted bitmap.
*/
Public static Bitmap convertToBlackWhite (Bitmap bmp ){
Int width = bmp. getWidth (); // obtain the width of the bitmap.
Int height = bmp. getHeight (); // obtain the height of the bitmap.

Int [] pixels = new int [width * height]; // creates a pixel array based on the bitmap size.

Bmp. getPixels (pixels, 0, width, 0, 0, width, height );
Int alpha = 0xFF <24;
For (int I = 0; I For (int j = 0; j <width; j ++ ){
Int gray = pixels [width * I + j];

Int red = (gray & 0x00FF0000)> 16 );
Int green = (gray & 0x0000FF00)> 8 );
Int blue = (gray & 0x000000FF );

Gray = (int) (red * 0.3 + green * 0.59 + blue * 0.11 );
Gray = alpha | (gray <16) | (gray <8) | gray;
Pixels [width * I + j] = gray;
}
}
Bitmap newBmp = Bitmap. createBitmap (width, height, Config. RGB_565 );
NewBmp. setPixels (pixels, 0, width, 0, 0, width, height );
Return newBmp;
}

2. Gaussian blur

Copy codeThe Code is as follows :/**
* Gaussian blur
*
* @ Param bmp
* @ Return
*/
Public static Bitmap convertToBlur (Bitmap bmp ){
// Gaussian Matrix
Int [] gauss = new int [] {1, 2, 1, 2, 4, 2, 1, 2, 1 };

Int width = bmp. getWidth ();
Int height = bmp. getHeight ();
Bitmap newBmp = Bitmap. createBitmap (width, height,
Bitmap. Config. RGB_565 );

Int pixR = 0;
Int pixG = 0;
Int pixB = 0;

Int pixColor = 0;

Int newR = 0;
Int newG = 0;
Int newB = 0;

Int delta = 16; // The smaller the value, the brighter the image. The larger the value, the darker the image.

Int idx = 0;
Int [] pixels = new int [width * height];
Bmp. getPixels (pixels, 0, width, 0, 0, width, height );
For (int I = 1, length = height-1; I <length; I ++ ){
For (int k = 1, len = width-1; k <len; k ++ ){
Idx = 0;
For (int m =-1; m <= 1; m ++ ){
For (int n =-1; n <= 1; n ++ ){
PixColor = pixels [(I + m) * width + k + n];
PixR = Color. red (pixColor );
PixG = Color. green (pixColor );
PixB = Color. blue (pixColor );

NewR = newR + pixR * gauss [idx];
NewG = newG + pixG * gauss [idx];
NewB = newB + pixB * gauss [idx];
Idx ++;
}
}

NewR/= delta;
NewG/= delta;
NewB/= delta;

NewR = Math. min (255, Math. max (0, newR ));
NewG = Math. min (255, Math. max (0, newG ));
NewB = Math. min (255, Math. max (0, newB ));

Pixels [I * width + k] = Color. argb (255, newR, newG, newB );

NewR = 0;
NewG = 0;
NewB = 0;
}
}

NewBmp. setPixels (pixels, 0, width, 0, 0, width, height );

Return newBmp;
}

3. Sketch EffectCopy codeThe Code is as follows :/**
* Sketch Effect
*
* @ Param bmp
* @ Return
*/
Public static Bitmap convertToSketch (Bitmap bmp ){
Int pos, row, col, clr;
Int width = bmp. getWidth ();
Int height = bmp. getHeight ();
Int [] pixSrc = new int [width * height];
Int [] pixNvt = new int [width * height];
// First, the pixels of the image are processed into grayscale colors and then reversed.
Bmp. getPixels (pixSrc, 0, width, 0, 0, width, height );

For (row = 0; row For (col = 0; col <width; col ++ ){
Pos = row * width + col;
PixSrc [pos] = (Color. red (pixSrc [pos])
+ Color. green (pixSrc [pos]) + Color. blue (pixSrc [pos])/3;
PixNvt [pos] = 255-pixSrc [pos];
}
}

// Perform Gaussian blur on the reversed pixel. The intensity can be set, tentatively set to 5.0
GaussGray (pixNvt, 5.0, 5.0, width, height );

// Calculate the difference between the gray color and the blurred Pixel
For (row = 0; row For (col = 0; col <width; col ++ ){
Pos = row * width + col;

Clr = pixSrc [pos] <8;
Clr // = 256-pixNvt [pos];
Clr = Math. min (clr, 255 );

PixSrc [pos] = Color. rgb (clr, clr, clr );
}
}
Bmp. setPixels (pixSrc, 0, width, 0, 0, width, height );

Return bmp;

}

Private static int gaussGray (int [] psrc, double horz, double vert,
Int width, int height ){
Int [] dst, src;
Double [] n_p, n_m, d_p, d_m, bd_p, bd_m;
Double [] val_p, val_m;
Int I, j, t, k, row, col, terms;
Int [] initial_p, initial_m;
Double std_dev;
Int row_stride = width;
Int max_len = Math. max (width, height );
Int sp_p_idx, sp_m_idx, vp_idx, vm_idx;

Val_p = new double [max_len];
Val_m = new double [max_len];

N_p = new double [5];
N_m = new double [5];
D_p = new double [5];
D_m = new double [5];
Bd_p = new double [5];
Bd_m = new double [5];

Src = new int [max_len];
Dst = new int [max_len];

Initial_p = new int [4];
Initial_m = new int [4];

// Vertical
'If (vert> 0.0 ){
Vert = Math. abs (vert) + 1.0;
Std_dev = Math. sqrt (-(vert * vert)/(2 * Math. log (1.0/255.0 )));

// Initial test constant
FindConstants (n_p, n_m, d_p, d_m, bd_p, bd_m, std_dev );

For (col = 0; col <width; col ++ ){
For (k = 0; k <max_len; k ++ ){
Val_m [k] = val_p [k] = 0;
}

For (t = 0; t Src [t] = psrc [t * row_stride + col];
}

Sp_p_idx = 0;
Sp_m_idx = height-1;
Vp_idx = 0;
Vm_idx = height-1;

Initial_p [0] = src [0];
Initial_m [0] = src [height-1];

For (row = 0; row Terms = (row <4 )? Row: 4;

For (I = 0; I <= terms; I ++ ){
Val_p [vp_idx] + = n_p [I] * src [sp_p_idx-I]-d_p [I]
* Val_p [vp_idx-I];
Val_m [vm_idx] + = n_m [I] * src [sp_m_idx + I]-d_m [I]
* Val_m [vm_idx + I];
}
For (j = I; j <= 4; j ++ ){
Val_p [vp_idx] + = (n_p [j]-bd_p [j]) * initial_p [0];
Val_m [vm_idx] + = (n_m [j]-bd_m [j]) * initial_m [0];
}

Sp_p_idx ++;
Sp_m_idx --;
Vp_idx ++;
Vm_idx --;
}

TransferGaussPixels (val_p, val_m, dst, 1, height );

For (t = 0; t Psrc [t * row_stride + col] = dst [t];
}
}
}

// Horizontal direction
If (horz> 0.0 ){
Horz = Math. abs (horz) + 1.0;

If (horz! = Vert ){
Std_dev = Math. sqrt (-(horz * horz)
/(2 * Math. log (1.0/255.0 )));

// Initial test constant
FindConstants (n_p, n_m, d_p, d_m, bd_p, bd_m, std_dev );
}

For (row = 0; row For (k = 0; k <max_len; k ++ ){
Val_m [k] = val_p [k] = 0;
}

For (t = 0; t <width; t ++ ){
Src [t] = psrc [row * row_stride + t];
}

Sp_p_idx = 0;
Sp_m_idx = width-1;
Vp_idx = 0;
Vm_idx = width-1;

Initial_p [0] = src [0];
Initial_m [0] = src [width-1];

For (col = 0; col <width; col ++ ){
Terms = (col <4 )? Col: 4;

For (I = 0; I <= terms; I ++ ){
Val_p [vp_idx] + = n_p [I] * src [sp_p_idx-I]-d_p [I]
* Val_p [vp_idx-I];
Val_m [vm_idx] + = n_m [I] * src [sp_m_idx + I]-d_m [I]
* Val_m [vm_idx + I];
}
For (j = I; j <= 4; j ++ ){
Val_p [vp_idx] + = (n_p [j]-bd_p [j]) * initial_p [0];
Val_m [vm_idx] + = (n_m [j]-bd_m [j]) * initial_m [0];
}

Sp_p_idx ++;
Sp_m_idx --;
Vp_idx ++;
Vm_idx --;
}

TransferGaussPixels (val_p, val_m, dst, 1, width );

For (t = 0; t <width; t ++ ){
Psrc [row * row_stride + t] = dst [t];
}
}
}

Return 0;
}

Private static void transferGaussPixels (double [] src1, double [] src2,
Int [] dest, int bytes, int width ){
Int I, j, k, B;
Int bend = bytes * width;
Double sum;

I = j = k = 0;
For (B = 0; B <bend; B ++ ){
Sum = src1 [I ++] + src2 [j ++];

If (sum & gt; 255)
Sums = 255;
Else if (sum <0)
Sum = 0;

Dest [k ++] = (int) sum;
}
}

Private static void findConstants (double [] n_p, double [] n_m, double [] d_p,
Double [] d_m, double [] bd_p, double [] bd_m, double std_dev ){
Double div = Math. sqrt (2*3.141593) * std_dev;
Double x0 =-1.783/std_dev;
Double x1 =-1.723/std_dev;
Double x2 = 0.6318/std_dev;
Double x3 = 1.997/std_dev;
Double x4 = 1.6803/div;
Double x5 = 3.735/div;
Double x6 =-0.6803/div;
Double x7 =-0.2598/div;
Int I;

N_p [0] = x4 + x6;
N_p [1] = (Math. exp (x1)
* (X7 * Math. sin (x3)-(x6 + 2 * x4) * Math. cos (x3) + Math
. Exp (x0) * (x5 * Math. sin (x2)-(2 * x6 + x4) * Math. cos (x2 )));
N_p [2] = (2
* Math. exp (x0 + x1)
* (X4 + x6) * Math. cos (x3) * Math. cos (x2)-x5 * Math. cos (x3)
* Math. sin (x2)-x7 * Math. cos (x2) * Math. sin (x3) + x6
* Math. exp (2 * x0) + x4 * Math. exp (2 * x1 ));
N_p [3] = (Math. exp (x1 + 2 * x0)
* (X7 * Math. sin (x3)-x6 * Math. cos (x3) + Math. exp (x0 + 2
* X1)
* (X5 * Math. sin (x2)-x4 * Math. cos (x2 )));
N_p [4] = 0.0;

D_p [0] = 0.0;
D_p [1] =-2 * Math. exp (x1) * Math. cos (x3)-2 * Math. exp (x0)
* Math. cos (x2 );
D_p [2] = 4 * Math. cos (x3) * Math. cos (x2) * Math. exp (x0 + x1)
+ Math. exp (2 * x1) + Math. exp (2 * x0 );
D_p [3] =-2 * Math. cos (x2) * Math. exp (x0 + 2 * x1)-2 * Math. cos (x3)
* Math. exp (x1 + 2 * x0 );
D_p [4] = Math. exp (2 * x0 + 2 * x1 );

For (I = 0; I <= 4; I ++ ){
D_m [I] = d_p [I];
}

N_m [0] = 0.0;
For (I = 1; I <= 4; I ++ ){
N_m [I] = n_p [I]-d_p [I] * n_p [0];
}

Double sum_n_p, sum_n_m, sum_d;
Double a, B;

Sum_n_p = 0.0;
Sum_n_m = 0.0;
Sum_d = 0.0;

For (I = 0; I <= 4; I ++ ){
Sum_n_p + = n_p [I];
Sum_n_m + = n_m [I];
Sum_d + = d_p [I];
}

A = sum_n_p/(1.0 + sum_d );
B = sum_n_m/(1.0 + sum_d );

For (I = 0; I <= 4; I ++ ){
Bd_p [I] = d_p [I] *;
Bd_m [I] = d_m [I] * B;
}
}

4. Sharpen

Copy codeThe Code is as follows :/**
* Image sharpening (Laplace transformation)
*
* @ Param bmp
* @ Return
*/
Public static Bitmap sharpenImageAmeliorate (Bitmap bmp ){

// Laplace Matrix
Int [] laplacian = new int [] {-1,-1,-1,-1, 9,-1,-1,-1,-1 };

Int width = bmp. getWidth ();
Int height = bmp. getHeight ();
Bitmap bitmap = Bitmap. createBitmap (width, height,
Bitmap. Config. RGB_565 );

Int pixR = 0;
Int pixG = 0;
Int pixB = 0;

Int pixColor = 0;

Int newR = 0;
Int newG = 0;
Int newB = 0;

Int idx = 0;
Float alpha = 0.3F;
Int [] pixels = new int [width * height];
Bmp. getPixels (pixels, 0, width, 0, 0, width, height );
For (int I = 1, length = height-1; I <length; I ++ ){
For (int k = 1, len = width-1; k <len; k ++ ){
Idx = 0;
For (int m =-1; m <= 1; m ++ ){
For (int n =-1; n <= 1; n ++ ){
PixColor = pixels [(I + n) * width + k + m];
PixR = Color. red (pixColor );
PixG = Color. green (pixColor );
PixB = Color. blue (pixColor );

NewR = newR + (int) (pixR * laplacian [idx] * alpha );
NewG = newG + (int) (pixG * laplacian [idx] * alpha );
NewB = newB + (int) (pixB * laplacian [idx] * alpha );
Idx ++;
}
}

NewR = Math. min (255, Math. max (0, newR ));
NewG = Math. min (255, Math. max (0, newG ));
NewB = Math. min (255, Math. max (0, newB ));

Pixels [I * width + k] = Color. argb (255, newR, newG, newB );
NewR = 0;
NewG = 0;
NewB = 0;
}
}

Bitmap. setPixels (pixels, 0, width, 0, 0, width, height );
Return bitmap;
}

5. Relief

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.