当前位置:网站首页>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; 
 } 
}
原网站

版权声明
本文为[Simon66991]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240608211298.html