| package com.doumee.keyCabinet.utils.ewm; | 
|   | 
| import android.annotation.SuppressLint; | 
| import android.app.Activity; | 
| import android.app.AlertDialog; | 
| import android.content.ContentUris; | 
| import android.content.Context; | 
| import android.content.DialogInterface; | 
| import android.content.Intent; | 
| import android.database.Cursor; | 
| import android.graphics.Bitmap; | 
| import android.graphics.BitmapFactory; | 
| import android.hardware.Camera; | 
| import android.net.Uri; | 
| import android.os.Build; | 
| import android.os.Environment; | 
| import android.provider.DocumentsContract; | 
| import android.provider.MediaStore; | 
| import android.text.TextUtils; | 
| import android.util.TypedValue; | 
|   | 
| import com.doumee.keyCabinet.R; | 
| import com.google.zxing.BinaryBitmap; | 
| import com.google.zxing.ChecksumException; | 
| import com.google.zxing.DecodeHintType; | 
| import com.google.zxing.MultiFormatReader; | 
| import com.google.zxing.NotFoundException; | 
| import com.google.zxing.RGBLuminanceSource; | 
| import com.google.zxing.Reader; | 
| import com.google.zxing.Result; | 
| import com.google.zxing.common.HybridBinarizer; | 
|   | 
| import java.io.UnsupportedEncodingException; | 
| import java.lang.reflect.Field; | 
| import java.nio.charset.Charset; | 
| import java.util.Hashtable; | 
|   | 
| public class Utils { | 
|   | 
|     public static final int SELECT_PIC_KITKAT = 1001; | 
|     public static final int SELECT_PIC = 1002; | 
|     public static final String BAR_CODE = "barCode"; | 
|     private static Camera camera; | 
|   | 
|     // dp转px | 
|     public static float dp2px(Context context, float dpValue) { | 
|         return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, | 
|                 dpValue, context.getResources().getDisplayMetrics()); | 
|     } | 
|   | 
|     // 相机打开出错弹框 | 
|     public static void displayFrameworkBugMessageAndExit(final Activity activity) { | 
|         AlertDialog.Builder builder = new AlertDialog.Builder(activity); | 
|         builder.setTitle(activity.getString(R.string.app_name)); | 
|         builder.setMessage("相机打开出错,请稍后重试"); | 
|   | 
|         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { | 
|             @Override | 
|             public void onClick(DialogInterface dialog, int which) { | 
|                 setResultAndFinish(activity, Activity.RESULT_CANCELED, null); | 
|             } | 
|   | 
|         }); | 
|         builder.setOnCancelListener(new DialogInterface.OnCancelListener() { | 
|   | 
|             @Override | 
|             public void onCancel(DialogInterface dialog) { | 
|                 setResultAndFinish(activity, Activity.RESULT_CANCELED, null); | 
|             } | 
|         }); | 
|         builder.show(); | 
|     } | 
|   | 
|     public static void setResultAndFinish(Activity activity, int resultCode, Intent data) { | 
|         activity.setResult(resultCode, data); | 
|         activity.finish(); | 
|     } | 
|   | 
|     // 获取状态栏高度 | 
|     public static int getStatusBarHeight(Context context) { | 
|         try { | 
|             @SuppressLint("PrivateApi") Class<?> c = Class.forName("com.android.internal.R$dimen"); | 
|             Object obj = c.newInstance(); | 
|             Field field = c.getField("status_bar_height"); | 
|             int x = Integer.parseInt(field.get(obj).toString()); | 
|             return context.getResources().getDimensionPixelSize(x); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|         } | 
|         return 0; | 
|     } | 
|   | 
|     // 跳转到图片选择 | 
|     public static void openAlbum(Activity activity) { | 
|         Intent intent = new Intent(Intent.ACTION_PICK); | 
|         intent.setType("image/*"); | 
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | 
|             activity.startActivityForResult(intent, SELECT_PIC_KITKAT); | 
|         } else { | 
|             activity.startActivityForResult(intent, SELECT_PIC); | 
|         } | 
|     } | 
|   | 
|     // 图片识别 | 
|     public static Result scanningImage(String path) { | 
|         if (TextUtils.isEmpty(path)) { | 
|             return null; | 
|         } | 
|         // DecodeHintType 和 EncodeHintType | 
|         Hashtable<DecodeHintType, String> hints = new Hashtable<>(); | 
|         hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); // 设置二维码内容的编码 | 
|         BitmapFactory.Options options = new BitmapFactory.Options(); | 
| //        options.inJustDecodeBounds = true; // 先获取原大小 | 
|         options.inJustDecodeBounds = false; // 获取新的大小 | 
|         int sampleSize = (int) (options.outHeight / (float) 200); | 
|         if (sampleSize <= 0) { | 
|             sampleSize = 1; | 
|         } | 
|         options.inSampleSize = sampleSize; | 
|         Bitmap scanBitmap = BitmapFactory.decodeFile(path, options); | 
|         int[] intArray = new int[scanBitmap.getWidth() * scanBitmap.getHeight()]; | 
|         scanBitmap.getPixels(intArray, 0, scanBitmap.getWidth(), 0, 0, scanBitmap.getWidth(), scanBitmap.getHeight()); | 
|         RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), intArray); | 
|         BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); | 
|         Reader reader = new MultiFormatReader(); | 
|         try { | 
|             return reader.decode(bitmap1, hints); | 
|         } catch (NotFoundException | ChecksumException e) { | 
|             e.printStackTrace(); | 
|         } catch (com.google.zxing.FormatException e) { | 
|             e.printStackTrace(); | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     // 中文乱码处理 | 
|     public static String recode(String str) { | 
|         String formart = ""; | 
|         try { | 
|             boolean ISO = Charset.forName("ISO-8859-1").newEncoder().canEncode(str); | 
|             if (ISO) { | 
|                 formart = new String(str.getBytes("ISO-8859-1"), "GB2312"); | 
|             } else { | 
|                 formart = str; | 
|             } | 
|         } catch (UnsupportedEncodingException e) { | 
|             e.printStackTrace(); | 
|         } | 
|         return formart; | 
|     } | 
|   | 
|     // 得到图片路径 | 
|     public static String getPath(final Context context, final Uri uri) { | 
|   | 
|         final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; | 
|   | 
|         // DocumentProvider | 
|         if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { | 
|             // ExternalStorageProvider | 
|             if (isExternalStorageDocument(uri)) { | 
|                 final String docId = DocumentsContract.getDocumentId(uri); | 
|                 final String[] split = docId.split(":"); | 
|                 final String type = split[0]; | 
|   | 
|                 if ("primary".equalsIgnoreCase(type)) { | 
|                     return Environment.getExternalStorageDirectory() + "/" + split[1]; | 
|                 } | 
|             } | 
|             // DownloadsProvider | 
|             else if (isDownloadsDocument(uri)) { | 
|   | 
|                 final String id = DocumentsContract.getDocumentId(uri); | 
|                 final Uri contentUri = ContentUris.withAppendedId( | 
|                         Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); | 
|   | 
|                 return getDataColumn(context, contentUri, null, null); | 
|             } | 
|             // MediaProvider | 
|             else if (isMediaDocument(uri)) { | 
|                 final String docId = DocumentsContract.getDocumentId(uri); | 
|                 final String[] split = docId.split(":"); | 
|                 final String type = split[0]; | 
|   | 
|                 Uri contentUri = null; | 
|                 if ("image".equals(type)) { | 
|                     contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; | 
|                 } else if ("video".equals(type)) { | 
|                     contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; | 
|                 } else if ("audio".equals(type)) { | 
|                     contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; | 
|                 } | 
|   | 
|                 final String selection = "_id=?"; | 
|                 final String[] selectionArgs = new String[]{ | 
|                         split[1] | 
|                 }; | 
|   | 
|                 return getDataColumn(context, contentUri, selection, selectionArgs); | 
|             } | 
|         } | 
|         // MediaStore (and general) | 
|         else if ("content".equalsIgnoreCase(uri.getScheme())) { | 
|   | 
|             // Return the remote address | 
|             if (isGooglePhotosUri(uri)) { | 
|                 return uri.getLastPathSegment(); | 
|             } | 
|   | 
|             return getDataColumn(context, uri, null, null); | 
|         } | 
|         // File | 
|         else if ("file".equalsIgnoreCase(uri.getScheme())) { | 
|             return uri.getPath(); | 
|         } | 
|   | 
|         return null; | 
|     } | 
|   | 
|     /** | 
|      * @param uri The Uri to check. | 
|      * @return Whether the Uri authority is ExternalStorageProvider. | 
|      */ | 
|     private static boolean isExternalStorageDocument(Uri uri) { | 
|         return "com.android.externalstorage.documents".equals(uri.getAuthority()); | 
|     } | 
|   | 
|     /** | 
|      * @param uri The Uri to check. | 
|      * @return Whether the Uri authority is DownloadsProvider. | 
|      */ | 
|     private static boolean isDownloadsDocument(Uri uri) { | 
|         return "com.android.providers.downloads.documents".equals(uri.getAuthority()); | 
|     } | 
|   | 
|     /** | 
|      * @param uri The Uri to check. | 
|      * @return Whether the Uri authority is MediaProvider. | 
|      */ | 
|     private static boolean isMediaDocument(Uri uri) { | 
|         return "com.android.providers.media.documents".equals(uri.getAuthority()); | 
|     } | 
|   | 
|     /** | 
|      * @param uri The Uri to check. | 
|      * @return Whether the Uri authority is Google Photos. | 
|      */ | 
|     private static boolean isGooglePhotosUri(Uri uri) { | 
|         return "com.google.android.apps.photos.content".equals(uri.getAuthority()); | 
|     } | 
|   | 
|     /** | 
|      * Get the value of the data column for this Uri. This is useful for | 
|      * MediaStore Uris, and other file-based ContentProviders. | 
|      * | 
|      * @param context       The context. | 
|      * @param uri           The Uri to query. | 
|      * @param selection     (Optional) Filter used in the query. | 
|      * @param selectionArgs (Optional) Selection arguments used in the query. | 
|      * @return The value of the _data column, which is typically a file path. | 
|      */ | 
|     private static String getDataColumn(Context context, Uri uri, String selection, | 
|                                         String[] selectionArgs) { | 
|   | 
|         final String column = "_data"; | 
|         final String[] projection = { | 
|                 column | 
|         }; | 
|   | 
|         try (Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, | 
|                 null)) { | 
|             if (cursor != null && cursor.moveToFirst()) { | 
|                 final int index = cursor.getColumnIndexOrThrow(column); | 
|                 return cursor.getString(index); | 
|             } | 
|         } | 
|         return null; | 
|     } | 
| } |