Android: Crop an Image after Taking it With Camera with a Fixed Aspect Ratio

后端 未结 4 857
悲&欢浪女
悲&欢浪女 2020-11-27 12:34

I\'m trying to crop an image after taking it, and my code is as follows:

   private void doTakePhotoAction() {

        Intent intent = new Intent(MediaStore         


        
4条回答
  •  温柔的废话
    2020-11-27 12:38

    Though this might be a very old thread, I was able to crop a picture programmatically with the following code :

            btnTakePicture.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    

    then I cropped it with :

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
    
            photo = (Bitmap) data.getExtras().get("data");
    
            performcrop();
        }
    
    }
    
    private void performcrop() {
        DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;
    
        Bitmap croppedBmp = Bitmap.createBitmap(photo, 0, 0, width / 2,
                photo.getHeight());
    
        imageTaken.setImageBitmap(croppedBmp);
    }
    

    imageTaken is an ImageView Component in my view. You can see my source Here

提交回复
热议问题