Cropping an image in ANDROID programmatically has always been a problem for ANDROID developers.
So Here I am putting a sample code to demonstrate this.
For cropping an image in ANDROID we need a source bitmap which is our image itself, the other one is the target bitmap which we have to
set the size as we want and a matrix.
Below code is a fully working code, just copy and paste this code to your new project.
The XML for the code is also provided below.
Make sure that you have an image named “image.png” in your resources.
Assuming the image to be of size 300×300 pixels.
package Move3.pack; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.AbsoluteLayout; import android.widget.FrameLayout; @SuppressWarnings({ "deprecation", "unused" }) public class Move extends Activity { public FrameLayout board; public View part1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); board = new FrameLayout(this); board = (FrameLayout)findViewById(R.id.Board); part1 = new View(this); part1 = findViewById(R.id.part1); try{ Paint paint = new Paint(); paint.setFilterBitmap(true); Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.image); int targetWidth = 300; int targetHeight = 300; Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888); RectF rectf = new RectF(0, 0, 100, 100); Canvas canvas = new Canvas(targetBitmap); Path path = new Path(); path.addRect(rectf, Path.Direction.CW); canvas.clipPath(path); canvas.drawBitmap( bitmapOrg, new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg.getHeight()), new Rect(0, 0, targetWidth, targetHeight), paint); Matrix matrix = new Matrix(); matrix.postScale(1f, 1f); Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, 100, 100, matrix, true); /*convert Bitmap to resource */ BitmapDrawable bd = new BitmapDrawable(resizedBitmap); part1.setBackgroundDrawable(bd); } catch(Exception e){ System.out.println("Error1 : " + e.getMessage() + e.toString()); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/Board" android:layout_gravity="top"> <View android:layout_height="100dp" android:layout_width="100dp" android:id="@+id/part1" > </FrameLayout>
If you got any errors please let me know.
error occur….
null java.lang.nullpointerexception…..
pls help me
OK… SOLVE IT.. THX
thax…….. error.. solve……….. by mistake. i am taking another xml file for view framelayout……. thax agmain
Pingback: Checking a camera or image for pixel size : Android Community - For Application Development
Really Nice!, Really happy i came accross this.
Hi there, I enjoy reading through your article post. I like to
write a little comment to support you.
I have error on following line
<?xml version="1.0" encoding=”utf-8″?>
Try to type the quotes once again.
i want to start the cropping from 200 down with a height of 100. returned bitmap is blank.
i only modify RectF() to RectF(0 , 200 , bitmapOrg.width , 100).
can somebody please help me out
Pingback: 从Android中的图库中选择时裁剪图像 Android Cookie
Pingback: Taglia un'image quando selezionata dalla galleria in android IL ANDORID
Pingback: Crop an image when selected from gallery in android Android Babe
Pingback: Cortair uma image quando selecionada da galeria no Android Android Cake
Pingback: 在Android中从图库中select图像时裁剪图像 Android Cookie
Pingback: Ritaglia un’immagine quando selezionata dalla galleria in Android Sviluppo Android
Pingback: android - Recorte de una imagen cuando se selecciona desde la galerĂa en android
Pingback: Crop an image when selected from gallery in android