• Android 实现4MB以上大图片压缩不失真方法


      1 import java.io.ByteArrayOutputStream;
      2 import java.io.File;
      3 
      4 import android.content.Context;
      5 import android.content.Intent;
      6 import android.graphics.Bitmap;
      7 import android.graphics.BitmapFactory;
      8 import android.net.Uri;
      9 import android.os.Environment;
     10 import android.util.Base64;
     11 
     12 public class PictureUtil {
     13 
     14     /**
     15      * 把bitmap转换成String
     16      * 
     17      * @param filePath
     18      * @return
     19      */
     20     public static String bitmapToString(String filePath) {
     21         Bitmap bm = getSmallBitmap(filePath);
     22         ByteArrayOutputStream baos = new ByteArrayOutputStream();
     23         bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
     24         byte[] b = baos.toByteArray();
     25         int x = b.length;
     26         System.out.println("" + x);
     27         return Base64.encodeToString(b, Base64.DEFAULT);
     28 
     29     }
     30 
     31     /**
     32      * 计算图片的缩放值
     33      * 
     34      * @param options
     35      * @param reqWidth
     36      * @param reqHeight
     37      * @return
     38      */
     39     public static int calculateInSampleSize(BitmapFactory.Options options,
     40             int reqWidth, int reqHeight) {
     41         // Raw height and width of image
     42         final int height = options.outHeight;
     43         final int width = options.outWidth;
     44         int inSampleSize = 1;
     45 
     46         if (height > reqHeight || width > reqWidth) {
     47 
     48             // Calculate ratios of height and width to requested height and
     49             // width
     50             final int heightRatio = Math.round((float) height
     51                     / (float) reqHeight);
     52             final int widthRatio = Math.round((float) width / (float) reqWidth);
     53 
     54             // Choose the smallest ratio as inSampleSize value, this will
     55             // guarantee
     56             // a final image with both dimensions larger than or equal to the
     57             // requested height and width.
     58             inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
     59         }
     60 
     61         return inSampleSize;
     62     }
     63 
     64     /**
     65      * 根据路径获得突破并压缩返回bitmap用于显示
     66      * 
     67      * @param imagesrc
     68      * @return
     69      */
     70     public static Bitmap getSmallBitmap(String filePath) {
     71         final BitmapFactory.Options options = new BitmapFactory.Options();
     72         options.inJustDecodeBounds = true;
     73         BitmapFactory.decodeFile(filePath, options);
     74 
     75         // Calculate inSampleSize
     76         options.inSampleSize = calculateInSampleSize(options, 480, 800);
     77 
     78         // Decode bitmap with inSampleSize set
     79         options.inJustDecodeBounds = false;
     80 
     81         return BitmapFactory.decodeFile(filePath, options);
     82     }
     83 
     84     /**
     85      * 根据路径删除图片
     86      * 
     87      * @param path
     88      */
     89     public static void deleteTempFile(String path) {
     90         File file = new File(path);
     91         if (file.exists()) {
     92             file.delete();
     93         }
     94     }
     95 
     96     /**
     97      * 添加到图库
     98      */
     99     public static void galleryAddPic(Context context, String path) {
    100         Intent mediaScanIntent = new Intent(
    101                 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    102         File f = new File(path);
    103         Uri contentUri = Uri.fromFile(f);
    104         mediaScanIntent.setData(contentUri);
    105         context.sendBroadcast(mediaScanIntent);
    106     }
    107 
    108     /**
    109      * 获取保存图片的目录
    110      * 
    111      * @return
    112      */
    113     public static File getAlbumDir() {
    114         File dir = new File(
    115                 Environment
    116                         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
    117                 getAlbumName());
    118         if (!dir.exists()) {
    119             dir.mkdirs();
    120         }
    121         return dir;
    122     }
    123 
    124     /**
    125      * 获取保存 隐患检查的图片文件夹名称
    126      * 
    127      * @return
    128      */
    129     public static String getAlbumName() {
    130         return "sheguantong";
    131     }
    132 }

    调用:   

    String content = PictureUtil.bitmapToString("/storage/sdcard0/Pictures/sheguantong/test.jpg");
    System.out.println("" + content);

  • 相关阅读:
    Java8 新的日期和时间API(笔记)
    java对象的强引用,软引用,弱引用和虚引用
    对性能有何帮助(读书笔记)
    ThreadLocal的实现原理(读书笔记)
    ThreadLocal的简单使用(读书笔记)
    编程、滑板与做饭
    在三四线城市谈软件业务,及其他
    EOS开发中内存RAM如何买卖
    用Visual Studio和c#开发以太坊dapp
    使用PHP和树莓派开发一个比特币和以太坊交易机器人
  • 原文地址:https://www.cnblogs.com/xiaoyao095/p/5367866.html
Copyright © 2020-2023  润新知