Android裁剪图像质量问题

时间:2016-07-14 07:17:49

标签: android image-processing resize-crop image-quality

我正在尝试将图像保存为从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");

3 个答案:

答案 0 :(得分:1)

更改:

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

到:

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

由于JPEG格式使用有损压缩,如果您不想要质量损失,则应使用PNG保存位图。

此外,您应避免使用com.android.camera.action.CROP意图,因为它并非如here所解释的那样存在于所有设备上。

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

答案 1 :(得分:1)

使用此库,此库可以管理裁剪的图像质量,同时保留图像Crop Library

答案 2 :(得分:0)

请参阅此链接:

traceShowId :: Show a => a -> a

以下是图像裁剪需要考虑的一些库:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Diagnostics;
using System.Data;

namespace ConsoleApplication45
{
    class Program
    {

        static void Main(string[] args)
        {
            List<Business> companies = new List<Business>() {
                new Business() { name = "CompanyA",items = new List<Item>(){ new Item() { name = "Item1"}, new Item() { name = "Item2"}}},
                new Business() { name = "CompanyB", items = new List<Item>() { new Item() { name = "Item3"}, new Item() { name = "Item4"}}},
                new Business() { name = "CompanyA",items = new List<Item>(){ new Item() { name = "Item5"}, new Item() { name = "Item6"}}},
            };


            var groups = companies.GroupBy(x => x.name).Select(x => new { business = x.FirstOrDefault(), items = x.SelectMany(y => y.items) });
            List<Business> newCompanies = new List<Business>();
            foreach (var group in groups)
            {
                Business company = group.business;
                company.items = group.items.ToList();
                newCompanies.Add(company);
            }
        }

    }
    public class Business
    {
        public string name { get; set; }
        public List<Item> items { get; set; }
        public decimal taxrate { get; set; }
        public string month { get; set; }
    }
    public class Item
    {
        public string name { get;set;}
        public decimal price { get;set;}
        public decimal total { get;set;}
    }
}