zoukankan      html  css  js  c++  java
  • PhotoshopManager(缩放图片file---->byte[])

    package com.k1.doctor.media;
    
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.UUID;
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.CompressFormat;
    import android.os.Environment;
    import android.graphics.BitmapFactory;
    
    public class PhotoshopManager {
    //计算缩放比例
        public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
            int height = options.outHeight;
            int width = options.outWidth;
            int inSampleSize = 1;
            if (height > reqHeight || width > reqWidth) {
                final int heightRatio = Math.round((float) height / (float) reqHeight);
                final int widthRatio = Math.round((float) width / (float) reqWidth);
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }
            return inSampleSize;
        }
    
        
    //得到压缩后图片的byte[]
        public static byte[] getSmallBitmap(String filePath) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, options);
    
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, 480, 800);
    
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            Bitmap bit = BitmapFactory.decodeFile(filePath, options);
    
            String name = UUID.randomUUID().toString();
            return bitmapToByte(bit, name);
        }
     //bitmap---->byte[]
        public static byte[] bitmapToByte(Bitmap bmp, String name) {
            ByteArrayOutputStream output = new ByteArrayOutputStream();// 初始化一个流对象
            bmp.compress(CompressFormat.JPEG, 100, output);// 把bitmap100%高质量压缩 到
                                                            // output对象里
    
            bmp.recycle();// 自由选择是否进行回收
    
            byte[] result = output.toByteArray();// 转换成功了
            try {
                output.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    }


    //放大
    private static Bitmap big(Bitmap bitmap) {
      Matrix matrix = new Matrix(); 
      matrix.postScale(1.5f,1.5f); //长和宽放大缩小的比例
      Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
      return resizeBmp;
     }
     //缩小
     private static Bitmap small(Bitmap bitmap) {
      Matrix matrix = new Matrix(); 
      matrix.postScale(0.8f,0.8f); //长和宽放大缩小的比例
      Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
      return resizeBmp;
     }
    
    
    
     
  • 相关阅读:
    洛谷 P4001 [ICPC-Beijing 2006]狼抓兔子
    杂题20201201
    杂题20210103
    杂题20201010
    杂题20200928
    ACL1 Contest 1 简要题解
    杂题20200906
    杂题20200623
    USACO2020DEC Gold/Platinum 解题报告
    CSP-S2020游记
  • 原文地址:https://www.cnblogs.com/g-sheng/p/5420680.html
Copyright © 2011-2022 走看看