How to get scaled Bitmap from ImageView - android

I am having a Activity which has a custom ImageView defined in the XML for Pinch Zoom functionality on an image. Now, my problem is that I want to fetch the scaled Bitmap from the Pinch Zoom functionality. For example if the user peforms zooming on the Image, then I want to store the exact size and the position of the Image as Bitmap.
This is my custom ImageView declared in XML.
<com.cam.view.PinchZoom_ImageView
android:id="#+id/gallery_selected_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/icon"
android:scaleType="center"
/>
UPDATE:
I am trying the below code to get the scaled Image, but It returns a Blank Bitmap.
pinchZoom.setDrawingCacheEnabled(true);
pinchZoom.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
pinchZoom.layout(0, 0, pinchZoom.getMeasuredWidth(), pinchZoom.getMeasuredHeight());
pinchZoom.buildDrawingCache(true);
Bitmap_gallery_Item = Bitmap.createBitmap(pinchZoom.getDrawingCache());
pinchZoom.setDrawingCacheEnabled(false);
Any help would be appreciated. Thanks

Well, finally I can't believe that it was just a one line code. I finally succeeded using the matrix. The solution is very simple that I was storing the translation and scaling of the Image in a matrix. So, I declared that final matrix as public static and used that matrix to create draw a Bitmap on canvas.
Canvas comboImage = new Canvas(cs);
background = Bitmap.createScaledBitmap(background, width, height, true);
comboImage.drawBitmap(background, 0, 0, null);
comboImage.drawBitmap(foreground, PinchZoom_ImageView.matrix, null);
As you can see here PinchZoom_ImageView.matrix. It is a matrix that contains my final position of the scaled and translated image which I had declared as public static.

If you have scaleType="fitxy" then you can use a scaling factor to enlarge or shrink the width/height of the ImageView - It also means that you can pull back the exact height/width of the image because it has the same dimensions as it's container.
HTH

This post includes Cropping the Image from the Gallery and allows user to crop the Image and save the cropped Image and Shows in ImageView.
Seems it will help you some extends
Image Crop and show cropped Image in View

Related

Set android bitmap size keeping aspect ratio based on minimum height and width

I'm currently loading an image that can be either landscape or portrait.
I'm then wanting to resize the bitmap to draw directly onto a canvas for a full screen image.
I need keep the aspect ratio but have the image not fit to the screen but crop off any image that's bigger than the screen.
I can resize it and fit it to the screen with the following:
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, b.getWidth(), b.getHeight()), new RectF(0, 0, MyWallpaperService.this.width, MyWallpaperService.this.height), Matrix.ScaleToFit.CENTER);
b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
but I don't want to have the black bars at the top/bottom or sides, but I can't think of the routine to resize correctly for this.
As you are drawing the image onto the canvas yourself the first step is to calculate the dimensions required for the final image based on the screen size and the original image aspect ratio.
Then use the static function createScaledBitmap from the Bitmap class to resize your bitmap to match the calculated dimensions. Make sure you only call this function once and store the bitmap for use in the drawing routine.
Finally draw the bitmap so only the area of the bitmap you want to see is on the screen.
A more memory friendly approach is to add another step to crop the image using Bitmap.createBitmap before calling Bitmap.createScaledBitmap. This reduces the chance of encountering memory issues caused by the user selecting a source image that is thin and long.

How to screen shot a ImageView without lost quality

I have two ImageView's in a RelativeLayout, the first one is as large as the RelativeLayout size (as background),and the second ImageView has a small image, now I want to combine these to ImageView's image into one by screenshot. not by combine two bitmap directly.
In the snippet below, I successfully take screenshot of the RelativeLayout, but I found the image quality is not as good as the first ImageView's bitmap, can anyone help me please?
view.setDrawingCacheEnabled(true);
view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap drawCache = view.getDrawingCache(true);
Try to get bitmap using window DecorView :
View window = activity.getWindow().getDecorView()
Canvas bitmapCanvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(window.getWidth()*2, window.getHeight()*2, Bitmap.Config.ARGB_8888);
bitmapCanvas.setBitmap(bitmap);
bitmapCanvas.scale(2.0f, 2.0f);
window.draw(bitmapCanvas);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, myOutputStream);
Ref : High resolution screen shot in Android

How do I configure ImageView to display subrect of bitmap in Android?

I have a view that contains an ImageView. The ImageView is populated with a png resource, and fills the width and height of the image view.
Instead of this configuration, I would like to display the image zoomed in by an arbitrary percentage, and translated by an arbitrary coordinate. My question is, how can I configure an image view to display a sub-region of it's src bitmap, with arbitrary translation?
You can do this with matrix scaleType
Make a ImageView to have matrix scaleType, android:scaleType="matrix"
Create and manipulate a Matrix like this,
Matrix matrix = new Matrix();
matrix.postTranslate(-10, -10);
matrix.postScale(2, 2);
set this matrix to ImageView, imageView.setImageMatrix(matrix)

set image size to full screen in android

I'm having an android app in which I'm taking a picture using the android camera.
This picture is taken in the activity A and after that is sent to activity B where is edited.
This is how I receive my image in activity B:
Bundle extras = getIntent().getExtras();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 5;
byte[] imageData = extras.getByteArray("imageData");
Bitmap myImage = BitmapFactory.decodeByteArray(imageData , 0, imageData.length,options);
Matrix mat=new Matrix();
mat.postRotate(90);
bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(), myImage.getHeight(), mat, true);
ImageView imageView = (ImageView) findViewById(R.id.myPic);
imageView.setImageBitmap(bitmapResult);
As you can see I'm rotating the bitmap I receive with 90 using this:
Matrix mat=new Matrix();
mat.postRotate(90);
And here is my imageView in xml file:
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="#+id/myPic"
/>
The problem that I'm facing is that my picture is not full screen...it's almoust full screnn but not entirly.As you can see here:
If you could help me to increase a little bit its size I would really apreciate it.Thanks
EDIT:I wanna increase the width of my image only.Sorry!!
You can use ImageView.ScaleType
ImageView.ScaleType="CENTER_CROP"
You might have to crop the image. Because the image is not at its full height, the width of the image is proportionately scaled down, hence the black strips at the sides.
To be more specific, images taken using the phone's camera are probably intended to fit the screen exactly i.e. width-height ratio of image = width-height ratio of the screen. However, in your app the height of the image is constrained by the buttons at the top, so in order to maintain the width-height ratio of the image, the width of the image is scaled down proportionately.
Please refer to setScaleType
You could use CENTER_CROP to get full screen image and maintain the image's aspect ratio.

Image Processing on Custom ImageView and saving the modified image

I am using a Custom ImageView. In its onDraw I do some processing by using the canvas which is like drawing line, bitmaps based on user touch.
To save the resultant snapshot when the user want to save the image, we are using drawingCache
imageView.setDrawingCacheEnabled(true;
Bitmap.createBitmap(imageView.getDrawingCache(), 0, 0,
imageView.getHeight(), imageView.getWidth(),
matrix, true).compress(CompressFormat.JPEG, 95,
new FileOutputStream(file));
Of course I can use this to get the actual image while user presses save but the image does not contain any of the processing done on the imageView.
Bitmap bmp = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
Bitmap.createBitmap(bmp, 0, 0,
bmp.getWidth(), bmp.getHeight(),
matrix, true).compress(CompressFormat.JPEG, 95,
new FileOutputStream(file));
Problem:
The size of the image saved is the size of the imageView which is fullscreen but depends on the device. For smaller screen size phones it goes down to 240 X 320.
Question:
a. Is it possible to get a decent size image irrespective of the device screen size?
b. Is it possible to do Image Processing directly on the Image that is being used in the Custom ImageView?
Thanks in anticipation!
Any thoughts?
Maybe an alternative would be use the Canvas class to do some of your drawing and image display: javadoc: android.graphics.Canvas.
The ImageView class is not the best when images need to "refreshed" often.
If you decide to stick to ImageView, don't create the bitmap using the ImageView's dimensions. Retain the aspect ratio of the image, and compose your bitmap according to the big dimensions you need. The imageView will shrink it (if needed) to fit the screen.
You can use
Bitmap.createScaledBitmap(Bitmap b, int newWidth, int newHieght, boolean filter);
and to get a bitmap from resources use
Bitmap image = BitmapFactory.decodeResource(..)
you can then use
ImageView.setImageBitmap(Bitmap bm)
to set the new bitmap to the imageview

Categories

Resources