zoukankan      html  css  js  c++  java
  • Bitmap工具类BitmapHelper

    BitmapHelper

    提供一些获取本地缩略图,获取网络图片。dp与px的相互转换等方法。

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.ProtocolException;
    import java.net.URL;
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.PixelFormat;
    import android.graphics.PorterDuff;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.graphics.Bitmap.Config;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.graphics.drawable.NinePatchDrawable;
    import android.util.Base64;
    
    public class BitmapHelper {
    
        private static BitmapHelper util;
        private static Context context = App.getInstance();
    
        public static BitmapHelper getInstance() {
    
            if (util == null) {
                util = new BitmapHelper();
    
            }
            return util;
    
        }
    
        private BitmapHelper() {
            super();
        }
    
        /**
         * 将bitmap转为base64
         * 
         * @param bitmap
         * @param imgFormat
         * @return
         */
        @SuppressLint("NewApi")
        public String bitmapToBase64(Bitmap bitmap, String imgFormat) {
    
            if (null == bitmap) {
    
                return null;
            }
            ByteArrayOutputStream out = null;
            try {
                out = new ByteArrayOutputStream();
                Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
                if (imgFormat.equalsIgnoreCase("png"))
                    compressFormat = Bitmap.CompressFormat.PNG;
                bitmap.compress(compressFormat, 85, out);
                out.flush();
                out.close();
                byte[] imgBytes = out.toByteArray();
                return Base64.encodeToString(imgBytes, Base64.DEFAULT);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
        /**
         * 将图片转为base64
         * 
         * @param imgPath
         * @param imgFormat
         * @return
         */
        @SuppressLint("NewApi")
        public String urlToBase64(String imgPath, String imgFormat) {
            Bitmap bitmap = null;
            if (imgPath != null && imgPath.length() > 0) {
                bitmap = readBitmap(imgPath);
            }
            return bitmapToBase64(bitmap, imgFormat);
    
        }
    
        private Bitmap readBitmap(String imgPath) {
            try {
                return BitmapFactory.decodeFile(imgPath);
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 得到圆角的bitmap
         * 
         * @param bitmap
         * @param corner
         *            以长或宽的比例为半径,2表示二分之中的一个。8表示八分之中的一个
         * @return
         */
        public Bitmap getRoundedCornerBitmap(Bitmap bitmap, int corner) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            float roundPx;
            float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
            if (width <= height) {
                roundPx = width / corner;
                top = 0;
                bottom = width;
                left = 0;
                right = width;
                height = width;
                dst_left = 0;
                dst_top = 0;
                dst_right = width;
                dst_bottom = width;
            } else {
                roundPx = height / corner;
                float clip = (width - height) / corner;
                left = clip;
                right = width - clip;
                top = 0;
                bottom = height;
                width = height;
                dst_left = 0;
                dst_top = 0;
                dst_right = height;
                dst_bottom = height;
            }
            Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect src = new Rect((int) left, (int) top, (int) right,
                    (int) bottom);
            final Rect dst = new Rect((int) dst_left, (int) dst_top,
                    (int) dst_right, (int) dst_bottom);
            final RectF rectF = new RectF(dst);
            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, src, dst, paint);
            return output;
        }
    
        /**
         * 设置Bitmap圆角
         * 
         * @param image
         *            传入的Bitmap
         * @param outerRadiusRat
         *            圆角半径
         * @return 返回处理后的Bitmap
         */
        public Bitmap getRoundedBitmap(Bitmap image, int outerRadiusRat) {
            int x = image.getWidth();
            int y = image.getHeight();
            // 依据源文件新建一个darwable对象
            Drawable imageDrawable = new BitmapDrawable(image);
    
            // 新建一个新的输出图片
            Bitmap output = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
    
            // 新建一个矩形
            RectF outerRect = new RectF(0, 0, x, y);
    
            // 产生一个红色的圆角矩形
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.RED);
            canvas.drawRoundRect(outerRect, outerRadiusRat, outerRadiusRat, paint);
    
            // 将源图片绘制到这个圆角矩形上
            // 具体解释见http://lipeng88213.iteye.com/blog/1189452
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            imageDrawable.setBounds(0, 0, x, y);
            canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
            imageDrawable.draw(canvas);
            canvas.restore();
    
            return output;
        }
    
        /**
         * 图片压缩
         * 
         * @param image
         * @return
         */
        public Bitmap getCompressBitmap(Bitmap image, int minSize) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            int options = 100;
            while (baos.toByteArray().length / 1024 > minSize) {
                options -= 10;
                baos.reset();
                image.compress(Bitmap.CompressFormat.JPEG, options, baos);
            }
            ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
            Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
            return bitmap;
        }
    
        /**
         * 得到本地缩略图
         * 
         * @param srcPath
         * @return
         */
        public Bitmap getCompressBitmapByLocal(String srcPath, int ww, int hh) {
            BitmapFactory.Options newOpts = new BitmapFactory.Options();
            newOpts.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
            newOpts.inJustDecodeBounds = false;
            int w = newOpts.outWidth;
            int h = newOpts.outHeight;
    
            int be = 1;
            if (w > h && w > ww) {
                be = (int) (newOpts.outWidth / ww);
            } else if (w < h && h > hh) {
                be = (int) (newOpts.outHeight / hh);
            }
            if (be <= 0)
                be = 1;
            newOpts.inSampleSize = be;
            bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
            return getCompressBitmap(bitmap, 100);
        }
    
        /**
         * 放大缩小图片
         * 
         * @param bitmap
         * @param w
         * @param h
         * @return
         */
        public Bitmap getZoomBitmap(Bitmap bitmap, int w, int h) {
            Bitmap newbmp = null;
            if (bitmap != null) {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                Matrix matrix = new Matrix();
                float scaleWidht = ((float) w / width);
                float scaleHeight = ((float) h / height);
                matrix.postScale(scaleWidht, scaleHeight);
                newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
                        true);
            }
            return newbmp;
        }
    
        /**
         * 得到网络图片
         * 
         * @param imgUrl
         * @return
         */
        public Bitmap getHttpBitmap(String imgUrl) {
    
            Bitmap bitmap = null;
            if (!PhoneHelper.getInstance().isNetworkConnected()) {
    
                return null;
            }
    
            InputStream is = getHttpPicInputStream(imgUrl);
            if (is != null) {
    
                bitmap = BitmapFactory.decodeStream(is);
    
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
    
            return bitmap;
    
        }
    
        /**
         * 得到网络图片的输入流
         * 
         * @param url
         * @return
         */
        public InputStream getHttpPicInputStream(String url) {
            InputStream is = null;
            URL url1 = null;
            try {
                url1 = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
                conn.setDoInput(true);
                conn.setConnectTimeout(30000);
                conn.setRequestMethod("GET");
                conn.connect();
    
                if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    is = conn.getInputStream();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return is;
        }
    
        /**
         * Bitmap转化为drawable
         * 
         * @param bitmap
         * @return
         */
        public Drawable bitmap2Drawable(Bitmap bitmap) {
            return new BitmapDrawable(bitmap);
        }
    
        /**
         * Drawable 转 bitmap
         * 
         * @param drawable
         * @return
         */
        public Bitmap drawable2Bitmap(Drawable drawable) {
            if (drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            } else if (drawable instanceof NinePatchDrawable) {
                Bitmap bitmap = Bitmap
                        .createBitmap(
                                drawable.getIntrinsicWidth(),
                                drawable.getIntrinsicHeight(),
                                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                                        : Bitmap.Config.RGB_565);
                Canvas canvas = new Canvas(bitmap);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                        drawable.getIntrinsicHeight());
                drawable.draw(canvas);
                return bitmap;
            } else {
                return null;
            }
        }
    
        /**
         * 将bitmap存为本地图片
         * @param bitmap
         * @param path
         * @return
         */
        public boolean saveBitmapForSdCard(Bitmap bitmap, String path) {
            File f = new File(path);
            File parent = new File(f.getParent());
            FileOutputStream out = null;
            if (!parent.exists()) {
                parent.mkdirs();
            }
            try {
                f.createNewFile();
                out = new FileOutputStream(f);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
                out.flush();
                out.close();
    
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            } finally {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * dp转px
         * 
         * @param context
         * @param dpValue
         * @return
         */
        public int dp2Px(float dpValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }
    
        /**
         * px转dp
         */
        public int px2Dp(float pxValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
    
            return (int) (pxValue / scale + 0.5f);
        }
    
        /**
         * 依据网络状态获取本地图片缩略图
         * 
         * @param picPath
         * @return
         */
        public Bitmap getBitmapByNetWork(String picPath) {
            int networkType = PhoneHelper.getInstance().getNetType();
    
            Bitmap tagBm = null;
            ByteArrayOutputStream decodeBitmap = null;
            int size = 100;
            switch (networkType) {
            case 1:
                size = 150;
                break;
            case 2:
                size = 80;
                break;
            case 3:
            case 4:
                size = 100;
                break;
    
            default:
                return null;
    
            }
            decodeBitmap = getBitmapByteArray(picPath, size);
    
            if (decodeBitmap != null) {
                byte[] array = decodeBitmap.toByteArray();
                try {
                    tagBm = BitmapFactory.decodeByteArray(array, 0, array.length);
                } catch (OutOfMemoryError oError) {
                    oError.printStackTrace();
                } catch (Exception e) {
                }
    
            }
    
            return tagBm;
    
        }
    
        /**
         * 将本地图片转为输出流
         * @param path
         * @param size
         * @return
         */
        public ByteArrayOutputStream getBitmapByteArray(String path, int size) {
    
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;// 设置成了true,不占用内存,仅仅获取bitmap宽高
            BitmapFactory.decodeFile(path, opts);
            opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800);
    
            opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,由于之前我们将其设置成了true
            opts.inPurgeable = true;
            opts.inInputShareable = true;
            opts.inDither = false;
            opts.inPurgeable = true;
            opts.inPreferredConfig = Bitmap.Config.RGB_565;
            opts.inTempStorage = new byte[100 * 1024];
            FileInputStream is = null;
            Bitmap bmp = null;
    
            ByteArrayOutputStream baos = null;
            try {
                is = new FileInputStream(path);
                bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);
    
                baos = new ByteArrayOutputStream();
    
                bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                int options = 100;
                while (baos.toByteArray().length / 1024 > size && options > 4) { // 循环推断假设压缩后图片是否大于100kb,大于继续压缩
                    baos.reset();// 重置baos即清空baos
                    bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%。把压缩后的数据存放到baos中
                    options -= 2;// 每次都降低10
                }
                bmp.recycle();
                return baos;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (OutOfMemoryError oError) {
                oError.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                    if (baos != null) {
                        baos.close();
                    }
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.gc();
            }
            return baos;
        }
    
        /**
         * 为了得到恰当的inSampleSize,Android提供了一种动态计算的方法
         * @param options
         * @param minSideLength
         * @param maxNumOfPixels
         * @return
         */
        private int computeSampleSize(BitmapFactory.Options options,
                int minSideLength, int maxNumOfPixels) {
            int initialSize = computeInitialSampleSize(options, minSideLength,
                    maxNumOfPixels);
    
            int roundedSize;
            if (initialSize <= 8) {
                roundedSize = 1;
                while (roundedSize < initialSize) {
                    roundedSize <<= 1;
                }
            } else {
                roundedSize = (initialSize + 7) / 8 * 8;
            }
    
            return roundedSize;
        }
    
        private int computeInitialSampleSize(BitmapFactory.Options options,
                int minSideLength, int maxNumOfPixels) {
            double w = options.outWidth;
            double h = options.outHeight;
    
            int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                    .sqrt(w * h / maxNumOfPixels));
            int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
                    Math.floor(w / minSideLength), Math.floor(h / minSideLength));
    
            if (upperBound < lowerBound) {
                return lowerBound;
            }
    
            if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
                return 1;
            } else if (minSideLength == -1) {
                return lowerBound;
            } else {
                return upperBound;
            }
        }
    
    }
  • 相关阅读:
    tar解压时如何去掉第一级目录并解压到指定目录?
    ubuntu下容器无法启动报错"failed to start daemon: failed to dial "/run/containerd/containerd.sock": unknown service containerd.services.namespaces.v1.Namespaces: not implemented"如何处理?
    redis用法介绍
    Map.putAll()用法
    Random,ThreadLocalRandom,SecureRandom的几点思考
    SOAPUI---使用断言
    AutoUpdater迁移到Github
    VirtualBox: linux 没有权限访问共享文件夹的问题
    MakeFIle: 解决“/bin/bash^M: bad interpreter: No such file or directory”的问题
    ubuntu 备忘录
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7141840.html
Copyright © 2011-2022 走看看