Bitmap operations in Android

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Bitmap operations in Android

Postby vivekkalady » Sat Nov 22, 2014 11:09 am

Bitmap from Resource

Creating bitmap from resource drawable - If we have image in drawable folder then we can easily create bitmap from it.

Code: Select all

Bitmap bmp= BitmapFactory.decodeResource(getResources(), R.drawable.img);
img.setImageBitmap(bmp);


Creating bitmap from a file stored in sdcard

Code: Select all

        try{
              Bitmap bit=BitmapFactory.decodeFile("file path");
             img.setImageBitmap(bit);
        }catch(Exception e){
           e.getMessage();
        }

        permission in manifest
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


Creating bitmap from URL

Code: Select all

 try{
        InputStream is=(new URL("image Url")).openStream();
        Bitmap bit=BitmapFactory.decodeStream(is);
        img.setImageBitmap(bit);
        }catch(Exception e){
            e.getMessage();
        }
       
        permission in manifest
        <uses-permission android:name="android.permission.INTERNET"/>



Creating bitmap from an imageview

Code: Select all

private Bitmap bmp;
private ImageView img;
img = (ImageView)findViewById(R.id.imageView1);
BitmapDrawable  abmp = (BitmapDrawable)img.getDrawable();

bmp = abmp.getBitmap();



Change Color of pixels in Bitmap

function to change bitmap color to gray

Code: Select all

public void gray(Bitmap bmp){
      operation= Bitmap.createBitmap(bmp.getWidth(),
      bmp.getHeight(),bmp.getConfig());

      double red = 0.33;
      double green = 0.59;
      double blue = 0.11;

   for(int i=0; i<bmp.getWidth(); i++){
      for(int j=0; j<bmp.getHeight(); j++){
         int p = bmp.getPixel(i, j);
         int r = Color.red(p);
         int g = Color.green(p);
         int b = Color.blue(p);

         r = (int) red * r;
         g = (int) green * g;
         b = (int) blue * b;

         operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
      img.setImageBitmap(operation);
   }



Increase brightness of the bitmap

Code: Select all

 public void bright(Bitmap bmp){
      operation= Bitmap.createBitmap(bmp.getWidth(),
      bmp.getHeight(),bmp.getConfig());


      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);

            r = 100  +  r;
            g = 100  + g;
            b = 100  + b;
            alpha = 100 + alpha;

            operation.setPixel(i, j, Color.argb(alpha, r, g, b));
         }
      }
      img.setImageBitmap(operation);
   }


Reduce brightness on the bitmap

Code: Select all

public void dark(Bitmap bmp){
      operation= Bitmap.createBitmap(bmp.getWidth(),
      bmp.getHeight(),bmp.getConfig());


      for(int i=0; i<bmp.getWidth(); i++){
         for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);

            r =  r - 50;
            g =  g - 50;
            b =  b - 50;
            alpha = alpha -50;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));


          }
       }

      img.setImageBitmap(operation);
   }



Rotate Bitmap in android

Rotate Clockwise

Code: Select all

   Matrix mMatrix = new Matrix();
        Matrix mat=img.getImageMatrix();
        mMatrix.set(mat);
        mMatrix.setRotate(90);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
        bitmap.getHeight(), mMatrix, false);
        img.setImageBitmap(bitmap);


for anti-clockwise, change 90 to -90


Zoom in and Zoom Out Image

zoomScale is the variable which you can set to adjust the zoom level.

Zoom in

Code: Select all

bitmap=Bitmap.createScaledBitmap(bitmap,bitmap.getWidth()+zoomScale,
                        bitmap.getHeight()+zoomScale,false);
                img.setImageBitmap(bitmap);


Zoom Out

Code: Select all

 bitmap=Bitmap.createScaledBitmap(bitmap,bitmap.getWidth()-zoomScale,
                        bitmap.getHeight()-zoomScale,false);
                img.setImageBitmap(bitmap);

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron