4

For my application, i have used pinch zoom feature by adapting from the tutorial and creating a custom view http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6-implementing-the-pinch-zoom-gesture/1847

Right now i'm working on capturing the zoomed image as bitmap.

Partially I was able to get the bitmap by making use of

setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(finalView.getDrawingCache());
setDrawingCacheEnabled(false);

Using this approach, I'm getting both the zoomed image and the screen background.

Is there any way to capture only the zoomed image as a bitmap?

Jitendar M
  • 633
  • 1
  • 10
  • 26

3 Answers3

0

This original answer here, just replace the setting background part. Hopefully it helps

public static Bitmap getBitmapFromView(View view) {
    if (view == null) return null;
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    // draw white background on the canvas
    canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}
thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
0

Try

setDrawingCacheEnabled(false)
finalView.setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(finalView.getDrawingCache());
finalView.setDrawingCacheEnabled(false);
weakwire
  • 9,284
  • 8
  • 53
  • 78
0

After having searched a lot, I found a 'Temporary' solution in the same Community & also thanks for the code from 'weakwire' provided in the same post.

Getting coordinates and width/height from a matrix

Idea is simple, all i have do is remove the background screen from the scaled image by cropping. Code is bit lengthy, but worked for me.

  1. First get the Original Bitmap size

    float imgWidth = imgBitmap.getWidth()   
    float imgHeight = imgBitmap.getHeight()
    
  2. Obtain the matrix ( pinch zoom feature used in my application uses matrix 'trick') values, used to scale the image and get the scaledWidth & scaledHeight of the original image.

    float[] values = new float[9];
    matrix.getValues(values);
    float globalX = values[2];
    float globalY = values[5];
    float scaledWidth = values[0]*imgWidth;
    float scaledHeight = values[4]*imgHeight;   
    
    // If zoomed Image gets out of screen, I'm trying to crop the image available in the screen by considering image from (0,0)
    
    if(globalX<0)
    globalX = 0;
    
    if(globalY<0)
    globalY = 0;
    
  3. Finally capture the View and crop the background In my case ("finalView" is the custom View name, where pinch zooming happens)

    setDrawingCacheEnabled(false);
    destroyDrawingCache();
    finalView.setDrawingCacheEnabled(true);
    Bitmap bm = Bitmap.createBitmap(finalView.getDrawingCache());
    finalView.setDrawingCacheEnabled(false);    
    
    //Final Image Width &  Height
    
    int finalWidth = Math.min((int)width,(int)finalView.getWidth());
    int finalHeight = Math.min((int)height,(int)finalView.getHeight());        
    

    // crop to get the scaled image

     Bitmap finalBitmap = Bitmap.createBitmap(bm, (int)globalX, (int)globalY, finalWidth ,finalHeight);
    

Though this is a temporary solution, it works for me. If there is any alternate solution, please post it.

Community
  • 1
  • 1
Jitendar M
  • 633
  • 1
  • 10
  • 26