Android Bitmap loading and pixel operations

Source: Internet
Author: User

Android Bitmap loading and pixel operations
Android Bitmap loading and pixel operations

I. Loading and pixel read/write
In the Android SDK, pixel reading and writing of images can be achieved through the API of getPixel and setPixel Bitmap. The code for Bitmap API to read pixels is as follows:

int pixel = bitmap.getPixel(col, row);// ARGBint red = Color.red(pixel); // same as (pixel >> 16) &0xffint green = Color.green(pixel); // same as (pixel >> 8) &0xffint blue = Color.blue(pixel); // same as (pixel & 0xff)int alpha = Color.alpha(pixel); // same as (pixel >>> 24)

The pixel is a 32-bit integer. The four bytes correspond to transparent channels, Red Channels, green channels, and blue channels respectively. The Bitmap API writes pixels. The Code is as follows:

bm.setPixel(col, row, Color.argb(alpha, red, green, blue));

The pixel value is re-assembled into an int using Color. argb.
Use BitmapFactory. when decodeFile or decodeResource is used to load the Bitmap object of an image, these methods allocate a suitable size of memory for the Bitmap object to be constructed. If the original image file has a large amount of data, the requested memory size cannot be allocated to the DVM, resulting in out of memory (OOM) problems. By configuring BitmapFactory. Option to read the Image Height and bandwidth in advance, the image can be properly sampled to avoid OOM problems. The code for obtaining only the Image Height and bandwidth in advance is as follows:

// Obtain Bitmap image size and type attributes BitmapFactory. options options = new BitmapFactory. options (); options. inJustDecodeBounds = true; BitmapFactory. decodeResource (getResources (), R. drawable. shar_03, options); int height = options. outHeight; int width = options. outWidth; String imageType = options. outMimeType;

Downsample loading of ultra-large Bitmap images:

// Undersampling int inSampleSize = 1; if (height> reqHeight | width> reqWidth) {final int halfHeight = height/2; final int halfWidth = width/2; // Calculate the largest inSampleSize value // that is a power of 2 and keeps both // height and width larger than the requested height and width. while (halfHeight/inSampleSize)> reqHeight & (halfWidth/inSampleSize)> reqWidth) {inSampleSize * = 2 ;}// obtain the image display after sampling, avoid OOM problems. options. inJustDecodeBounds = false; srcImage = BitmapFactory. decodeResource (getResources (), R. drawable. shar_03, options );

Ii. pixel operations
Three simple methods for Grayscale android color images
Grayscale method 1:
GRAY = (max (red, green, blue) + min (red, green, blue)/2
Method 2 of grayscale:
GRAY = (red + green + blue)/3
Method 3 of grayscale:
GRAY = red * 0.3 + green * 0.59 + blue * 0.11
The code is implemented as follows:

public Bitmap gray(Bitmap bitmap, int schema){    Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());    int width = bitmap.getWidth();    int height = bitmap.getHeight();    for(int row=0; row
  
   > 16) &0xff          int green = Color.green(pixel); // same as (pixel >> 8) &0xff          int blue = Color.blue(pixel); // same as (pixel & 0xff)          int alpha = Color.alpha(pixel); // same as (pixel >>> 24)          int gray = 0;          if(schema == 0)          {              gray = (Math.max(blue, Math.max(red, green)) +                           Math.min(blue, Math.min(red, green))) / 2;          }          else if(schema == 1)          {              gray = (red + green + blue) / 3;          }          else if(schema == 2)          {              gray = (int)(0.3 * red + 0.59 * green + 0.11 * blue);          }          bm.setPixel(col, row, Color.argb(alpha, gray, gray, gray));       }    }    return bm;}
  

The Bitmap image ing and brightness adjustment code is as follows:

public Bitmap brightness(Bitmap bitmap, double depth){    Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());    int width = bitmap.getWidth();    int height = bitmap.getHeight();    for(int row=0; row
  
   > 16) &0xff          int green = Color.green(pixel); // same as (pixel >> 8) &0xff          int blue = Color.blue(pixel); // same as (pixel & 0xff)          int alpha = Color.alpha(pixel); // same as (pixel >>> 24)          double gray = (0.3 * red + 0.59 * green + 0.11 * blue);          red += (depth * gray);          if(red > 255) { red = 255; }          green += (depth * gray);          if(green > 255) { green = 255; }          blue += (depth * gray);          if(blue > 255) { blue = 255; }          bm.setPixel(col, row, Color.argb(alpha, red, green, blue));       }    }    return bm;}public Bitmap flip(Bitmap bitmap){    Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());    int width = bitmap.getWidth();    int height = bitmap.getHeight();    for(int row=0; row
   
    > 16) &0xff          int green = Color.green(pixel); // same as (pixel >> 8) &0xff          int blue = Color.blue(pixel); // same as (pixel & 0xff)          int alpha = Color.alpha(pixel); // same as (pixel >>> 24)          int ncol = width - col - 1;          bm.setPixel(ncol, row, Color.argb(alpha, red, green, blue));       }    }    return bm;}
   
  

Run:

The content of the layout XML file is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;">

The onCreate method code in MainActivity is as follows:

Super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ImageView iView = (ImageView) this. findViewById (R. id. image_content); Bitmap B = Bitmap. createBitmap (400,400, Bitmap. config. ARGB_8888); Paint paint = new Paint (Paint. ANTI_ALIAS_FLAG); paint. setColor (Color. BLACK); Canvas c = new Canvas (B); c. drawText (Load Image from here ..., 50,200, paint); iView. setImageBitmap (B); Button saveBtn = (Button) this. findViewById (R. id. button_save); saveBtn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {Toast toast = Toast. makeText (getApplicationContext (), Please load the image firstly ..., toast. LENGTH_SHORT); toast. show (); loadImage (); ImageView iView = (ImageView) findViewById (R. id. image_content); iView. setImageBitmap (srcImage); if (srcImage! = Null) {// saveFile (srcImage) ;}}); Button processBtn = (Button) this. findViewById (R. id. button_gray_3); proc1_tn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {if (srcImage = null) {loadImage ();} ImagePixelsProcessor processor = new ImagePixelsProcessor (); bitmap bm = processor. gray (srcImage, 2); // There are different grayscale policies final ImageView iView = (ImageView) findViewById (R. id. image_content); iView. setImageBitmap (bm) ;}}); Button inverseBtn = (Button) this. findViewById (R. id. button_inverse); inverseBtn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {if (srcImage = null) {loadImage ();} ImagePixelsProcessor processor = new ImagePixelsProcessor (); bitmap bm = processor. brightness (srcImage, 0.3); final ImageView iView = (ImageView) findViewById (R. id. image_content); iView. setImageBitmap (bm) ;}}); Button noRedBtn = (Button) this. findViewById (R. id. button_gray_1); noRedBtn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {if (srcImage = null) {loadImage ();} ImagePixelsProcessor processor = new ImagePixelsProcessor (); bitmap bm = processor. gray (srcImage, 0); // There are different grayscale policies final ImageView iView = (ImageView) findViewById (R. id. image_content); iView. setImageBitmap (bm) ;}}); Button gray2Btn = (Button) this. findViewById (R. id. button_gray_2); gray2Btn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {if (srcImage = null) {loadImage ();} ImagePixelsProcessor processor = new ImagePixelsProcessor (); bitmap bm = processor. gray (srcImage, 1); // There are different grayscale policies final ImageView iView = (ImageView) findViewById (R. id. image_content); iView. setImageBitmap (bm) ;}}); Button flipBtn = (Button) this. findViewById (R. id. button_flip); flipBtn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {if (srcImage = null) {loadImage ();} ImagePixelsProcessor processor = new ImagePixelsProcessor (); bitmap bm = processor. flip (srcImage); final ImageView iView = (ImageView) findViewById (R. id. image_content); iView. setImageBitmap (bm );}});

 

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.