从相机或画廊获取图像时裁剪图像

时间:2014-11-12 04:41:33

标签: android camera

如何在从android中的相机或图库中获取图像时裁剪图像?

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);

intent.putExtra("crop", "true");
intent.putExtra("aspectX", 500);
intent.putExtra("aspectY", 750);
intent.putExtra("scale", true);
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 487);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);

我使用过这段代码,但是在micromax中,它不起作用,任何人都有任何想法吗?

3 个答案:

答案 0 :(得分:0)

link帮助我获得了我想要的东西......

你只需将此代码放入onCreate。

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
    mFileTemp = new File(Environment.getExternalStorageDirectory(), UtilityEventApp.TEMP_PHOTO_FILE_NAME);
}
else {
    mFileTemp = new File(getFilesDir(), UtilityEventApp.TEMP_PHOTO_FILE_NAME);
}

答案 1 :(得分:0)

您可以从此处使用Cropper库: - https://github.com/edmodo/cropper

并使用此代码从图库或相机获取图像,然后从和。仍有任何问题要求我。

`public class PhotoUpload扩展了ActionBarActivity {

ImageView gallery, camera, rotate, crop, uploadphoto, croppedImageView,
        noimage;
  InputStream inputStream;

static Cursor c;
CropImageView cropImageView;
TableRow photochooserrow, editorrow, uploadrow;
private static final int ROTATE_NINETY_DEGREES = 90;
Bitmap croppedImage;
BitmapFactory.Options options;
ActionBar act;
ProgressDialog pd;
Boolean isInternetPresent = false;
ConnectionDetector cd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photoupload);
    act=getSupportActionBar();
    act.setTitle("Upload Profile Photo");


    //connection checking
    cd = new ConnectionDetector(getApplicationContext());
    isInternetPresent = cd.isConnectingToInternet();
    System.out.println("Network states:" + isInternetPresent);


    cropImageView = (CropImageView) findViewById(R.id.CropImageView);




    noimage = (ImageView) findViewById(R.id.noimage);
    gallery = (ImageView) findViewById(R.id.gallery);
    camera = (ImageView) findViewById(R.id.camera);
    rotate = (ImageView) findViewById(R.id.rotate);
    crop = (ImageView) findViewById(R.id.crop);
    uploadphoto = (ImageView) findViewById(R.id.uploadphoto);
    croppedImageView = (ImageView) findViewById(R.id.cropedphoto);

    // table rows
    photochooserrow = (TableRow) findViewById(R.id.photochooserrow);
    editorrow = (TableRow) findViewById(R.id.editorrow);
    uploadrow = (TableRow) findViewById(R.id.uploadrow);

    // gallery to choose photo
    gallery.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent ig = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(ig, 1);

        }
    });

    // camera button to choose photo
    camera.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent ic = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(ic, 2);

        }
    });

    // rotate button
    rotate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            cropImageView.rotateImage(ROTATE_NINETY_DEGREES);
        }
    });

    // cropped button and set on a hidden image view
    crop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            options=new BitmapFactory.Options();
            options.inSampleSize=8;


            croppedImage = cropImageView.getCroppedImage();

            croppedImageView.setImageBitmap(croppedImage);

            editorrow.setVisibility(View.GONE);
            uploadrow.setVisibility(View.VISIBLE);

            LinearLayout hideit=(LinearLayout)findViewById(R.id.hideit);
            hideit.setVisibility(View.GONE);
            croppedImageView.setVisibility(View.VISIBLE);





        }
    });



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.actionbarmenu, menu);

       return true; 


}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.skipp:

    if (IGEFSharedPrefrence.getCOVERPHOTO().equals("null")) {
        System.out.println("no COver Photo Set --> moving to CoverPhotoChooser activity");

    Intent intt=new Intent(PhotoUpload.this, CoverPhotoChooser.class);
        startActivity(intt);
        finish();

}

    else{
        Intent i = new Intent(PhotoUpload.this, UserScreen.class);
        startActivity(i);
        finish();
    }

break;

default:
    return super.onOptionsItemSelected(item);
}

return false;


}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
        Uri image = data.getData();
        String[] loc = { MediaStore.Images.Media.DATA };
        c = getContentResolver().query(image, loc, null, null, null);
        c.moveToFirst();
        int index = c.getColumnIndex(loc[0]);
        String picturePath = c.getString(index);
        c.close();
        cropImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        photochooserrow.setVisibility(View.GONE);

        editorrow.setVisibility(View.VISIBLE);
        noimage.setVisibility(View.GONE);
        cropImageView.setVisibility(View.VISIBLE);

    } else if (requestCode == 2 && resultCode == RESULT_OK && null != data) {
        Bundle b = data.getExtras();
        Bitmap bmp = (Bitmap) b.get("data");

        cropImageView.setImageBitmap(bmp);
        photochooserrow.setVisibility(View.GONE);
        editorrow.setVisibility(View.VISIBLE);
        noimage.setVisibility(View.GONE);
        cropImageView.setVisibility(View.VISIBLE);


    }
}

答案 2 :(得分:0)

试试这个answer

int h = 48; // height in pixels
int w = 48; // width in pixels    
Bitmap scaled = Bitmap.createScaledBitmap(largeBitmap, h, w, true);