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

Posted

技术标签:

【中文标题】捕获和裁剪图像并保存裁剪的图像【英文标题】:Capture and Crop Image and save croped Image 【发布时间】:2016-09-02 22:21:11 【问题描述】:

我正在尝试捕获图像并保存裁剪后的图像,而不是具有指定名称的原始图像。 目前我可以裁剪并在 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 新手,我真的很想知道如何做到这一点,任何形式的帮助都非常受欢迎。

【问题讨论】:

Android does not have a CROP Intent。有很多image cropping libraries available for Android。请使用一个。 【参考方案1】:

所以问题是:如何保存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();

【讨论】:

我需要在哪里使用 AsyncTask?我的意思是我必须创建新课程? 不不,只需将其添加到您的 MainActivity.class 中 请用我的代码详细说明您建议的解决方案。我不明白如何遵循您建议的代码。我正在获取 uri 并将该 uri 读入位图。【参考方案2】:

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

【讨论】:

有些确实支持这个意图。

以上是关于捕获和裁剪图像并保存裁剪的图像的主要内容,如果未能解决你的问题,请参考以下文章

如何使用Swift选择图像的一部分,裁剪并保存?

从相机捕获图像并裁剪不工作

如何裁剪图像并保存?

Android自动裁剪相机捕获的图像

裁剪图像并保存到 sdcard (Android) 中的特定文件夹?

在 imagebox 控件中裁剪图像并保存