简体   繁体   中英

capture image from camera and do fixed cropping in android

I am trying to crop the image as we are doing in facebook. I have used this link in my app: https://github.com/oginotihiro/cropview This is working fine for me.But here when I click on a button, its directly going to gallery and cropping the selected image.Instead of that I want to open camera in my device and click the image and I want to do cropping.How can I do this?Can someone help me.

I have tried this code.But I am not able to implement.

 imageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reset();
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, REQUEST_PICK);

            }
        });

    doneBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final ProgressDialog dialog = ProgressDialog.show(StudentDetails.this, null, "Please wait…", true, false);
                cropView.setVisibility(View.GONE);
                layout4.setVisibility(View.GONE);
                resultIv.setVisibility(View.VISIBLE);
                layout3.setVisibility(View.GONE);
                layoutUpload.setVisibility(View.VISIBLE);
                //       editTextName.setVisibility(View.GONE);
                buttonUpload.setVisibility(View.VISIBLE);

                new Thread() {
                    public void run() {
                        croppedBitmap = cropView.getOutput();

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // cropped image set
                                resultIv.setImageBitmap(croppedBitmap);
                            }
                        });

                        Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
                        CropUtil.saveOutput(StudentDetails.this, destination, croppedBitmap, 1);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialog.dismiss();
                            }
                        });
                    }
                }.start();
            }
        });

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

        if (resultCode == RESULT_OK && requestCode == REQUEST_PICK) {
            filePath = data.getData();        
            cropView.setVisibility(View.VISIBLE);
            layoutImage.setVisibility(View.VISIBLE);
            layout3.setVisibility(View.GONE);
            layout4.setVisibility(View.VISIBLE);
            textNameVal.setVisibility(View.GONE);
            text1.setVisibility(View.GONE);
            buttonUpload.setVisibility(View.GONE);

            int x=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
            int y=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());         
            cropView.of(filePath).withAspect(x,y).initialize(StudentDetails.this);
        }
        }

Step1: Start to open gallery

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

Step2: Get Image from gallery:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

With your case, you can crop at the onActivityResult method. It will help you.

With camera: Step1: Open camera

/*
 * Capturing Camera Image will lauch camera app requrest image capture
 */
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

Step2: Get image from camera.

/**
 * Receiving activity result method will be called after closing the camera
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // successfully captured the image
            // display it in image view
            previewCapturedImage();
        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled Image capture
            Toast.makeText(getApplicationContext(),
                    "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

Step3: Add permission

    <!-- Accessing camera hardware -->
    <uses-feature android:name="android.hardware.camera" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM