Android图片处理——压缩、剪裁、圆角、保存
项目中用到的关于图片的处理
public class UtilPicture {
public static final String IMAGE_UNSPECIFIED = "image/*";
/**
* 将图片存储至SD卡,需推断是否装有SD卡、是否可读写、是否有空间。否则提示出错
* @param ctx 上下文
* @param jpeg 要存储的照片
* @param quality 压缩照片的质量,0至100,100最佳。一般80-90
* @param filePath 存储的路径
* @param filename 照片的名称
* @return
*/
public static boolean save_picture(Context ctx, Bitmap bitmap, int quality, String filePath, String filename) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
byte[] data = baos.toByteArray();
if (!Common.checkSDStatus(data.length/1024/1024)) {
Toast.makeText(ctx, "您的储存卡有错误", Toast.LENGTH_SHORT).show();
return false;
}
try {
File destDir = new File(filePath);
if (!destDir.exists())
destDir.mkdirs();
String path = filePath + "/" + filename;
File file = new File(path);
if (!file.exists())
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 获得圆角图片的方法
* @param bitmap 需处理的图片
* @param roundPx 圆角的弧率
* @return
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
/**
* 图片中绘入GPS和时间等文字
* @param bitmap 需处理的图片
* @param datetime 时间
* @param lat 经度
* @param lng 纬度
* @return
*/
public static Bitmap getGpsBitmap(Bitmap bitmap, String datetime, String lat, String lng) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
/* 把位图写进画布canvas类 */
Canvas canvas = new Canvas(output);
/* 画布的区域 */
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
/* 喷漆Paint类 */
Paint paint = new Paint();
paint.setAntiAlias(true);//消除锯齿
paint.setColor(Color.RED);//着色
paint.setTextSize(16);//字体大小
canvas.drawText("经度:" + lng, 10, 20, paint);
canvas.drawText("纬度:" + lat, 10, 38, paint);
canvas.drawText("时间:" + datetime, 10, 56, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
/**
* 裁切图片
* @param originFile 源文件
* @param TargetFile 目标文件
* @param aspect 宽高比例,假设为null,则不限制
* @param output 输出分辨率
* @return
*/
public static Intent startPhotoZoom(File originFile, File TargetFile, int[] aspect, int[] output) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(originFile), IMAGE_UNSPECIFIED);
intent.putExtra("crop", "true");
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", false);
if (null != output) {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
BitmapFactory.decodeFile(originFile.getPath(), op);
int jpgWidth = op.outWidth;
int jpgHeight = op.outHeight;
if (jpgWidth > output[0] && jpgHeight > output[1]) {
intent.putExtra("outputX", output[0]);
intent.putExtra("outputY", output[1]);
}
}
if (null != aspect) {
intent.putExtra("aspectX", aspect[0]);
intent.putExtra("aspectY", aspect[1]);
}
if (!TargetFile.exists())
try {
TargetFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(TargetFile));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
return intent;
}
/**
* 从相冊中选择一张照片之后。获取该照片的绝对路径
* @param ctx
* @param photoUri
* @return
*/
public static String getPickPhotoPath(Context ctx, Uri photoUri) {
Cursor cursor = null;
try {
cursor = ctx.getContentResolver().query(photoUri, null, null, null, null);
cursor.moveToFirst();
String imgPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
return imgPath;
} catch (Exception e) {
return "";
} finally {
cursor.close();
}
}
public static File getPickPhotoFile(Context ctx, Uri photoUri) {
String imgPath = getPickPhotoPath(ctx, photoUri);
if (!TextUtils.isEmpty(imgPath))
return new File(imgPath);
else
return null;
}
/**
* 压缩图片大小,避免图片过大。保持比例不变,宽或高不超过XX个像素
* @param newName 新的文件名称称
* @param filePath 原文件全路径。包括文件名称
* @param attachPath 处理过后,文件存放的位置
* @param String 新的文件全路径
*/
public static String compressPixelPhotos(final Context ctx, final String newName, final String filePath,
final String attachPath) {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, op);
int jpgWidth = op.outWidth;
int jpgHeight = op.outHeight;
if (jpgWidth > 800 || jpgHeight > 800) {
int wSendRatio = (int) Math.ceil(jpgWidth / 800.0f);
int hSendRatio = (int) Math.ceil(jpgHeight / 800.0f);
if (wSendRatio > 1 && hSendRatio > 1) {
op.inSampleSize = wSendRatio > hSendRatio ?
wSendRatio : hSendRatio;
}
op.inJustDecodeBounds = false;
Bitmap b = BitmapFactory.decodeFile(filePath, op);
if (!save_picture(ctx, b, 90, attachPath, newName)) {
Common.copyFileToFile(filePath, attachPath + File.separator + newName);
}
if (b != null && !b.isRecycled())
b.recycle();
} else {
Common.copyFileToFile(filePath, attachPath + File.separator + newName);
}
return attachPath + File.separator + newName;
}
/**
* 检查图片分辨率大小,是否须要压缩
* @param ctx
* @param filePath
* @return
*/
public static boolean compressPixelPhotosCheck(final Context ctx, final String filePath) {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, op);
if (op.outWidth > 800 || op.outHeight > 800) {
return true;
} else {
return false;
}
}
/**
* 将
* @param filename 文件名称,全路径
* @param jpgGetWidth 照片宽
* @param jpgGetHeight 照片高
* @return
*/
public static Bitmap decodeFile(String filename, int jpgGetWidth, int jpgGetHeight) {
Bitmap b = null;
try {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filename, op);
int jpgWidth = op.outWidth;
int jpgHeight = op.outHeight;
int wSendRatio = (int) Math.ceil(jpgWidth / Double.valueOf(jpgGetWidth));
int hSendRatio = (int) Math.ceil(jpgHeight / Double.valueOf(jpgGetHeight));
if (wSendRatio > 1 && hSendRatio > 1) {
op.inSampleSize = wSendRatio > hSendRatio ? wSendRatio : hSendRatio;
}
op.inJustDecodeBounds = false;
b = BitmapFactory.decodeFile(filename, op);
} catch (Exception e) {
}
return b;
}
}
很多其它交流可加技术讨论群:71262831
扫一扫,一起坐看风云变幻。扫描下方二维码关注it达人(也可微信搜索:it达人)。
为您推送最新开发资源、分享it牛人职业发展经验:
Android图片处理——压缩、剪裁、圆角、保存的更多相关文章
- android图片的压缩和水印
学习了一下压缩和水印,以后要用到的时候可以直接来这里拷贝 activity_main.xml <LinearLayout xmlns:android="http://schemas.a ...
- android图片的缩放、圆角处理
android中图片缩放方法有三种:1,bitmapFactory:2,bitmap+metrix:3,thumbUtil 方法一:bitmapFactory: public static Bitma ...
- Android 图片压缩各种方式
前言:由于公司项目当中需要用到压缩这块的相应技术,之前也做过的图片压缩都不是特别的理想, 所以这次花了很多心思,仔细研究和在网上找到了很多相对应的资料.为了就是 以后再做的时候直接拿来用就可以了 ...
- Android图片剪裁库
最近利用一周左右的业余时间,终于完成了一个Android图片剪裁库,核心功能是根据自己的理解实现的,部分代码参考了Android源码的图片剪裁应用.现在将该代码开源在Github上以供大家学习和使用, ...
- Android 图片压缩,基于比例和质量压缩
package cc.util.android.image; import java.io.ByteArrayOutputStream; import java.io.File; import jav ...
- Android 图片内存优化与图片压缩
1. 对图片本身进行操作 尽量不要使用 setImageBitmap.setImageResource. BitmapFactory.decodeResource 来设置一张大图,因为这些方法在完成 ...
- Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案
1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...
- (转)android图片压缩总结
原文地址:http://blog.csdn.net/cherry609195946/article/details/9264409 一.图片的存在形式 1.文件形式(即以二进制形式存在于硬盘上)2.流 ...
- Android图片压缩方法总结
本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 第一:质量压缩方法: ? 1 2 3 ...
随机推荐
- BZOJ——T 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛
http://www.lydsy.com/JudgeOnline/problem.php?id=1612 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1 ...
- ArcGIS api for javascript——图形-选择一个范围内的点
描述 本例展示了如何使用图形显示查询结果,如何使用draw toolbar在地图上选择图形和如何修改图形符号为“高亮”. 在地图上画一个矩形区域,加亮矩形范围内的城市.这个应用统计高亮的城市个数并列出 ...
- Snail—UI学习之导航视图控制器UINavigationController(系统)
背景 有一个根视图控制器 然后跳转到第一个界面 第一个界面能够返回到根视图 也能够跳转到第二个视图 第二个视图能够直接返回到根视图 新建三个ViewController RootViewCon ...
- 让透明div里的文字不透明
最近在工作中遇到一个问题,我在div里写上文字,当我把div变为半透明的时候,里面的文字也会随之透明.情况如下: <div class="box"> 这是一段不应该透明 ...
- Spring Security 4 Method security using @PreAuthorize,@PostAuthorize, @Secured, EL--转
原文地址:http://websystique.com/spring-security/spring-security-4-method-security-using-preauthorize-pos ...
- Gallery滑动一页(一个Item)效果
本文主要介绍如何使用Gallery只滑动一页以及其实现原理. Demo APK 可以方便的查看效果,在各大应用商店搜索 trinea android 下载即可,如:Google Play. 可运行代码 ...
- 分享关于浏览器对象 history对象
window.history.forward() == window.history.go(-1) //返回下一页 window.history.back() == window.history.go ...
- 利用反射实现Servlet公共类的抽取
一次请求的执行过程: 请求:发送请求地址-->到达web.xml中,找到地址对应的servlet类-->通过反射调用该类的构造函数,创建该servlet类的对象-->通过当前对象调用 ...
- nvm安装node流程及报错解决
第一步:下载NVM下载nvm并解压 nvm-window 下载地址:https://github.com/coreybutler/nvm-windows/releases 下载文件,然后解压得到nvm ...
- GCJ1C09C - Bribe the Prisoners
GCJ1C09C - Bribe the Prisoners Problem In a kingdom there are prison cells (numbered 1 to P) built t ...