Android Image Processing

Source: Internet
Author: User

Android Image Processing

 

The following describes how to process Android images.

RGBA model: red green blue alpha

1. Change the color image, transparency, and brightness.

2. Use color matrix to process images

3. Change pixel processing image

 

1. Use ColorMatrix to change the color, transparency, and brightness.

To obtain a new image, follow these steps:

(1) Create a New BitMap and use the createBitmap () method.

(2) Use a ColorMatrix object to change its color phase, color value, and brightness.

 

/**     * Set the rotation on a color axis by the specified values.     * axis=0 correspond to a rotation around the RED color     * axis=1 correspond to a rotation around the GREEN color     * axis=2 correspond to a rotation around the BLUE color     */

SetRotate (parameter 1, parameter 2) changes the color phase. The first parameter: 0 indicates the changed red, 1 indicates the changed green, 2 indicates that the changed amount is blue, and the second parameter: indicates the depth.

 

SetSaturation (): sets transparency.

PostConcat (): sets the brightness.

(3) Use a paint brush to draw a new BitMap on the canvas.

Set a color selector for the paint brush to connect the color matrix with the paint brush.

SetColorFilter ();

 

 

/*** Gets the color value changed, transparency, brightness BitMap ** @ param bitmap * @ param rorate color value * @ param saturation transparency * @ param scale brightness * @ return */public static Bitmap handleImageEffect (Bitmap bitmap, float rorate, float saturation, float scale) {Bitmap newBitMap = Bitmap. createBitmap (bitmap. getWidth (), bitmap. getHeight (), Bitmap. config. ARGB_8888); // color value: ColorMatrix rorateMatrix = new ColorMatrix (); rorateMatrix. setRotate (0, rorate); // red rorateMatrix. setRotate (1, rorate); // green rorateMatrix. setRotate (2, rorate); // blue // transparency ColorMatrix saturationMatrix = new ColorMatrix (); saturationMatrix. setSaturation (saturation); // brightness ColorMatrix scaleMatrix = new ColorMatrix (); scaleMatrix. setScale (scale, 1); ColorMatrix colorMatrix = new ColorMatrix (); colorMatrix. postConcat (rorateMatrix); colorMatrix. postConcat (saturationMatrix); colorMatrix. postConcat (scaleMatrix); Canvas canvas = new Canvas (newBitMap); Paint paint = new Paint (Paint. ANTI_ALIAS_FLAG); paint. setColorFilter (new ColorMatrixColorFilter (colorMatrix); canvas. drawBitmap (bitmap, 0, 0, paint); return newBitMap ;}


 

Primary_color_activity.xml

 

     
      
       
       
        
        
         
     
    
   
  
 

PrimaryColor. java

 

In initData (), obtain the BitMap displayed by ImageView and display the maximum value and default progress to the SeekBar.

Take setting the color phase as an example:

The maximum value of the color is 255, and the minimum value is 0,

private final int MAX_VALUE = 255; private final int MID_VALUE = 127;


 

 

seek_rorate.setMax(MAX_VALUE);        seek_rorate.setProgress(MID_VALUE);


 

Set a progress change listener for SeekBar

 

@ Override public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {switch (seekBar. getId () {case R. id. seek_rorate: // color phase rorate = (progress-MID_VALUE) * 1.0f/MID_VALUE * 180); break; case R. id. seek_sacle: // brightness sacle = progress * 1.0f/MID_VALUE; break; case R. id. seek_saturation: // transparency saturation = progress * 1.0f/MID_VALUE; break;} image. setImageBitmap (ImageHelper. handleImageEffect (bitmap, rorate, saturation, sacle ));}

:

 

The first seekBar adjusts the color, the second seekBar adjusts the transparency, and the third seekBar adjusts the brightness.

 

2. Use color matrix to process Android Images

Color matrix: 4*5 Matrix

Use color matrix to set Images

(1) Create a New BitMap

(2) Use ColorMatrix to set the color matrix

(3) join the new BitMap Using the canvas

(4) set the color selector of the paint brush-color matrix

(5) Use a paint brush to draw a new BitMap on the canvas.

 

 

/*** Obtain BitMap through the color matrix ** @ param bitmap * @ param flagColor color matrix * @ return */public static Bitmap handleImageColorMatrix (Bitmap bitmap, float [] flagColor) {Bitmap bitmap1 = Bitmap. createBitmap (bitmap. getWidth (), bitmap. getHeight (), Bitmap. config. ARGB_8888); ColorMatrix matrix = new ColorMatrix (); matrix. set (flagColor); Canvas canvas = new Canvas (bitmap1); Paint paint = new Paint (Paint. ANTI_ALIAS_FLAG); paint. setColorFilter (new ColorMatrixColorFilter (matrix); canvas. drawBitmap (bitmap, 0, 0, paint); return bitmap1 ;}


 

Matrix_color_activity.xml

 

     
      
       
   
    
    
   
  
 

Initialize color value

 

 

 private float[] colorMatrix = new float[20];

private void initColorMatrix() {        for (int i = 0; i < 20; i++) {            if (i % 6 == 0) {                colorMatrix[i] = 1;            } else {                colorMatrix[i] = 0;            }        }    }

Add EditText to GridLayout

 

 

private void initEditTextMatrix() {        EditText editText;        for (int i = 0; i < 20; i++) {            editText = new EditText(MatrixColor.this);            editText.setText(String.valueOf(colorMatrix[i]));            editTextMatrix[i] = editText;            gridlayout.addView(editText);        }    }


 

Why is this matrix value set by default? because such a matrix is multiplied by other matrices, the original matrix value will not be changed.

 

When the value in the matrix is changed, click Change to set the new matrix value to BitMap,

 

Private void change (Bitmap bitmap, float [] flagColor) {getEditText (); Bitmap bitmap1 = ImageHelper. handleImageColorMatrix (bitmap, flagColor); // sets ImageView image. setImageBitmap (bitmap1 );}

After clicking Reset, the default value is set to BitMap.

 

 

 

private void reset() {        initColorMatrix();        setEditText();        change(bitmap, colorMatrix);    }    private void setEditText() {        for (int i = 0; i < editTextMatrix.length; i++) {            editTextMatrix[i].setText(String.valueOf(colorMatrix[i]));        }    }

:

 

The first line changes the red color, the second line changes the green color, and the third line changes the blue color.

 

 

3. process images with pixel values

(1) Create a New BitMap

(2) obtain the pixel array of the current BitMap

(3) obtain a new pixel array through algorithms

(4) set a pixel array for the new BitMap

 

/*** Obtain the negative film-control the pixel ** @ param bitmap * @ return */public static Bitmap handleImagePixel (Bitmap bitmap) {// obtain the length and width of the BitMap, it is actually the pixel value int width = bitmap. getWidth (); int height = bitmap. getHeight (); // create a new BitMap Bitmap newBitMap = Bitmap. createBitmap (width, height, Bitmap. config. ARGB_8888); int [] oldPixel = new int [width * height]; int [] newPixel = new int [width * height]; int r, g, B, a, r1, g1, b1; // get The current BitMap pixel // array of the received pixel, get the starting position of the pixel array, get the pixel width, read the starting x coordinate of the pixel, read the starting y coordinate of the pixel, the calculated pixel width range and the calculated pixel height range bitmap. getPixels (oldPixel, 0, width, 0, 0, width, height); for (int I = 0; I <oldPixel. length; I ++) {// get the pixel value int color = oldPixel [I]; // convert the pixel value to red, green, blue, and transparency r = Color. red (color); g = Color. green (color); B = Color. blue (color); a = Color. alpha (color); // obtain the new values of red, green, blue, and transparency. This is only one of the algorithms r1 = 255-r; g1 = 255-g; B1 = 255-B; // The color value ranges from 0 ~ 255, cannot exceed this range if (r1 <0) {r1 = 0;} else if (r1> 255) {r1 = 255;} if (g1 <0) {g1 = 0;} else if (g1> 255) {g1 = 255;} if (b1 <0) {b1 = 0;} else if (b1> 255) {b1 = 255;} // convert the new red, green, blue, and transparency to the ARGB color value. Int newColor = Color. argb (a, r1, g1, b1); // Add it to the new pixel array newPixel [I] = newColor;} // set the pixel array newBitMap for the new BitMap. setPixels (newPixel, 0, width, 0, 0, width, height); return newBitMap ;}

Pixel_color_activity.xml

 

 

     
          
           
        
    
   
  
 


 

 

PixelColor. java

 

 bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.beauty);        image1 = (ImageView) findViewById(R.id.image1);        image2 = (ImageView) findViewById(R.id.image2);        image1.setImageBitmap(bitmap);        image2.setImageBitmap(ImageHelper.handleImagePixel(bitmap));

:

 

 

 

 

 

Related Article

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.