bitmap图像工具

1、转换图片为bitmap对象

public static Bitmap getToBitmap(String path, int w, int h) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        // 设置为ture只获取图片大小
        opts.inJustDecodeBounds = true;
        opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
        // 返回为空
        BitmapFactory.decodeFile(path, opts);
        int width = opts.outWidth;
        int height = opts.outHeight;
        float scaleWidth = 0.f, scaleHeight = 0.f;
        if (width > w || height > h) {
            // 缩放
            scaleWidth = ((float) width) / w;
            scaleHeight = ((float) height) / h;
        }
        opts.inJustDecodeBounds = false;
        float scale = Math.max(scaleWidth, scaleHeight);
        opts.inSampleSize = (int) scale;
        WeakReference weak = new WeakReference(
                BitmapFactory.decodeFile(path, opts));
        return Bitmap.createScaledBitmap(weak.get(), w, h, true);
    }

2、把图片bitmap转换成字符流byte

public static byte[] bitmap2Bytes(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

3、吧byte字节转换图片

public static Bitmap Bytes2Bitmap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }

4、吧byte转换图片保存本机

public static void writeImageToDisk(byte[] img, String fileName) {
        try {
            String path=Environment.getExternalStorageDirectory().toString() + File.separator + "image/";
            File file = new File(path);
            if(!file.exists()){
                file.mkdirs();
            }
            File photoFile=new File(path,fileName+".png");
            FileOutputStream fops = new FileOutputStream(photoFile);
            fops.write(img);
            fops.flush();
            fops.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

5、获取文件夹下的所有图片路径

public static List getImagePath() {  
        List imagePathList = new ArrayList();  
        String filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "image";  
        File fileAll = new File(filePath);  
        File[] files = fileAll.listFiles();  
        for (int i = 0; i < files.length; i++) {  
            File file = files[i];  
            if (checkIsImageFile(file.getPath())) {  
                imagePathList.add(file.getPath());  
            }  
        }  
        return imagePathList;  
    } 
//检查扩展名,得到图片格式的文件 
private static boolean checkIsImageFile(String fName) {  
        boolean isImageFile = false;  
        String FileEnd = fName.substring(fName.lastIndexOf(".") + 1,fName.length()).toLowerCase();  
        if (FileEnd.equals("jpg") || FileEnd.equals("png") || FileEnd.equals("gif")  
                || FileEnd.equals("jpeg")|| FileEnd.equals("bmp") ) {  
            isImageFile = true;  
        } else {  
            isImageFile = false;  
        }  
        return isImageFile;  
    } 

6、对图片进行修改操作

public static Bitmap revitionImageSize(String path) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(
                new File(path)));
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        int i = 0;
        Bitmap bitmap = null;
        while (true) {
            if ((options.outWidth >> i <= 1000)
                    && (options.outHeight >> i <= 1000)) {
                in = new BufferedInputStream(
                        new FileInputStream(new File(path)));
                options.inSampleSize = (int) Math.pow(2.0D, i);
                options.inJustDecodeBounds = false;
                bitmap = BitmapFactory.decodeStream(in, null, options);
                break;
            }
            i += 1;
        }
        return bitmap;
    }

7、缩放图片

public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
        if (bitmap == null)
            return bitmap;
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) width / w);
        float scaleHeight = ((float) height / h);
        if (scaleWidth < scaleHeight) {
            matrix.postScale(scaleWidth, scaleWidth);
        } else {
            matrix.postScale(scaleHeight, scaleHeight);
        }
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        return newbmp;
    }

你可能感兴趣的:(Android工具类)