簡體   English   中英

Android裁剪圖像質量問題

[英]Android cropped image quality issue

我試圖將圖像保存為從Android裁剪的圖像,然后將其顯示在我的應用中。 我正在使用下面的代碼,但是當我嘗試在我的應用程序中查看圖像時,圖像質量不如附件中的圖像好。 做錯什么了嗎? 任何幫助都會很棒。

printscreeiphone6

我的代碼是:

dipToPixel = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1 && resultCode == getActivity().RESULT_OK && data != null) {

    picUri = data.getData();

    performCrop();

}


if (requestCode == 111 && resultCode == getActivity().RESULT_OK && data != null) {

        Bundle extras = data.getExtras();

        Bitmap bitmapImage = extras.getParcelable("data");

        tweetImage.setImageBitmap(bitmapImage);

        tweetImage.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                tweetImage.getViewTreeObserver().removeOnPreDrawListener(this);

                widthPixel = tweetImage.getMeasuredWidth();
                heightPixel = tweetImage.getMeasuredHeight();

                return true;

            }
        });

        System.out.println("photo added");

        addPhotoVar = 1;
        addPhotoBtn.setText("remove");


}

callbackManager.onActivityResult(requestCode, resultCode, data);


}


private void performCrop() {

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", 1);
    cropIntent.putExtra("aspectY", 1);
    //indicate output X and Y

    cropIntent.putExtra("outputX", Math.round(screenWidth / dipToPixel)-10);
    cropIntent.putExtra("outputY", Math.round(screenWidth / dipToPixel)-10);
    //retrieve data on return
    cropIntent.putExtra("return-data", true);
    //start the activity - we handle returning in onActivityResult
    startActivityForResult(cropIntent, 111);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
    // display an error message
    String errorMessage = "your device doesn't support the crop action!";
    Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT);
    toast.show();
}
}

下面是我使用圖像並將其保存到數據庫的代碼:

                tweetImage.buildDrawingCache();
                bm = tweetImage.getDrawingCache();

                if (widthPixel < heightPixel) {

                    basePixel = widthPixel;

                }

                else {

                    basePixel = heightPixel;

                }


                if (basePixel > 768) {

                    widthRatio = (float) 768/basePixel;
                    heightRatio = (float) 768/basePixel;


                }

                else {

                    widthRatio = 1;
                    heightRatio = 1;

                }


                Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(widthPixel*widthRatio), (int)(heightPixel*heightRatio), true);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byteArray1 = stream.toByteArray();

                image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg");

變更:

bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);

至 :

bmResized.compress(Bitmap.CompressFormat.PNG, 100, stream);

由於JPEG格式使用有損壓縮,因此如果您不想降低質量,則應使用PNG保存位圖。

此外,你應該避免使用com.android.camera.action.CROP意圖,因為它沒有在所有設備上存在的解釋在這里

上面的鏈接中列出了一些替代方案,您可以使用其中一種。

使用這個庫,這個庫管理裁剪后的圖像質量,以及它使兩個圖像裁剪庫

請參考此鏈接:

https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html

以下是一些用於圖像裁剪的庫:

https://github.com/lvillani/android-cropimage
https://github.com/biokys/cropimage
https://github.com/MMP-forTour/cropimage (forked from the above one)
https://github.com/dtitov/pickncrop

暫無
暫無

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

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