web-dev-qa-db-fra.com

Comment recadrer des images de la caméra

Comment puis-je recadrer les images de la caméra. Il affiche maintenant l'image à recadrer et après avoir sélectionné la section de recadrage tout en tapotant sur le bouton " Enregistrer ". Son affichage comme " save image ". Après que rien ne se passe. Voici mon code.

Clic bouton:

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);
}
7
user4066103

Vous pouvez utiliser ce code pour effectuer un recadrage:

.....
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();
        }
    }

Vous pouvez utiliser le tutoriel suivant pour effectuer un recadrage:

  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
23
Himanshu Agarwal

Économisez beaucoup de temps et utilisez cette bibliothèque J'essayais d'essayer de le faire moi-même. Je suis tombé sur cette bibliothèque. Son utilisation est vraiment simple et vous obtenez une vue de rognage d'image d'aspect professionnel qui vous permet de choisir un appareil photo ou galerie de photos.

Exemple simple:

Inclure la bibliothèque dans votre diplôme

implementation 'com.theartofdev.edmodo:Android-image-cropper:2.7.+'

Ajouter des autorisations pour manifester

<uses-permission Android:name="Android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission Android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>

//add this under <application>
<activity Android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
  Android:theme="@style/Base.Theme.AppCompat"/>

Dans votre activité

//on button press or anywhere, this starts the image picking and cropping process
CropImage.activity()
  .setGuidelines(CropImageView.Guidelines.ON)
  .start(this);

//in your activity where you will get the result of your cropped image
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
    CropImage.ActivityResult result = CropImage.getActivityResult(data);
    if (resultCode == RESULT_OK) {
      Uri resultUri = result.getUri();

      //From here you can load the image however you need to, I recommend using the Glide library

    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
      Exception error = result.getError();
    }
  }
}

Je ne suis pas affilié à ce logiciel

2
Fonix