Search This Blog

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


6 comments:

  1. thnx dude grt job.....go on with ur improvosing

    ReplyDelete
  2. Hi, I tried to change the rotation degree from 90 degree to 10 degree for clockwise and -90 to -10 for anticlockwise, when rotating the image, the image gets disappeared after some clicks..
    how is that and any solutions ??

    ReplyDelete
    Replies
    1. Hi johnson, First i am sorry for late reply. I tried with - angle its working. May be your image disappear because of wrong pivot point. Check your pivot point once

      Delete
  3. thanks for this job. how can I do rotation and zooming without buttons?
    I mean,How can I achive rotation and zooming by touching?

    ReplyDelete
    Replies
    1. I think you are talking about pinch zoom. pinching zoom with rotation is something i looking to complete as soon as i will complete , i will post to my blog. Stay in touch on my google profile

      Delete

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code