2014-10-14 12 views
7

Làm cách nào để cắt ảnh. Bây giờ nó đang hiển thị hình ảnh cho cây trồng và sau khi chọn phần cắt trong khi chạm vào nút "Lưu". Hiển thị là "lưu hình ảnh". Sau đó không có gì xảy ra. Đây là mã của tôi.Cách cắt ảnh từ máy ảnh

Nút bấm:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 
intent.putExtra("crop", "true"); 
intent.putExtra("aspectX", 0); 
intent.putExtra("aspectY", 0); 
intent.putExtra("outputX", 200); 
intent.putExtra("outputY", 150); 
intent.putExtra("return-data", true); 
startActivityForResult(intent, CAMERA_PIC_REQUEST); 

onActivityResult:

Bundle extras = data.getExtras(); 
Bitmap bitmap = (Bitmap) extras.get("data"); 
if (bitmap != null) { 
    Img_View.setImageBitmap(bitmap); 
} 

Trả lời

21

Bạn có thể sử dụng mã này để thực hiện cắt xén:

..... 
final int CAMERA_CAPTURE = 1; 
final int CROP_PIC = 2; 
private Uri picUri; 
.... 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button captureBtn = (Button) findViewById(R.id.capture_btn); 
     captureBtn.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     if (v.getId() == R.id.capture_btn) { 
      try { 
       // use standard intent to capture an image 
       Intent captureIntent = new Intent(
         MediaStore.ACTION_IMAGE_CAPTURE); 
       // we will handle the returned data in onActivityResult 
       startActivityForResult(captureIntent, CAMERA_CAPTURE); 
      } catch (ActivityNotFoundException anfe) { 
       Toast toast = Toast.makeText(this, "This device doesn't support the crop action!", 
         Toast.LENGTH_SHORT); 
       toast.show(); 
      } 
     } 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == CAMERA_CAPTURE) { 
       // get the Uri for the captured image 
       picUri = data.getData(); 
       performCrop(); 
      } 
      // user is returning from cropping the image 
      else if (requestCode == CROP_PIC) { 
       // get the returned data 
       Bundle extras = data.getExtras(); 
       // get the cropped bitmap 
       Bitmap thePic = extras.getParcelable("data"); 
       ImageView picView = (ImageView) findViewById(R.id.picture); 
       picView.setImageBitmap(thePic); 
      } 
     } 
    } 

    /** 
    * this function does the crop operation. 
    */ 
    private void performCrop() { 
     // take care of exceptions 
     try { 
      // call the standard crop action intent (the user device may not 
      // support it) 
      Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
      // indicate image type and Uri 
      cropIntent.setDataAndType(picUri, "image/*"); 
      // set crop properties 
      cropIntent.putExtra("crop", "true"); 
      // indicate aspect of desired crop 
      cropIntent.putExtra("aspectX", 2); 
      cropIntent.putExtra("aspectY", 1); 
      // indicate output X and Y 
      cropIntent.putExtra("outputX", 256); 
      cropIntent.putExtra("outputY", 256); 
      // retrieve data on return 
      cropIntent.putExtra("return-data", true); 
      // start the activity - we handle returning in onActivityResult 
      startActivityForResult(cropIntent, CROP_PIC); 
     } 
     // respond to users whose devices do not support the crop action 
     catch (ActivityNotFoundException anfe) { 
      Toast toast = Toast 
        .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    } 

Bạn có thể sử dụng theo hướng dẫn đơn giản để thực hiện cắt xén:

  1. http://khurramitdeveloper.blogspot.in/2013/07/capture-or-select-from-gallery-and-crop.html
  2. http://www.londatiga.net/featured-articles/how-to-select-and-crop-image-on-android/
  3. http://www.coderzheaven.com/2012/12/15/crop-image-android/
  4. http://shaikhhamadali.blogspot.in/2013/09/capture-images-and-crop-images-using.html
+0

Mã này hoạt động, chỉ cần một câu hỏi tôi cần phải lưu đường dẫn hình ảnh, nhưng hình ảnh bị cắt cho phép điều này? và thay vì một bitmap để hiển thị hình ảnh có thể sử dụng uri mới? – RexDough

+2

Tôi nhận được nullPointerException trên dòng này. ** Bundle extras = data.getExtras(); **. phần bổ sung là null. Tôi đang sử dụng thiết bị Android M. –

+2

không hoạt động! vì data.getData() là null sau CAMERA_CAPTURE – Alex

0

Hãy thử điều này

   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

       mImageCaptureUri = Uri.fromFile(new File(Environment 
         .getExternalStorageDirectory(), "tmp_avatar_" 
         + String.valueOf(System.currentTimeMillis()) 
         + ".jpg")); 

       intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, 
         mImageCaptureUri); 

       intent.setData(mImageCaptureUri); 
       intent.putExtra("outputX", 200); 
       intent.putExtra("outputY", 200); 
       intent.putExtra("aspectX", 1); 
       intent.putExtra("aspectY", 1); 
       intent.putExtra("scale", true); 
       intent.putExtra("return-data", true); 
Các vấn đề liên quan