android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。
有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小。减少图片的大小有两种方法,1. 照小图片; 2. 压缩大图片。 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些; 压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片仍就清晰)。下面组要是介绍图片的压缩:
1. 照相请查看http://www.jb51.net/article/37760.htm ->想要保存图片到制定目录,启动Camera应用时,需要指定文件
2. 压缩过程:
2.1 从图片路径中读取图片(图片很大,不能全部加在到内存中处理,要是全部加载到内存中会内存溢出)
[java]
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 bm = BitmapFactory.decodeFile(filePath, options);
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 bm = BitmapFactory.decodeFile(filePath, options);
2.2 处理图片旋转
[java]
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm,degree) ;
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm,degree) ;[java] view plaincopyprint?private static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
private static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
[java]
view plaincopyprint?private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){
if(bitmap == null)
return null ;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){
if(bitmap == null)
return null ;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
2.3压缩图片
[java]
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0
完整的方法代码:
[java]
public static Bitmap 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 bm = BitmapFactory.decodeFile(filePath, options);
if(bm == null){
return null;
}
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm,degree) ;
ByteArrayOutputStream baos = null ;
try{
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
}finally{
try {
if(baos != null)
baos.close() ;
} catch (IOException e) {
e.printStackTrace();
}
}
return bm ;
}
public static Bitmap 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 bm = BitmapFactory.decodeFile(filePath, options);
if(bm == null){
return null;
}
int degree = readPictureDegree(filePath);
bm = rotateBitmap(bm,degree) ;
ByteArrayOutputStream baos = null ;
try{
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
}finally{
try {
if(baos != null)
baos.close() ;
} catch (IOException e) {
e.printStackTrace();
}
}
return bm ;
}
[java]
view plaincopyprint?private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
}
return inSampleSize;
}
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
}
return inSampleSize;
}
相关推荐
Android图片压缩解决方案,解决图片加载过程中ANR现象
android bitmap compress(图片压缩)代码,需要的朋友可以参考一下
CompressHelper 压缩文件,压缩图片,压缩Bitmap,Compress,CompressImage,CompressFile,CompressBitmap
在Luban的算法策略上,我丰富了外围的api,提供更多的可配参数,多线程压缩和不同细粒度的任务控制。
压缩,图片压缩,压缩Bitmap,Compress,CompressImage,CompressFile,CompressBitmap 主要通过尺寸压缩和质量压缩,以达到清晰度最优,该项目参考了 https://github.com/zetbaitsu/Compressor 的部分代码,且在基础上...
一、图片质量压缩 /** * 质量压缩方法 * @param image * @return */ public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress...
Android图片压缩结合多种压缩方式,常用的有尺寸压缩、质量压缩以及通过JNI调用libjpeg库来进行压缩,三种方式结合使用实现指定图片内存大小,清晰度达到最优。使用:导入lib-bither-compress NativeUtil....
android 图片压缩方法: 第一:质量压缩法: 代码如下:private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG,...
flutter_image_compress 将图像压缩为本机插件(Obj-C / Kotlin) 该库可在Android和iOS上运行。 Webp HEIF(Heic) 适用于iOS的Heif 适用于Android的Heif inSampleSize keepExif 结果关于List和Uint8List ...
一个Android gradle插件,可扫描项目中的资源目录并执行批处理图像压缩。 它提供3种压缩方法以支持有损和无损压缩。 它还记录压缩的文件,适合个人和团队协作。 如何使用 步骤1:在project的build.gradle档案中: ...
CompressTools-Android 压缩,图片压缩,压缩Bitmap 特性: *这是和微信压缩效果类似的压缩方式,采用底层压缩。 *尽量无损压缩图片,保持清晰度最优。可以对比原生方法bitmap.compress(CompressFormat.JPEG, ...
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的...
Android优雅的打包时自动化获取全部res资源 McImage is a Non-invasive plugin for compress all res in your project. Include The img in Jar The img in AAR The img in Module Used algorithm pngquant ...
Compressor是一个轻量级且功能强大的android图像压缩库。 Compressor可让您将大照片压缩为较小尺寸的照片,而图像质量的损失则很小或可忽略不计。 Gradle dependencies { implementation ' id.zelory:compressor:...
>从相机或者相册选取图片,图片压缩 ImageUtil.getImageFromAlbum(UploadFileActivity.this); ImageUtil.getImageFromCamera(UploadFileActivity.this); ImageUtil.compress(); TimeTransform 时间戳格式转换类 >在...
总结Android图片压缩方式包含质量压缩 采样率压缩 比例压缩 jpeg压缩 Luban压缩 以及 jpeg压缩。具体博文详情地址如下:欢迎关注LZ个人公众号~ 不定期发布博文~欢迎大家查阅~
1:介绍图片耗用内存资源原理,以及计算图片内存方法 2:Bitmap压缩图片基本手段和思路
android 完美图片压缩处理工具类demo
McImage是一个非侵入式插件,用于压缩项目中的所有资源。 包括 Jar中的img AAR中的img 模块中的img 二手算法 压缩png 压缩jpg 压缩webp 发布成功! 1.0.1版本现在支持所有build.gradle版本! 特征 压缩所有...