Android resolution independent Bitmap from ImageView - java

I have a Bitmap which is 1200 pixels in both height and width.
And I put it in ImageView. My app requires that later on I get the Bitmap from the ImageView so that I get the same quality of the Bitmap just when put in the ImageView. If I do it with getDrawingCache for the Layout I get bad image since I have to scale to the original size of the Bitmap and there is quality loss.
So is there any resolution independent way to get the image from the ImageView in its original size?
Thanks

Just use the getDrawable function.
Drawable image=imageview.getDrawable ()

Related

How to make bitmap bigger without resizing the actual image

I am using Pax A920 device.
I have an issue when printing a bitmap, there is no option to center the bitmap(image) documentation
Methods reference:
The issue is when the bitmap's width is short, its left-aligned on the printed receipt.
I can scale the bitmap using:
Bitmap.createScaledBitmap(receiptLogo, 512, receiptLogo.getHeight(), false);
But it looks bad, I'm thinking of creating a new Bitmap with width of 512 & height of receipt.Logo.getHeight() then put the receipt logo in the middle so it wont look stretch.
Is this doable?

BItmap appears to be rotated after rendering to imageview android camera api ndk?

I am trying to learn how to use ndk and renderscript. This is what i tried so far. I am using renderscript to convert nv21 frame to rgb. After that, i am getting the pixels from the rgb and sending the pixels to the ndk script to convert to grayscale. Then i created a bitmap from the grayscale pixels and i am rendering the bitmap to the imageview. Things seem to be working fine. However, I am facing an issue. When the preview is rendered in the imageview, the bitmap appears to be rotated. What could i be doing wrong ?
The NV21 frame received from the camera on most phones is always in landscape (see CameraInfo.orientation.
I tried rotating the imageview but then the grayscale effect disappears
Please show your code, there is some easy typo probably

Bitmap quality lost after scaling canvas

I want to paint a Bitmap scaled in a canvas (based on the Bitmap and the View size). The problem is that if I scale the canvas before painting the bitmap:
canvas.scale(1.5f,1.5f);
canvas.drawBitmap(mBitmap, 0, 0, paint);
This is the result:
But if instead of scaling the canvas I scale the bitmap:
Bitmap scaled = Bitmap.createScaledBitmap(
mBitmap, bitmapWidth, bitmapHeight,
true);
canvas.drawBitmap(scaledBitmap, 0, 0, paint);
The result is much better:
The problem is that the Bitmap is pretty big and having in memory both the original and the scaled bitmap may be problematic.
Is there any way to obtain the second result without creating a scaled Bitmap every time the Bitmap or the View bounds changes?
Use a matrix that contains the scaling info when drawing to canvas
canvas.drawBitmap(x,y,..., matrix...)
Use inSampleSize in BitmapOptions when reading the bitmap from your source to scale it accordingly (it is passed as an extra parameter).
This is androids preferred way of loading images in nearly all cases to avoid memory overflow, as the full image is Never loaded into memory.
The android docs on Loading BItmaps efficiently can be found here.
With example code on how to retrieve an image of desired size from your source. (resource, file, etc etc)
The two methods in Loading Large Bitmaps Efficiently are the ones you are looking for.
The solution to my problem was that the Paint hasn't enabled the FILTER_BITMAP_FLAG flag.

Attempting to get Bitmap pixel from an ImageView's onTouchListener

I am displaying a Bitmap on an ImageView. When the ImageView is tapped, I would like to get the pixel x/y coordinates for the Bitmap where it was tapped. I have registered the ImageView's onTouchListener, and within the onTouch method, I use getX and getY to get the location of the touch. The problem is that the image within the ImageView may be larger than the view itself, so it is scaled down to fit the screen. The x and y coordinates returned, then, are the coordinates of the view, but not necessarily the coordinates of the corresponding pixel on the Bitmap.
Can I get some sort of scale factor to know how much it was resized? Or if not, could someone suggest how I could go about getting the information I need? What's most important is that I can get the pixel coordinates - if I have to change the view type, that's alright.
Also, sometimes the bitmap is smaller than the screen, so it scales it up. In this scenario, it is possible that the x and y received from the MotionEvent are outside the bounds of the actual Bitmap.
Check into the ScaleType property. You'll have to do the math on the original image, based on the results of getScaleType(), but it's worth checking out.
I can across the same case. The thread is more than a year old, but thought it might be useful for someone. I had to scale the bitmap before setting to the ImageView.
Here is the code:
//Convert Uri to Bitmap
inputBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
//Scale the bitmap to fit in the ImageView
Bitmap bmpScaled = Bitmap.createScaledBitmap(inputBitmap, img.getWidth(), img.getHeight(),true);
//Set the scaled bitmap to the ImageView
img.setImageBitmap(bmpScaled);
After this, the touched coordinates are the same as the coordinates of the bitmap.

how do I change the size of a drawable that is on mapview in android?

Hi I have managed to create a drawable from a path as show here:
Drawable drawable = Drawable.createFromPath(path);
but the image that shows up on the map is very large because it is taken from the camera and stored in the SD card is there a way to make the image smaller when viewed on the map?
Is your purpose to just put an image on the Mapview. If that is the case, why don't you use the BitmapFactory to create a Bitmap from the FileInputStream which you open to your image file.
Then on the Bitmap created by BitmapFactory, you could use the createScaledBitmap method to scale the image and then put it on MapView.
EDIT: Create a BitmapDrawable from the scaled Bitmap.

Categories