import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.media.ExifInterface; import android.util.Base64; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.IOException; public class PicUtil { public static String compressImage(String filePath, String type) { Bitmap scaledBitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); // by setting this field as true, the actual bitmap pixels are not loaded in the memory. Just the bounds are loaded. If // you try the use the bitmap here, you will get null. options.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeFile(filePath, options); int actualHeight = options.outHeight; int actualWidth = options.outWidth; // max Height and width values of the compressed image is taken as 816x612 float maxHeight = 816.0f; float maxWidth = 612.0f; float imgRatio = actualWidth / actualHeight; float maxRatio = maxWidth / maxHeight; // width and height values are set maintaining the aspect ratio of the image
android图片压缩并转为base64字符串
最新推荐文章于 2024-07-10 03:10:36 发布
介绍了一个Android工具类,用于高效压缩图片至400k左右,同时提供图片转Base64字符串的方法,确保质量和文件大小的平衡。
摘要由CSDN通过智能技术生成