Android: modifying a certain color value of an image

Source: Internet
Author: User

First, we create a class called Image, which is mainly used to process graph-related operations. [Java] <span style = "font-size: 18px;"> package org.cn. tools; import java. io. IOException; import java. io. inputStream; import android. content. context; import android. graphics. bitmap; import android. graphics. matrix; import android. graphics. bitmap. config; import android. graphics. bitmapFactory; public class Image {public Bitmap bitmap; // Image resource object/*** obtain the Image by id * @ param context * @ param id */public Image (Context context, int id) {bitmap = BitmapFactory. decodeResource (context. getResources (), id);}/*** get the image by using the image file name, it is mainly used when an image is in the Asset directory * @ param context * @ param name image name * @ param scaleW Image Width scaling ratio * @ param scaleH Image Height scaling ratio * @ throws IOException * /public Image (Context context, string name, float scaleW, float scaleH) throws IOException {InputStream is = context. getAssets (). open (name); bitmap = BitmapFactory. DecodeStream (is); is. close (); bitmap = setScaleSize (bitmap, scaleW, scaleH);} public int getWidth () {return bitmap. getWidth ();} public int getHeight () {return bitmap. getHeight ();} /*** put the bitmap pixel value into the array * @ param x * @ param y * @ return */public int [] getPixel (int [] array, int x, int y) {bitmap. getPixels (array, 0, getWidth (), 0, 0, getWidth (), getHeight (); return array ;}/*** Create a Bitmap * @ param array * @ param w * @ param h * @ return */public Bitmap CreateImage (int [] array, int w, int h) {Bitmap image = Bitmap. createBitmap (array, w, h, Config. RGB_565); return image;}/*** image scaling * @ param bitmap * @ param arg0 * @ param arg1 * @ return */private Bitmap setScaleSize (Bitmap bitmap, float arg0, float arg1) {Matrix matrix = new Matrix (); matrix. postScale (arg0, arg1); bitmap = Bi Tmap. createBitmap (bitmap, 0, 0, getWidth (), getHeight (), matrix, true); return bitmap ;} /*** image memory recycling ** @ param Image * @ return */public static void free (image) {try {if (Image. bitmap! = Null) {if (image. bitmap. isRecycled () = false) {// if no memory image occupied by the image is recycled. bitmap. recycle (); if (image. bitmap. isRecycled () = true) {image. bitmap = null ;}} else {image. bitmap = null ;}} else {image. bitmap = null; // return null;} catch (Exception e) {e. printStackTrace () ;}// return image ;}</span> after the image processing class is created, we can perform operations in the activity to create an ImageAct. java. in this example, the white color is changed to red. [Java] <span style = "font-size: 18px;"> package org.cn; import java. io. IOException; import java. lang. character. unicodeBlock; import android. app. activity; import android. graphics. bitmap; import android. graphics. drawable. bitmapDrawable; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. view. windowManager; import andro Id. view. windowManager. layoutParams; import android. widget. imageView; public class ImageAct extends Activity {Image image; int len; int red; int greed; int blue; int apla; int pix;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); try {image = new Image (this, "Alfa_Romeo _ 8C_Competizione_Detail.png ", 1, 1); len = image. getWidth () * image. getHeight (); int [] imageARGB = new int [len]; int [] newimage = new int [len]; image. getPixel (imageARGB, 0, 0); for (int I = 0; I <image. getHeight (); I ++) for (int j = 0; j <image. getWidth (); j ++) {if (imageARGB [I * image. getWidth () + j]! = 0) {// apla = (imageARGB [I * image. getWidth () + j] & 0xaa000000)> 24); red = (imageARGB [I * image. getWidth () + j] & 0x00ff0000)> 16); greed = (imageARGB [I * image. getWidth () + j] & 0x0000ff00)> 8); blue = (imageARGB [I * image. getWidth () + j] & 0x000000ff); // int apla = (5*255/10) <24) | 0x00ffffff; if (red = 255 & greed = 255 & blue = 255) {red = 255; greed = 0; blue = 0; // p Ix = (red <16) | (greed <8) | blue) & apla;} pix = (red <16) | (greed <8) | blue; newimage [I * image. getWidth () + j] = pix;} else newimage [I * image. getWidth () + j] = imageARGB [I * image. getWidth () + j];} Bitmap bitmap = image. createImage (newimage, image. getWidth (), image. getHeight (); (ImageView) findViewById (R. id. imageView1 )). setImageBitmap (bitmap);} catch (IOException e) {// TODO Auto-gener Ated catch block e. printStackTrace () ;}}</span> OK. The preceding two classes can convert the white color in the image to red.

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.