当前位置:网站首页>Picture tools
Picture tools
2022-06-24 08:42:00 【Simon66991】
public class PhotoBitmapUtils {
/** * Folder where pictures are taken */
private static final String FILES_NAME = "/MyPhoto";
/** * Acquired time format */
public static final String TIME_STYLE = "yyyyMMddHHmmss";
/** * Types of pictures */
public static final String IMAGE_TYPE = ".png";
// Prevent instantiation
private PhotoBitmapUtils() {
}
/** * Get the path that the phone can store * * @param context Context * @return The phone can store paths */
private static String getPhoneRootPath(Context context) {
// Is there a SD card
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
|| !Environment.isExternalStorageRemovable()) {
// obtain SD Kagan catalogue
return context.getExternalCacheDir().getPath();
} else {
// obtain apk Cache path under the package
return context.getCacheDir().getPath();
}
}
/** * Use the current system time as the name of the uploaded image * * @return The root path of the storage + Image name */
public static String getPhotoFileName(Context context) {
File file = new File(getPhoneRootPath(context) + FILES_NAME);
// Determine if the file already exists , Create if it does not exist
if (!file.exists()) {
file.mkdirs();
}
// Set the picture file name
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;
}
/** * preservation Bitmap Picture in SD In the card * without SD The card is stored in the mobile phone * * @param mbitmap What needs to be preserved Bitmap picture * @return The path of the returned image when saving successfully , Return... On failure null */
public static String savePhotoToSD(Bitmap mbitmap, Context context) {
FileOutputStream outStream = null;
String fileName = getPhotoFileName(context);
try {
outStream = new FileOutputStream(fileName);
// Write the data to a file ,100 Means no compression
mbitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
return fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (outStream != null) {
// Remember to close the stream !
outStream.close();
}
if (mbitmap != null) {
mbitmap.recycle();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/** * Press the original drawing 1/10 Proportional compression of * * @param path The path of the original graph * @return Compressed image */
public static Bitmap getCompressPhoto(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 10; // The size of the picture is set to one tenth of its original size
Bitmap bmp = BitmapFactory.decodeFile(path, options);
options = null;
return bmp;
}
/** * Process rotated images * @param originpath The original path * @param context Context * @return Return to the image path after repair */
public static String amendRotatePhoto(String originpath, Context context) {
// Get the rotation angle of the picture
int angle = readPictureDegree(originpath);
// Compress the original image to get Bitmap object
Bitmap bmp = getCompressPhoto(originpath);;
// Fix the rotation angle of the picture
Bitmap bitmap = rotaingImageView(angle, bmp);
// Save the repaired image and return to the saved image path
return savePhotoToSD(bitmap, context);
}
/** * Read the rotation angle of the photo * * @param path Photo path * @return angle */
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;
}
/** * Rotate the picture * @param angle The angle of rotation * @param bitmap Image objects * @return The rotated picture */
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
Bitmap returnBm = null;
// According to the rotation angle , Generate rotation matrix
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
// Rotate the original image according to the rotation matrix , And get a new picture
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;
}
}
边栏推荐
- 利用ngrok做内网穿透
- 【微服务~Nacos】Nacos服务提供者和服务消费者
- RuntimeError: Missing dependencies:XXX
- Shell basic operator -- arithmetic operator
- ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language
- 常用日期格式符与Qt获取当前时间的办法
- [acnoi2022] I have done it, but I can't
- Promise usage scenarios
- Using sonar for code checking
- win11在cmder中使用vim查看内容的时候空白
猜你喜欢

A preliminary study of IO model

一文详解|增长那些事儿

ZUCC_ Principles of compiling language and compilation_ Experiment 08 parsing LR parsing
![[micro services ~nacos] Nacos service providers and service consumers](/img/b7/47ecd6979ccfeb270261681d6130be.png)
[micro services ~nacos] Nacos service providers and service consumers

2021-03-16 comp9021 class 9 notes

ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis

Qt 中发送自定义事件

数据库迁移从PostgreSQL迁移到 MYSQL

Maya re deployment

Vscode install the remote -wsl plug-in to connect to the local WSL
随机推荐
Shell basic operator -- arithmetic operator
ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析
Qt导出PDF文件的两种方法
Application of tidb in Netease games
小黑ai4code代码baseline啃食1
[explain the difference between operation and maintenance and network engineering]
ZUCC_编译语言原理与编译_实验01 语言分析与简介
uniapp 热更新后台管理
Use cpulimit to free up your CPU
There was an error checking the latest version of pip
[xinliu-s6 new model +sa 3-star Xinghai] the new two-way server of the third generation chip was launched and the product was updated~
Fund raising, trading and registration
ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis
liunx服务器 telnet 带用户名 端口登陆方法
Easydss anonymous live channel data volume instability optimization scheme sharing
利用sonar做代码检查
日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
There was an error checking the latest version of pip
Battle history between redis and me under billion level traffic
单目双视三维坐标确定