Search This Blog

Support my cause by downloading

Saturday, 12 May 2012

Bitmap operations like re sizing, rotating bitmap and other operations

In programming, Image processing is the most difficult work. All though i am not going to discuss image processing in depth but we will discuss about bitmap basic operation like re sizing, rotating bitmap, how to create bitmap from file , input stream and resource.we will discuss it step by step and finally you will get source in which you can enjoy playing with it. img is the ImageView object in my project.
As we are going to discuss bitmap to we need to study how to avoid Memory Over Flow while using big image


1) Creating bitmap from resource drawable - If we have image in drawable folder then we can easily create bitmap from it. Later in my project i have a image view on which i will set a newly created bitmap

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

2) Creating bitmap from a file stored in sdcard - Give complete string path from sdcard .if you want to select path dynamically then you can see File explorer.

        /**
        *Creating bitmap from a file
        *Permission needed in manifest 
        *<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        */
        try{
         Bitmap bit=BitmapFactory.decodeFile("file path");
         img.setImageBitmap(bit);
        }catch(Exception e){
        e.getMessage();
        }

3) Creating bitmap from URL - Give complete url in to string and create a URL from this

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

4) Changing bitmap to drawable and drawable to bitmap - Some times we need to change drawable to bitmap and bitmap to drawable

        /**
         * Changing drawable to  bitmap, android bitmap to drawable
         */
        Drawable d=new BitmapDrawable(bitmap);
        //use drawable where ever you want
        BitmapDrawable bitmDraw=(BitmapDrawable) d;
        Bitmap mp=bitmDraw.getBitmap();
        //Now use mp where you want
        
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img);
        img.setImageBitmap(bitmap);

5) Rotating a bitmap anticlockwise and clock wise - Matrix is used to rotate bitmap as per our requirement. I have two button to rotate image as you want

        /**
         * Rotate a bitmap clockwise and anticlockwise
         */
        btn_clock = (Button) findViewById(R.id.btn_clockWise);
        btn_clock.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                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);
            }
        });
        btn_antiClock = (Button) findViewById(R.id.btn_AnticlockWise);
        btn_antiClock.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                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);
            }
        });



6) Zoom in and zoom out image using bitmap scale option - we will scale bitmap and then set it to  image view object that is img in my project code

        btn_zoomin = (Button) findViewById(R.id.btn_in);
        btn_zoomin.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                zoomScale+=zoomScale;
                bitmap=Bitmap.createScaledBitmap(bitmap,bitmap.getWidth()+zoomScale,
                        bitmap.getHeight()+zoomScale,false);
                img.setImageBitmap(bitmap);
            }
        });
        btn_zoom_out = (Button) findViewById(R.id.btn_zoomout);
        btn_zoom_out.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                zoomScale-=zoomScale;
                bitmap=Bitmap.createScaledBitmap(bitmap,bitmap.getWidth()-zoomScale,
                        bitmap.getHeight()-zoomScale,false);
                img.setImageBitmap(bitmap);
            }
        });
    }

Download source code from here ..please click on advertisement and keep visiting my blog :)


                             Download Source Code


Android News and source code