中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android圖片處理工具類(圓角,壓縮)

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
工作中用到的圖片處理工具類,簡單寫下來,以便備用!
public class BitmapUtils {

    /**
     * 圖像背景圓角處理
     * bitmap要處理的圖片 roundPx 圖片彎角的圓度一般是5到10之間
     */
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
        // 創(chuàng)建與原圖大小一樣的bitmap文件,Config.ARGB_8888根據(jù)情況可以改用其它的
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        // 實例畫布,繪制的bitmap將保存至output中
        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(android.graphics.PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        bitmap.recycle();
        bitmap = null;
        return output;
    }

    /**
     * bitmap縮放
     * width要縮放的寬度 height要縮放的高度
     */
    public static Bitmap getBitmapDeflation(Bitmap bitmap, int width, int height, boolean recycle) {

        if (null == bitmap) {

          return null;

          }
        float scaleWidth = 0f;
        float scaleHeight = 0f;
        // 獲取bitmap寬高
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        // 計算縮放比,圖片的寬高小于指定的寬高則不縮放
        if (width < bitmapWidth) {
            scaleWidth = ((float) width) / bitmapWidth;
        } else {
            scaleWidth = 1.00f;
        }
        if (height < bitmapHeight) {
            scaleHeight = ((float) height) / bitmapHeight;
        } else {
            scaleHeight = 1.00f;
        }
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
        if (recycle && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
        bitmap = null;

        return newBitmap;
    }

    /**
     *
     * 方法概述:進入圖片的大小與質(zhì)量壓縮,用于區(qū)分大小圖片
     */
    public static Bitmap getCompressedImage(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        // 開始讀入圖片,此時把options.inJustDecodeBounds 設(shè)回true了
        newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
        newOpts.inPurgeable = true;
        newOpts.inJustDecodeBounds = true;
        FileInputStream is = null;
        try {
            is = new FileInputStream(srcPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(is, null, newOpts);// 此時返回bm為空
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        // 現(xiàn)在主流手機比較多是800*480分辨率,所以高和寬我們設(shè)置為
        float hh = 130f;// 這里設(shè)置高度為800f
        float ww = 130f;// 這里設(shè)置寬度為480f
        // 縮放比。由于是固定比例縮放,只用高或者寬其中一個數(shù)據(jù)進行計算即可
        int be = 1;// be=1表示不縮放
        if (w > h && w > ww) {// 如果寬度大的話根據(jù)寬度固定大小縮放
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {// 如果高度高的話根據(jù)寬度固定大小縮放
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0) {
            be = 1;
        }
        newOpts.inSampleSize = be;// 設(shè)置縮放比例
        // 重新讀入圖片,注意此時已經(jīng)把options.inJustDecodeBounds 設(shè)回false了
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        if (bitmap != null) { return compressImageSize(bitmap, 8);// 壓縮好比例大小后再進行質(zhì)量壓縮
        }
        return null;
    }

    /**
     *
     * 方法概述:圖片質(zhì)量壓縮
     */
    protected static Bitmap compressImageSize(Bitmap image, int size) {
        if (image == null)
            return image;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 質(zhì)量壓縮方法,這里10表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1000 > size && options / 3 > 0) { // 循環(huán)判斷如果壓縮后圖片是否大于10kb,大于繼續(xù)壓縮
            baos.reset();// 重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
            options -= options / 3;// 每次都減少30%
        }

        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream數(shù)據(jù)生成圖片
        return bitmap;
    }

    /**
     *
     * 方法概述:保存圖片
     */
    public static String saveBitmapWithName(String path, String bitName, Bitmap mBitmap) {
        File f = new File(path + bitName + ".png");
        String url = path + bitName + ".png";
        try {
            f.createNewFile();
        } catch (IOException e) {
            System.out.println("文件創(chuàng)建出錯");
        }
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            System.out.println(" 創(chuàng)建文件流失敗");
            e.printStackTrace();
        }
        mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }

    /**
     *
     * 方法概述:根據(jù)傳入?yún)?shù)保存圖片
     */
    public static boolean saveImageTo(Bitmap photo, String spath) {
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(spath, false));
            photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:限制文件下載速度php代碼

下一篇:iOS 簡單的音頻操作代碼