捕获和裁剪图像并保存图像

时间:2016-09-02 22:21:12

标签: android android-studio android-intent crop

我正在尝试捕获图像并保存裁剪的图像而不是具有指定名称的原始图像。 目前我能够在imageview上裁剪和显示,但我需要知道如何保存裁剪的图像而不是原始图像。这是代码。

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

我是Android的新手,我真的想知道如何做到这一点,任何形式的帮助都很受欢迎。

2 个答案:

答案 0 :(得分:0)

所以问题是:如何保存bitmap

有时保存位图需要很长时间。我建议你使用AsyncTask以避免在你的应用程序中产生恼人的滞后。

public class saveFile extends AsyncTask<Void, Void, File>{
        @Override
        protected void onPreExecute() {

            super.onPreExecute();

            progressBar.setVisibility(View.VISIBLE);//Show user that app is working in backgrind

        }


        @Override
        protected File doInBackground(Void... params) {

            String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
                    "/CropedImage";

            File dir = new File(file_path);

            if(!dir.exists())

            dir.mkdirs();

            String format = new SimpleDateFormat("yyyyMMddHHmmss",
            java.util.Locale.getDefault()).format(new Date());

            File file = new File(dir, format + ".png");

            FileOutputStream fOut;

            try {

            fOut = new FileOutputStream(file);

            thePic.compress(Bitmap.CompressFormat.PNG, 100, fOut);

            fOut.flush();

            fOut.close();

            } 

            catch (Exception e) {

            e.printStackTrace();

            }


                return file;
        }

         @Override
            protected void onPostExecute(File result) {

            progressBar.setVisibility(View.INVISIBLE);

            String respath=result.getPath().toString();
            Toast.makeText(MainActivity.this, "Saved at "+respath, Toast.LENGTH_LONG).show();

            MediaScannerConnection.scanFile(MainActivity.this, new String[] { result.getPath() }, new String[] { "image/jpeg" }, null);     





          }
    }

然后调用AsyncTask:

 new saveFile().execute();

答案 1 :(得分:0)

Android没有任何裁剪意图,您可以下载裁剪库Click here