package com.changim.patient.app.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.Gravity;
import android.widget.Toast; import java.io.File; /**
* Created by zzw on 2016/2/23 0023.
*/
public class ImageUtils { /**
* 根据图片的Uri得到它的路径
*
* @param activity
* @param imageUri
* @return 返回null表示不能找到此图片
*/
public static String getImagePathFromImageUri(Activity activity, Uri imageUri) { String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = activity.getContentResolver().query(imageUri, filePathColumn, null, null, null); if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imagePath = cursor.getString(columnIndex);
cursor.close(); if (imagePath == null || imagePath.equals("null")) {
Toast toast = Toast.makeText(activity, "不能发现图片", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return null;
} return imagePath;
} else {
File file = new File(imageUri.getPath());
if (!file.exists()) {
Toast toast = Toast.makeText(activity, "不能发现图片", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return null;
}
return file.getAbsolutePath();
}
} /**
* 根据图片的路径得到图片资源(压缩后)
* 如果targetW或者targetH为0就自动压缩
*
* @param path
* @param
* @return 压缩后的图片
*/
public static Bitmap getYaSuoBitmapFromImagePath(String path, int targetW, int targetH) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = calculateInSampleSize(options);
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
Bitmap src = BitmapFactory.decodeFile(path, options); if (src == null) {
return null;
}
Bitmap bitmap = null; if (targetH == 0 || targetW == 0) {
bitmap = Bitmap.createScaledBitmap(src, width / inSampleSize, height / inSampleSize, false);
} else {
bitmap = Bitmap.createScaledBitmap(src, targetW, targetH, false);
} if (src != bitmap) {
src.recycle();
} return bitmap;
} /**
* 计算压缩比
*
* @param options
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options) {
int height = options.outHeight;
int width = options.outWidth; int min = height > width ? width : height;
int inSampleSize = min / 400; if (inSampleSize == 0) return 1; return inSampleSize;
} /**
* 根据图片的路径得到该图片在表中的ID
*
* @param cr
* @param fileName
* @return
*/
public static String getImageIdFromPath(ContentResolver cr, String fileName) { //select condition.
String whereClause = MediaStore.Images.Media.DATA + " = '" + fileName + "'"; //colection of results.
Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, whereClause, null, null);
if (cursor == null || cursor.getCount() == 0) {
if (cursor != null)
cursor.close();
return null;
}
cursor.moveToFirst();
//imageView id in imageView table.
String imageId = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID));
cursor.close();
if (imageId == null) {
return null;
}
return imageId;
} /**
* 根据图片的ID得到缩略图
*
* @param cr
* @param imageId
* @return
*/
public static Bitmap getThumbnailsFromImageId(ContentResolver cr, String imageId) {
if (imageId == null || imageId.equals(""))
return null; Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888; long imageIdLong = Long.parseLong(imageId);
//via imageid get the bimap type thumbnail in thumbnail table.
bitmap = MediaStore.Images.Thumbnails.getThumbnail(cr, imageIdLong, MediaStore.Images.Thumbnails.MINI_KIND, options); return bitmap;
} /**
* 获得圆角图片的方法
*
* @param bitmap 源Bitmap
* @param roundPx 圆角大小
* @return 期望Bitmap
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.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(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint); return output;
}
}

最新文章

  1. 在Linux上运行ASP.NET vNext
  2. Ubuntu下的解压缩
  3. linux vps安装kloxo配置全部过程
  4. 发布自己的包到Nuget上
  5. python 简单爬虫diy
  6. makefile_2
  7. Mysqldump参数大全
  8. AngularJS recursive(递归)
  9. c# MVC在WEB.Config中配置MIME
  10. OC基础(3)
  11. Android JNI之调用JAVA方法的返回类型签名
  12. COJN 0483 800501求最大非空子矩阵
  13. 9.5 在 C# 中使用 F# 库
  14. ORA-28002 -- oracle密码过期
  15. solr6.6初探之主从同步
  16. [Swift]LeetCode263. 丑数 | Ugly Number
  17. 天融信资料下载官方FTP服务器
  18. CS229 6.4 Neurons Networks Autoencoders and Sparsity
  19. linux常用命令:df 命令
  20. redis删除单个key和多个key,ssdb会落地导致重启redis无法清除缓存

热门文章

  1. oracle修改连接数后无法启动(信号量的问题)
  2. Python之简单函数练习(Day30)
  3. django用户信息扩展
  4. springMVC中使用 RequestBody 及 Ajax POST请求 415 (Unsupported Media Type)
  5. Python基础(9)_生成器(yield表达式形式)、面向过程编程
  6. 前端 css续
  7. 每天一个Linux命令(46)ifconfig命令
  8. 【Head First Servlets and JSP】笔记6:什么是响应首部 & 快速搭建一个简单的测试环境
  9. samtools的基本用法
  10. Go make 和 new的区别