2016-02-24 32 views

回答

0

試試這個庫是我在這兩種情況下很好地工作 - https://github.com/lvillani/android-cropimage

更新

這不是完美的代碼b'z我甚至有項目之間,所以我沒有時間讓它清楚,但你可以修改並讓我知道。 很高興在這裏發表您的評論。

private static final int REQUEST_CAMERA = 0; 
private static final int CAMERA_REQUEST = 1; 
private static final int RESULT_LOAD_IMAGE = 2; 
private static final int GALLERY_KITKAT_INTENT_CALLED = 3; 
private static final int CROP_IMAGE_GALLERY = 4; 
private static final int CROP_IMAGE_CAMERA = 5; 

private String FilePath; 
private Uri ImageUri, croppedImageUri; 
private File pic_file; 


private void SelectImage() 
{ 
    final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Remove", "Cancel"}; 
    AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this); 
    builder.setTitle("Upload Photo"); 
    builder.setItems(options, new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int item) { 

      if (options[item].equals("Take Photo")) { 
       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       pic_file = new File(Environment.getExternalStorageDirectory(), 
         "tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); 
       ImageUri = Uri.fromFile(pic_file); 
       cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, 
         ImageUri); 

       cameraIntent.putExtra("return-data", true); 

       startActivityForResult(cameraIntent, CAMERA_REQUEST); 
      } else if (options[item].equals("Choose from Gallery")) { 

       Intent intent = new Intent(Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(Intent.createChooser(intent, "Select Picture"), RESULT_LOAD_IMAGE); 


      } else if (options[item].equals("Remove")) { 
       ivProfilePic.setImageResource(R.mipmap.ic_profile_pic); 
       FilePath = ""; 
       dialog.dismiss(); 
      } else if (options[item].equals("Cancel")) { 
       dialog.dismiss(); 
      } 
     } 
    }); 
    builder.show(); 
} 


/* 
* After capture Image or Select Image from Gallary On activityResult Call 
* 
*/ 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 


    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) 
    { 
     try { 

      CropImage(); 

     } catch (Exception e) { 
      rootLogger.error(Utility.createLogableFormat(Utility.getStringStackTrace(e))); 
     } 


    } 
    else if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){ 

     try { 
      ImageUri = data.getData(); 

      CropImage(); 
     } catch (Exception e) { 
      rootLogger.error(Utility.createLogableFormat(Utility.getStringStackTrace(e))); 
     } 

    } 
    else if (requestCode == CROP_IMAGE_CAMERA) { 
      if (data != null) { 
       // get the returned data 
       Bundle extras = data.getExtras(); 

       if (extras != null) { 
        FilePath = ImageUri.getPath(); 

        File f = new File(FilePath); 

        Bitmap bmp_post_news = decodeSampledBitmapFromResource(f.getAbsolutePath(),120,120); 

        ivProfilePic.setImageBitmap(bmp_post_news); 

       } 
      } 
     } 
     else if (requestCode == CROP_IMAGE_GALLERY) { 
      if (data != null) { 
       // get the returned data 
       Bundle extras = data.getExtras(); 

       if (extras != null) { 

        FilePath = croppedImageUri.getPath(); 

        Bitmap bmp_post_news = decodeSampledBitmapFromResource(FilePath,120,120); 

        ivProfilePic.setImageBitmap(bmp_post_news); 


       } 

      } 
     } 
} 

public Bitmap decodeSampledBitmapFromResource(String Filepath, 
                int reqWidth, int reqHeight) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(Filepath, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeFile(Filepath, options); 
    } 

public int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    int inSampleSize = 1; 
    final int height = options.outHeight; 
    final int width = options.outWidth; 


    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

protected void CropImage() { 

    CropImageIntentBuilder cropImage = new CropImageIntentBuilder(3, 4,120,120, ImageUri); 
    //cropImage.setOutlineColor(0xFF03A9F4); 
    cropImage.setSourceImage(ImageUri); 
    cropImage.setCircleCrop(true); 
    cropImage.setOutlineCircleColor(Color.WHITE); 
    cropImage.setOutputQuality(100); 

    Intent intent = cropImage.getIntent(this); 
    //intent.putExtra("return-data", false); 

    startActivityForResult(intent, CROP_IMAGE_CAMERA); 
} 
+0

https://github.com/jdamcd/android-crop不能正常工作 –

+0

你能告訴我圖像裁剪的完整說明代碼 –

+0

@KalpeshKumawat檢查我的更新答案,並接受答案,如果有幫助。 – androidnoobdev

相關問題