Hi so I need a user to be able to upload a profile picture that I have to re-size and put in their profile. The "area" for the profile picture is a 100x100 square since its on an android device. Lets assum someone posted a 300 x 600 or 600 x 300 how would I convert / crop an image to fit a 100 x 100 square and look good as a profile image? I feel like this is more of a conventional question.
I do know how to resize and crop bitmaps just can't figure out how would I go about it.
for instance
android.graphics.Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
Well if you are not against wasting some of the space, I recommend just doing basic scaling. Divide the larger size by 100 gives the scale factor. Then divide by that on each dimension and you have a picture with the right aspect ratio that fits your area.
Related
I am trying to work on a small requirement which provides users with an option to resize an image to a given percentage. Lets say 75% is the option. Does that mean I should resize the image size to 75% or the resolution of the image to 75%?
Any thoughts on this?
I wanted to use
Bitmap yourBitmap; resized =
Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.75),
(int)(yourBitmap.getHeight()*0.75), true);
For the newWidth and newHeight should I blindly pass calculate like above?
I think
image resolution is 100% like 100x100 or 200x200 or 1000x1000
user want to resize it to 75%
I think scaleX, scaleY
so on the end image must be 75% like 75x75 or 150x150 or 750x750
Within the help of Bitmap.createScaledBitmap you can resize your image even without losing your image quality
Bitmap scaledBitmap = Bitmap.createScaledBitmap(unscaledBitmap, wantedWidth, wantedHeight, true);
I hope it will work for you
You can implement as following way.
/* sample code */
Bitmap srcBitmap;
Bitmap destBitmap = Bitmap.createScaledBitmap(srcBitmap, (int)(srcBitmap.getWidth()*0.75), (int)(srcBitmap.getHeight()*0.75), true);
Hopefully...
What you consider to be 75% of an image is up to you. There are several posibilities. A nice variant is to resize to a predetermined size like 1024 for width if landscape and for height if portrait.
All the here suggested techniques can often not be used as they construct a full blown original bitmap first. Often memory for that will not be available.
So scale it down while loading. BitmapFactory will do it for you.
I'm in the works of a game and I'm loading a large image that is 5120 wide and 116 high. When I try to draw it to the screen using
canvas.drawBitmap(land, 0, 0, paint);
nothing gets drawn. However when I use a smaller file (512x116) with the same method call, it gets drawn. Is there a image size limit? My image is 750 KB. Also the max canvas width and height is 8192. Thanks in advance!
EDIT:
I noticed this in the log:
Bitmap too large to be uploaded into a texture (20480x464, max=8192x8192)
as if my bitmap is 4 times larger than its actual size. Why would that be?
EDIT 2: Found the issue. Bitmapfactory is returning a larger file because is multiplies the dimensions by my screen density. So the file ends up being too large.
I haven't used Canvas layout but try using largeHeap = "true" in your Manifest
I would like to know how can I crop , scale and rotate a ImageView that have a scaled background image using a fixed width and height on it's parent.
To crop (get subImage) see the following thread:
How to Cut a ImageView to a subimage in the range of four specified points
For scale and rotate use Matrix with setScale and setRotate.
This forum has a number of threads discussing relevant topic, e.g.
Android Scale a bitmap image
Android - Rotate image around center point?
Before publishing a question you better look around :)
I am trying to develop a custom image display application for android. So far I am able to load a bitmap and display it on the screen. I want to center the users view on the center of the image. To do this, I have been using
Bitmap bmp = BitmapFactory.decodeResource(...)
float offsetX = (androidScreenHeight - bitmapFactoryOptions.outHeight) / 2
canvas.drawBitmap(bmp, offsetX, 0, myPaint)
to render it. androidScreenHeight is correct as far as I can tell. I am using a samsung note which has 1280x800 screen which is the value I am getting for that. My image is 1920 pixels wide, so the offset on each side should be 560, with 800 in the middle for the actual screen. See this picture:
!http://imgur.com/a2DGmjG
The value of offsetX is correct at 560. So I know at least that part is working correctly. But instead of the above, what I am getting is this:
!http://imgur.com/jtHw926
(these are not the actual image)
I am not sure what is going on. Are pixels treated differently somehow on my android device than on my computer? I understand that each pixel will take up a different size on each since the dpi is different. but an offset of 560 pixels should give the same offset on each screen, regardless of the size of the individual pixels. Any ideas what is going on here?
(Promoting my comment to proper answer)
BitmapFactory will perform scaling between that folder's dpi (assuming you have one) and the device dpi. Putting the bitmap in 'drawable-nodpi' will disable the autoscaling but be careful that you really want to do this (cos autoscaling is usually useful and desirable).
I have a Canvas that i draw text on.
(this is quite advanced i think hope you can follow me)
See attached image below.
The functionality is that I can tap on the screen to change the textsize.
Tap left screen = smaller Font.
Tap right Screen = bigger Font.
I can then also move the text on the screen.
When textsize is ok and i have moved the text where i want it,
Then I want to save it back to the original Bitmap.
I use options.inSampleSize = 4; to initially load the Bitmap
The ImageView that have the Bitmap is of course smaller then the original Image.
Some kind of calculation is needed.
This tends to be quite difficult to do.
I have the options.inSampleSize = 4 Bitmaps Ratio.
It's 0.59, 0.69 something depending on Landscape or portrait.
Im playing around with that to somehow change the new BitmapsetTextSize
to look the same as the ImageView smaller Bitmap.
What could i do here?
I have a feeling that since one never know what size an image have.
I have to somehow scale/constrain the Loaded Bitmap Ratio to a fixed Ratio.
Then i need to using percentage or something to transfer the text location
to the bigger image.
I can kind of do that when it comes to initial
(red small ball on picture) location. Hence, the starting point of the text.
But i dont know how long the text is so im stuck so so speak and asking for advice
One way i tried was to divide paint.getTextSize() with the Ratio something like 0.59. That looked like a solution at first. But the image ratio is not fixed and the Font size is not fixed something else is needed.
Here are two pictures showing the problem.
On phone Bitmap:
The saved new Bitmap:
I'm not 100% clear that I understand what you mean, but here's a go. It sounds like you were close to the right approach. Instead of using a fixed ratio, you need to calculate the ratio that the image is scaled by to fit in the view on the phone, then you can scale the text by the inverse ratio. So in steps:
Measure the width of the original image (height would do just as well, but we just need one dimension)
Measure the width of the scaled image
Calculate ratio (ratio = original / scaled)
Let the user type their text
You can then get the text size using something like: float paintSize = paint.getTextSize();
For rendering on the final image, use paint.setTextSize(paintSize / ratio);.