Crop particular part of image in android - 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

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.

Android get rotated bitmap. (Image straightening)

I am trying to rotate a bitmap and crop out a valid Rectangle out of it, and then save it on the disk. What i am trying to do is explained in the following link:
http://i.stack.imgur.com/IIhBw.png
Black is the original image. Red is rotated image (by 15 degrees here) and green is the valid portion of the red image.
This feature is similar to the Image straightening which Instagram app does.
So far, I have tried this:
public static Bitmap getRotatedBitmap2(Bitmap bmp, int rotation){
Matrix matrix = new Matrix();
matrix.preRotate(rotation);
Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 0,
bmp.getWidth(), bmp.getHeight(),
matrix, false);
return bitmap;
}
This rotates the bitmap but it shrinks in size, also the rotated bitmap is enclosed is an black rectangle. i need to get rid of the out-bounding black rectangle. Also, How can I obtain the valid portion of bitmap?
Aviary and ImageMagick are libraries that can help to achieve the results asked in the question, as well perform other Bitmap operations and Image Manipulations.
I know you already solved this question, but if somebody is looking for another solution, you can check this library image-rotator that solves problem like this or very similar.

how to crop top,left,right and bottom portion of imageview in android

In my application i am getting transparent pixels in my Imageview while taking screenshot.How to cut top,left,right and bottom portion of Imageview.I tried below code but not working
Bitmap bitmap = Bitmap.createBitmap(imgView.getWidth(),imgView.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
imgView.draw(canvas);
Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100);
bitmap.recycle();
please help me to solve this issue.
I am not very sure what are you referring with "not working", but from first sight I would suggest you to change from
Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100);
to
Bitmap result =Bitmap.createBitmap(bitmap, 10, 50, bitmap.getWidth()-20, bitmap.getHeight()-100);
But I would not recommend you to use this kind of approach which explicitly setting width and height value.

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