点击查看原文

项目中用到的关于图片的处理

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牛人职业发展经验:

最新文章

  1. Mybatis传入参数类型为Map
  2. Python学习记录day5
  3. 【GO】GO语言学习笔记二
  4. 使用 CSS3 动感的图片标题动画效果【附源码下载】
  5. Windows Log4日志发送到ElasticSearch
  6. C# 异步操作 async await 的用法
  7. is not in the sudoers file.This incident will be reported
  8. Java并发编程之ThreadLocal类
  9. Swift - 程序进入后台,以及应用终止时调用的方法
  10. 【Android进阶】使用第三方平台ShareSDK实现新浪微博的一键分享功能
  11. IOS 利用图片设置背景
  12. VB6之断点续传
  13. CSS背景图片
  14. 本机不安装Oracle客户端,使用PL/SQL Developer和 Instant Client 工具包连接oracle 11g远程数据库
  15. C# 读取 appconfig文件配置数据库连接字符串,和配置文件
  16. Retrofit2 原理解析
  17. 2.在demo bag上运行cartographer ROS
  18. Dos命令快速设置ip、网关、dns地址
  19. 2016-2017-2 20155326实验二《Java面向对象程序设计》实验报告
  20. 浅谈一致性哈希(My转)

热门文章

  1. idea+spring4+springmvc+mybatis+maven实现简单增删改查CRUD
  2. 题解 P2330 【[SCOI2005]繁忙的都市】
  3. Opencv Mat的三种常用类型简介
  4. Maven的SSH搭建以及部署
  5. PostgreSQL数据库创建/删除
  6. [2012山东省第三届ACM大学生程序设计竞赛]——Mine Number
  7. 用Hexo搭建个人博客
  8. 使用INSERT…SELECT语法插入记录(三十二)
  9. org.w3c.dom.Document 与org.dom4j.Document互转
  10. rowcount和@@rowcount的区别