1. 程式人生 > >android 圖片壓縮以及其他處理

android 圖片壓縮以及其他處理

1、質量壓縮方法

  1. private Bitmap compressImage(Bitmap image) {  
      
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質量壓縮方法,這裡100表示不壓縮,把壓縮後的資料存放到baos中  
            int options = 100;  
            while ( baos.toByteArray().length / 1024>100) {  //迴圈判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮         
                baos.reset();//重置baos即清空baos  
                image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裡壓縮options%,把壓縮後的資料存放到baos中  
                options -= 10;//每次都減少10  
            }  
            ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮後的資料baos存放到ByteArrayInputStream中  
            Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream資料生成圖片  
            return bitmap;  
        } 

    2圖片大小

  2.  /**
         * 把圖片按螢幕寬度縮放
         *
         * @param context
         * @param wi
         * @param hi
         * @return
         */
        public static void setPicWidth(Context context, ImageView imageView, String wi, String hi) {
            DisplayMetrics dm2 = context.getResources().getDisplayMetrics();
            float density = dm2.density;
    
            int widthPixels = dm2.widthPixels - (int) (30 / density + 0.5f);
            if (wi.equals("0") || "0".equals(hi)) {
               
                imageView.setLayoutParams(new LinearLayout.LayoutParams(widthPixels, widthPixels));
                return;
            }
            double w, h;
    
            w = Double.parseDouble(wi);
            h = Double.parseDouble(hi);
    
    
            if (w == widthPixels) {
               
                imageView.setLayoutParams(new LinearLayout.LayoutParams((int) widthPixels, (int) h));
            } else {
                try {
                    double height = widthPixels * h / w;
                    
                    LinearLayout.LayoutParams para = (LinearLayout.LayoutParams) imageView.getLayoutParams();
                    para.height = (int)height;
                    imageView.setLayoutParams(para);
                    //如果寬度大於螢幕寬度
    //                if (w > widthPixels) {
    //                    double size = widthPixels / w + widthPixels % w;
    //                    double hh = (h * size);
    //                    imageView.setLayoutParams(new LinearLayout.LayoutParams(widthPixels, (int) hh));
    
    //                } else {
    //                    double size = widthPixels / w+ widthPixels % w;
    //                    double hh = (h * size);
    //                    imageView.setLayoutParams(new LinearLayout.LayoutParams(widthPixels, (int) hh));
    //                    LogUtil.I("ImageUtil_5", "size=" + size + "hh=" + hh);
    //                }
                } catch (Exception e) {
                    LogUtil.I("ImageUtil_6", "e" + e.toString());
                }
            }
        }

3


    //將Drawable轉化為Bitmap
    public static Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable == null) {
            return null;
        }
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888
                        : Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;

    }

    //獲得圓角圖片的方法
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
        if (bitmap == null) {
            return null;
        }
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }

5

 //獲得帶倒影的圖片方法
    public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }final int reflectionGap = 4;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
                0, height / 2, width, height / 2, matrix, false);

        Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmapWithReflection);
        canvas.drawBitmap(bitmap, 0, 0, null);
        Paint deafalutPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap,
                deafalutPaint);

        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0,
                bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
                + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        // Draw a rectangle using the paint with our linear gradient
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
                + reflectionGap, paint);

        return bitmapWithReflection;
    }

 6