当前位置:网站首页>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;
}
}
边栏推荐
- os. path. Pits encountered during the use of join()
- How to mount a USB hard disk with NTFS file format under RHEL5 system
- 新技术实战,一步步用Activity Results API封装权限申请库
- Tencent conference API - get rest API & webhook application docking information
- A preliminary study of IO model
- Common misconceptions in Tencent conference API - signature error_ code 200003
- Why do you want to file? What are the advantages and disadvantages of website filing?
- One development skill a day: how to establish P2P communication based on webrtc?
- Shell basic operator -- arithmetic operator
- Introduction to RCNN, fast RCNN and fast RCNN
猜你喜欢

ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language

Two methods of QT exporting PDF files

JUC personal simple notes

Base64编码详解及其变种(解决加号在URL变空格问题)

独立站运营中如何提升客户留存率?客户细分很重要!

2021-03-16 comp9021 class 9 notes

A preliminary study of IO model

OpenCV to realize the basic transformation of image

MySQL 因字符集问题插入中文数据时提示代码 :1366

ZUCC_编译语言原理与编译_实验05 正则表达式、有限自动机、词法分析
随机推荐
2021-03-04 comp9021 class 6 notes
ZUCC_ Principles of compiling language and compilation_ Experiment 01 language analysis and introduction
How to replace the web player easyplayerproactivex Key in OCX?
ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析
Redis cluster data skew
定时备份数据库脚本
Use cpulimit to free up your CPU
Common CVM transcribes audio using virtual sound card
05 Ubuntu installing mysql8
利用sonar做代码检查
MySQL 因字符集问题插入中文数据时提示代码 :1366
Win10 cloud, add Vietnamese
RuntimeError: Missing dependencies:XXX
Tencent conference API - get rest API & webhook application docking information
mysql组合索引的有序性
jwt(json web token)
Two methods of QT exporting PDF files
ZUCC_编译语言原理与编译_实验04 语言与文法
Application of tidb in Netease games
Maya re deployment