当前位置:网站首页>图片工具
图片工具
2022-06-24 07:02:00 【Simon66991】
public class PhotoBitmapUtils {
/** * 存放拍摄图片的文件夹 */
private static final String FILES_NAME = "/MyPhoto";
/** * 获取的时间格式 */
public static final String TIME_STYLE = "yyyyMMddHHmmss";
/** * 图片种类 */
public static final String IMAGE_TYPE = ".png";
// 防止实例化
private PhotoBitmapUtils() {
}
/** * 获取手机可存储路径 * * @param context 上下文 * @return 手机可存储路径 */
private static String getPhoneRootPath(Context context) {
// 是否有SD卡
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
|| !Environment.isExternalStorageRemovable()) {
// 获取SD卡根目录
return context.getExternalCacheDir().getPath();
} else {
// 获取apk包下的缓存路径
return context.getCacheDir().getPath();
}
}
/** * 使用当前系统时间作为上传图片的名称 * * @return 存储的根路径+图片名称 */
public static String getPhotoFileName(Context context) {
File file = new File(getPhoneRootPath(context) + FILES_NAME);
// 判断文件是否已经存在,不存在则创建
if (!file.exists()) {
file.mkdirs();
}
// 设置图片文件名称
SimpleDateFormat format = new SimpleDateFormat(TIME_STYLE, Locale.getDefault());
Date date = new Date(System.currentTimeMillis());
String time = format.format(date);
String photoName = "/" + time + IMAGE_TYPE;
return file + photoName;
}
/** * 保存Bitmap图片在SD卡中 * 如果没有SD卡则存在手机中 * * @param mbitmap 需要保存的Bitmap图片 * @return 保存成功时返回图片的路径,失败时返回null */
public static String savePhotoToSD(Bitmap mbitmap, Context context) {
FileOutputStream outStream = null;
String fileName = getPhotoFileName(context);
try {
outStream = new FileOutputStream(fileName);
// 把数据写入文件,100表示不压缩
mbitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
return fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (outStream != null) {
// 记得要关闭流!
outStream.close();
}
if (mbitmap != null) {
mbitmap.recycle();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/** * 把原图按1/10的比例压缩 * * @param path 原图的路径 * @return 压缩后的图片 */
public static Bitmap getCompressPhoto(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 10; // 图片的大小设置为原来的十分之一
Bitmap bmp = BitmapFactory.decodeFile(path, options);
options = null;
return bmp;
}
/** * 处理旋转后的图片 * @param originpath 原图路径 * @param context 上下文 * @return 返回修复完毕后的图片路径 */
public static String amendRotatePhoto(String originpath, Context context) {
// 取得图片旋转角度
int angle = readPictureDegree(originpath);
// 把原图压缩后得到Bitmap对象
Bitmap bmp = getCompressPhoto(originpath);;
// 修复图片被旋转的角度
Bitmap bitmap = rotaingImageView(angle, bmp);
// 保存修复后的图片并返回保存后的图片路径
return savePhotoToSD(bitmap, context);
}
/** * 读取照片旋转角度 * * @param path 照片路径 * @return 角度 */
public 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;
}
/** * 旋转图片 * @param angle 被旋转角度 * @param bitmap 图片对象 * @return 旋转后的图片 */
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
Bitmap returnBm = null;
// 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
}
if (returnBm == null) {
returnBm = bitmap;
}
if (bitmap != returnBm) {
bitmap.recycle();
}
return returnBm;
}
}
边栏推荐
- 11-- longest substring without repeated characters
- ZUCC_编译语言原理与编译_实验05 正则表达式、有限自动机、词法分析
- Question 4 - datepicker date selector, disabling two date selectors (start and end dates)
- A preliminary study of IO model
- MAYA重新拓布
- [real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
- Tencent cloud ASR product PHP realizes real-time voice authentication request
- AUTO PWN
- [micro services ~nacos] Nacos service providers and service consumers
- Redis cluster data skew
猜你喜欢

Redis cluster data skew

李白最经典的20首诗排行榜

问题4 — DatePicker日期选择器,2个日期选择器(开始、结束日期)的禁用

LabVIEW finds prime numbers in an array of n elements

日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用

Permission model DAC ACL RBAC ABAC

Qt导出PDF文件的两种方法

OpenCV to realize the basic transformation of image

ZUCC_编译语言原理与编译_实验06 07 语法分析 LL 分析

Markdown 实现文内链接跳转
随机推荐
ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
ZUCC_编译语言原理与编译_实验05 正则表达式、有限自动机、词法分析
Common date formatter and QT method for obtaining current time
Easydss anonymous live channel data volume instability optimization scheme sharing
[real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
【无标题】
Fundamentals of 3D mathematics [17] inverse square theorem
How to implement approval function in Tekton
pyQt 常用系统的事件
Qt导出PDF文件的两种方法
Promise的使用场景
根据网络上的视频的m3u8文件通过ffmpeg进行合成视频
The article takes you to understand the security of Windows operating system and protect your computer from infringement
Matlab求解线性方程组Ax=b
uniapp 热更新后台管理
Micro build low code online "quick registration applet" capability
orb slam build bug: undefined reference to symbol ‘_ZN5boost6system15system_categoryEv‘
LabVIEW finds prime numbers in an array of n elements
贷款五级分类
Two methods of QT exporting PDF files