从相机捕获图像,并在android中修复裁剪

时间:2016-08-04 07:26:38

标签: android

我正在尝试裁剪图像,就像我们在Facebook上做的那样。我在我的应用程序中使用了这个链接:https://github.com/oginotihiro/cropview这对我来说很好。但是当我点击一个按钮时,它会直接进入图库并裁剪所选图像。而不是我要打开相机我的设备,然后点击图片,我想做裁剪。我怎么能这样做?有人可以帮助我。

我已尝试过此代码。但我无法实现。

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

2 个答案:

答案 0 :(得分:0)

Step1:开始打开图库

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

Step2:从图库中获取图片:

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

根据您的情况,您可以使用onActivityResult方法进行裁剪。 它会对你有所帮助。

答案 1 :(得分:0)

带相机: 第1步:打开相机

/*
 * 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);
}

第二步:从相机中获取图像。

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

第3步:添加权限

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