簡體   English   中英

在Android中裁剪后保存圖像

[英]save image after cropping in android

我正在嘗試保存裁剪的圖像,並且已成功保存,但是問題是保存的圖像太小。 我想獲取裁剪圖像的原始尺寸。 這是我保存裁剪圖像的代碼。

Bundle extras = data.getExtras();
if (extras != null) {
 Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bytes);
File f = new File(fileUri.getPath());
 try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}

誰能幫我獲得高質量的裁剪圖像。 任何幫助將非常感激。 謝謝 :)

這全都取決於原始圖像的尺寸和裁剪圖像的尺寸。

一種裁剪方式是:

               Intent photoPickerIntent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                photoPickerIntent.setType("image/*");

                photoPickerIntent.putExtra("scaleType", "centerCrop");
                photoPickerIntent.putExtra("crop", "true");
                photoPickerIntent.putExtra("aspectX", 1);
                photoPickerIntent.putExtra("aspectY", 1);

                //....snip....
                photoPickerIntent.putExtra("outputX", 400);
                photoPickerIntent.putExtra("outputY", 400);
                //....snip....

                photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getBackgroundUri());
                photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
                startActivityForResult(photoPickerIntent, ActivityCrop);

有了它,您將始終獲得大小為400px(輸出= 400)的正方形圖像(長寬比= 1)。 因此,如果您選擇圖像的800像素見方,則會縮小圖像,從而降低質量。 相反,選擇一個50px的正方形也會使您得到400px的結果。

現在,如果您刪除了尺寸限制(在虛線的右邊)。 您將獲得最初選擇的區域(示例為800和50),並獲得最佳質量。

使用長寬比和大小發送裁剪意向

Intent cropIntent = new Intent("com.android.camera.action.CROP");

            cropIntent.setDataAndType(picUri, "image/*");

            cropIntent.putExtra("crop", "true");

            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);

            cropIntent.putExtra("outputX", 256);
            cropIntent.putExtra("outputY", 256);

            cropIntent.putExtra("return-data", true);

            startActivityForResult(cropIntent, PIC_CROP);

活動結果

if (requestCode == PIC_CROP) {

            Bundle extras = data.getExtras();
            Bitmap thePic = extras.getParcelable("data");

            BitmapDrawable drawable = (BitmapDrawable) i1.getDrawable();
            Bitmap bb11 = drawable.getBitmap();



            File sdCardDirectory = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    "MFA - AFTER - CROP");

            if (!sdCardDirectory.exists()) {
                if (!sdCardDirectory.mkdirs()) {
                    Log.d("MySnaps", "failed to create directory");

                }
            }

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                    .format(new Date());

            String nw = "MFA_CROP_" + timeStamp + ".jpeg";

            File image = new File(sdCardDirectory, nw);

            boolean success = false;

            // Encode the file as a PNG image.
            FileOutputStream outStream;
            try {

                outStream = new FileOutputStream(image);
                bb11.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                /* 100 to keep full quality of the image */

                outStream.flush();
                outStream.close();
                success = true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            b7.setEnabled(true);

        }

快樂編碼:)

這是我的代碼

  intent.setData(mImageCaptureUri); intent.putExtra("crop", "true"); intent.putExtra("outputX", 320); intent.putExtra("outputY", 470); intent.putExtra("aspectX", 320); intent.putExtra("aspectY", 470); intent.putExtra("scale", true); intent.putExtra("return-data", true); 

從本網站引薦

不同設備的屏幕分辨率

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM