set image size to full screen in android - 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.

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 fix scale of image

I want to have a ImageView of fixed x / y ratio on all of the screens.
for example famous 16/9 ratio.
And I also want that The ImageView be as large as possible.
how could I do it in android?
Thanks a lot.
The above answer will alter the aspect ratio.
You cannot achieve your goal by only modifying the xml layout file. You have to do this in Java code.
The basic steps are:
read your image file into a Bitmap, and obtain the initial width and height and aspect ratio of the Bitmap.
calculate the scaling factors for x and y dimension respectively, and use the smaller one of the two factors to scale your image
factorX= ScreenWitdhInPixel/ImgWidth
factorY= ScreenHeightInPixel/ImgHeight
factor= (factorX<factorY?factorX:factorY)
scale your image up using the factor value calculated in step 2
Matrix matrix = new Matrix();
matrix.postScale(factor, factor);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
Please refer to this post for a full example: ImageView fit without stretching the image
You must set android:scaleType="fitXY" to your imageView.
And if you want your ImageView be as large as its parent, you can write this lines of codes:
android:layout_width="match_parent"
android:layout_height="match_parent"

Poor Image Quality on scaling image in android

I am working on android application development...when i click on thumbnail image, it enlarges but give extreme poor quality...please help me how to resolve it
ImageView ivPreview = (ImageView)nagDialog.findViewById(R.id.image1);
int h =200 ; // height in pixels
int w = 200; // width in pixels
Bitmap Scaled = Bitmap.createScaledBitmap(bm, h, w, true);
ivPreview.setImageBitmap(Scaled);
If I understand correctly you want to enlarge a thumbnail. This is a bad idea because a thumbnails' size is a small image and enhancing it only makes it look worse. The Bitmap.createScaledBitmap() method stretches the bitmap resulting in quality loss. You should use a bigger original image, create the thubmnail from that image and load the original image when you click on it. Here's some code to get you going:
Bitmap original = //get original image
Bitmap thumbnail = Bitmap.createScaledBitmap(original, 50, 50, true);
//when the user clicks on the imageview:
imgView.setImageBitmap(original);

How to properly set a wallpaper on Android

It seems that setting a wallpaper on Android just doesn't work in any useful way.
If you get an image from your phone and set it as the wallpaper, it's way too big for the screen
If you resize it (either using a createBitmap() function that allows you to specify size, or the ridiculously useless createScaledBitmap()) it goes all jaggy and out of focus
If you use some freaky hacks to fix the quality of the image, it's better, but still not perfectly clear by any stretch.
If you attempt to get the current wallpaper and set that, it still seems to make it too big. It appears to give you the original image file, forcing you to resize it, which doesn't work.
Now, the phone's internal software is perfectly capable of resizing an image to be smaller with no reduction of quality. Why does it not share this functionality?
WallpaperManager wallpaperManager = WallpaperManager.getInstance(Spinnerz.this);
// gets the image from file, inexplicably upside down.
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "DCIM/Camera/Zedz.jpg");
// use some rotation to get it on an angle that matches the screen.
Matrix bitmapTransforms = new Matrix();
bitmapTransforms.setRotate(90); // flip back upright
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),bitmapTransforms, false); // create bitmap from bitmap from file, using the rotate matrix
// lets set it exactly to the resolution of my Samsung Galaxy S2: 480x800 pixels.
// This function appears to exist specifically to scale a bitmap object - should do a good job of it!
bitmap = Bitmap.createScaledBitmap(bitmap, 480, 800, false);
wallpaperManager.setBitmap(bitmap);
// result is quite a jaggy image - not suitable as a wallpaper.
PIC:
Try this ,
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();

Crop particular part of image in android

I want to crop Red part from following image, Is there any simple method available in android that can crop following image.
I have found many SO questions but all are suggesting to used following code:
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100);
This code work well if width & height are around 2MP resolution, but if that cropped part is more than 3MP resolution than application got crashed with OOM error.
Is there any way that handle image more than 3MP during cropping?
You can used following code that can solve your problem.
Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);
Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.
For more detail you can refer this blog
1- Change your imageview for bitmap
final Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img);
2-use your bitmap to crop what you want
Bitmap croppedBmp = Bitmap.createBitmap(bitmap, x, y , width , height);
3-Take care x,y from Top and left
4- to preview your bitmap again in your imageview
imageView.setImageBitmap(croppedBmp);
If you want to crop image in any shape OR only selected part then
you can use ready made open source library

Categories

Resources