main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageuri" android:layout_marginTop="15dp" android:adjustViewBounds="true" android:layout_gravity="center" android:maxWidth="250dip" android:maxHeight="250dip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btncropimg" android:layout_gravity="center" android:layout_marginTop="15dp" android:layout_height="50dip" android:text="Crop Image" android:layout_width="150dip"/> </LinearLayout>
TestCropActivity.java:
public class TestCropActivity extends Activity { /** Called when the activity is first created. */ private File tempFile; public static final int NONE = 0; ImageView imageView = null; Button buttoncrop = null; Uri imgUri; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView=(ImageView)findViewById(R.id.imageuri); buttoncrop=(Button)findViewById(R.id.btncropimg); //image URI from SDCARD tempFile = new File(Environment.getExternalStorageDirectory()+"/Images/a07.jpg"); imgUri=Uri.fromFile(tempFile); imageView.setImageURI(imgUri); buttoncrop.setOnClickListener(btncroplistener); } private OnClickListener btncroplistener=new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startCrop(imgUri); } }; public void startCrop(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); //intent.setClassName("com.android.camera", "com.android.camera.CropImage"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); // aspectX aspectY width and height ratio intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY Crop picture width and height intent.putExtra("outputX", 96); intent.putExtra("outputY", 96); //output file Uri where Cropped image save intent.putExtra("output", Uri.fromFile(tempFile)); intent.putExtra("outputFormat", "JPEG"); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ if(requestCode==NONE) return; if(data==null) return; if(requestCode==1){ Bundle extras=data.getExtras(); if(extras !=null){ //get cropped image here from data as Bitmap Bitmap photo=extras.getParcelable("data"); ByteArrayOutputStream stream=new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream); imageView.setImageBitmap(photo); } } super.onActivityResult(requestCode, resultCode, data); } }
Download Source Code: CropImage
1 comments:
Really great work..........Solved an extremely confusing problem of mine.......Keep progressing
Post a Comment