簡體   English   中英

如何在android中沒有質量中斷的情況下減小圖像大小

[英]how to reduce image Size without Quality break in android

                Bitmap bmp = getResizedBitmap(
                        BitmapFactory.decodeByteArray(data, 0, data.length),
                        500);


public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float) width / (float) height;
        if (bitmapRatio > 0) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        return Bitmap.createScaledBitmap(image, width, height, true);
    }

這是我的代碼 使用 getResizedBitmap 我能夠減小圖像大小但無法在 android 中保持其原始質量請告訴我如何保持質量良好以便圖像大小減小和質量應該不錯請建議我!

嗨,請嘗試下面的代碼希望它滿足你想要的

public static Bitmap scaleImage(String p_path, int p_reqHeight, int p_reqWidth) throws Throwable
    {
        Bitmap m_bitMap = null;
        System.gc();
        File m_file = new File(p_path);
        if (m_file.exists())
        {
            BitmapFactory.Options m_bitMapFactoryOptions = new BitmapFactory.Options();
            m_bitMapFactoryOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
            m_bitMapFactoryOptions.inSampleSize = calculateInSampleSize(m_bitMapFactoryOptions, p_reqHeight, p_reqWidth);
            m_bitMapFactoryOptions.inJustDecodeBounds = false;
            m_bitMap = BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
        }
        else
        {
            throw new Throwable(p_path + " not found or not a valid image");
        }
        return m_bitMap;
    }


    // Helper method
    private static int calculateInSampleSize(BitmapFactory.Options p_options, int p_reqWidth, int p_reqHeight)
    {
        // Raw height and width of image
        final int m_height = p_options.outHeight;
        final int m_width = p_options.outWidth;
        int m_inSampleSize = 1;
        if (m_height > p_reqHeight || m_width > p_reqWidth)
        {
            final int m_halfHeight = m_height / 2;
            final int m_halfWidth = m_width / 2;
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((m_halfHeight / m_inSampleSize) > p_reqHeight && (m_halfWidth / m_inSampleSize) > p_reqWidth)
            {
                m_inSampleSize *= 2;
            }
        }
        return m_inSampleSize;
    }

暫無
暫無

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

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