package com.wzkj.vzone.org.util;
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
/**
* Tools for handler picture
*
* @author Ryan.Tang
*
*/
public final class ImageTools {
/**
* Save image to the SD card
* @param photoBitmap
* @param photoName
* @param path
*/
public static void savePhotoToSDCard(Bitmap photoBitmap,String path,String photoName){
if (checkSDCardAvailable()) {
File photoFile =
new File(path , photoName);
FileOutputStream fileOutputStream =
null;
try {
fileOutputStream =
new FileOutputStream(photoFile);
if (photoBitmap !=
null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG,
100, fileOutputStream)) {
fileOutputStream.flush();
}
}
}
catch (Exception e) {
photoFile.delete();
e.printStackTrace();
}
finally{
try {
fileOutputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void savePhotoToSDCard(Bitmap photoBitmap,String path){
if (checkSDCardAvailable()) {
File photoFile =
new File(path);
FileOutputStream fileOutputStream =
null;
try {
fileOutputStream =
new FileOutputStream(photoFile);
if (photoBitmap !=
null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG,
100, fileOutputStream)) {
fileOutputStream.flush();
}
}
}
catch (Exception e) {
photoFile.delete();
e.printStackTrace();
}
finally{
try {
fileOutputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* Check the SD card
* @return
*/
public static boolean checkSDCardAvailable(){
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 根据路径加载bitmap
*
* @param path
* 路径
* @param w
* 款
* @param h
* 长
* @return
*/
public static final Bitmap
convertToBitmap(String path,
int w,
int h) {
try {
BitmapFactory.Options opts =
new BitmapFactory.Options();
opts.inJustDecodeBounds =
true;
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.decodeFile(path, opts);
int width = opts.outWidth;
int height = opts.outHeight;
float scaleWidth =
0.f, scaleHeight =
0.f;
if (width > w || height > h) {
scaleWidth = ((
float) width) / w;
scaleHeight = ((
float) height) / h;
}
opts.inJustDecodeBounds =
false;
float scale = Math.max(scaleWidth, scaleHeight);
opts.inSampleSize = (
int) scale;
WeakReference<Bitmap> weak =
new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
Bitmap bMapRotate = Bitmap.createBitmap(weak.get(),
0,
0, weak.get().getWidth(), weak.get().getHeight(),
null,
true);
if (bMapRotate !=
null) {
return bMapRotate;
}
return null;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String
getPath(
final Context context,
final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
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];
}
}
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);
}
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 = MediaStore.MediaColumns._ID +
"=?";
final String[] selectionArgs =
new String[] { split[
1] };
return getDataColumn(context, contentUri, selection,
selectionArgs);
}
}
else if (
"content".equalsIgnoreCase(uri.getScheme())) {
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri,
null,
null);
}
else if (
"file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* 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.
*/
public static String
getDataColumn(Context context, Uri uri,
String selection, String[] selectionArgs) {
Cursor cursor =
null;
final String column = MediaStore.MediaColumns.DATA;
final String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection,
selection, selectionArgs,
null);
if (cursor !=
null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
}
finally {
if (cursor !=
null)
cursor.close();
}
return null;
}
/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public 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.
*/
public 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.
*/
public 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.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri
.getAuthority());
}
/**
* @param url
* @return
*/
public static Bitmap
getLoacalBitmap(String url) {
try {
FileInputStream fis =
new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
}
catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* Get bitmap from specified image path
*
* @param imgPath
* @return
*/
public static Bitmap
getBitmap(String imgPath) {
BitmapFactory.Options newOpts =
new BitmapFactory.Options();
newOpts.inJustDecodeBounds =
false;
newOpts.inPurgeable =
true;
newOpts.inInputShareable =
true;
newOpts.inSampleSize =
1;
newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
return BitmapFactory.decodeFile(imgPath, newOpts);
}
/**
* Store bitmap into specified image path
*
* @param bitmap
* @param outPath
* @throws FileNotFoundException
*/
public static void storeImage(Bitmap bitmap, String outPath)
throws FileNotFoundException {
FileOutputStream os =
new FileOutputStream(outPath);
bitmap.compress(Bitmap.CompressFormat.JPEG,
100, os);
}
/**
* Compress image by pixel, this will modify image width/height.
* Used to get thumbnail
*
* @param imgPath image path
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @return
*/
public Bitmap
ratio(String imgPath,
float pixelW,
float pixelH) {
BitmapFactory.Options newOpts =
new BitmapFactory.Options();
newOpts.inJustDecodeBounds =
true;
newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeFile(imgPath,newOpts);
newOpts.inJustDecodeBounds =
false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = pixelH;
float ww = pixelW;
int be =
1;
if (w > h && w > ww) {
be = (
int) (newOpts.outWidth / ww);
}
else if (w < h && h > hh) {
be = (
int) (newOpts.outHeight / hh);
}
if (be <=
0) be =
1;
newOpts.inSampleSize = be;
bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
return bitmap;
}
/**
* Compress image by size, this will modify image width/height.
* Used to get thumbnail
*
* @param image
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @return
*/
public static Bitmap
ratio(Bitmap image,
float pixelW,
float pixelH) {
ByteArrayOutputStream os =
new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG,
100, os);
if( os.toByteArray().length /
1024>
1024) {
os.reset();
image.compress(Bitmap.CompressFormat.JPEG,
50, os);
}
ByteArrayInputStream is =
new ByteArrayInputStream(os.toByteArray());
BitmapFactory.Options newOpts =
new BitmapFactory.Options();
newOpts.inJustDecodeBounds =
true;
newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeStream(is,
null, newOpts);
newOpts.inJustDecodeBounds =
false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = pixelH;
float ww = pixelW;
int be =
1;
if (w > h && w > ww) {
be = (
int) (newOpts.outWidth / ww);
}
else if (w < h && h > hh) {
be = (
int) (newOpts.outHeight / hh);
}
if (be <=
0) be =
1;
newOpts.inSampleSize = be;
is =
new ByteArrayInputStream(os.toByteArray());
bitmap = BitmapFactory.decodeStream(is,
null, newOpts);
return bitmap;
}
/**
* Compress by quality, and generate image to the path specified
*
* @param image
* @param outPath
* @param maxSize target will be compressed to be smaller than this size.(kb)
* @throws IOException
*/
public static void compressAndGenImage(Bitmap image, String outPath,
int maxSize)
throws IOException {
ByteArrayOutputStream os =
new ByteArrayOutputStream();
int options =
100;
image.compress(Bitmap.CompressFormat.PNG, options, os);
while ( os.toByteArray().length /
1024 > maxSize) {
os.reset();
options -=
10;
image.compress(Bitmap.CompressFormat.PNG, options, os);
}
FileOutputStream fos =
new FileOutputStream(outPath);
fos.write(os.toByteArray());
fos.flush();
fos.close();
}
/**
* Compress by quality, and generate image to the path specified
*
* @param imgPath
* @param outPath
* @param maxSize target will be compressed to be smaller than this size.(kb)
* @param needsDelete Whether delete original file after compress
* @throws IOException
*/
public void compressAndGenImage(String imgPath, String outPath,
int maxSize,
boolean needsDelete)
throws IOException {
compressAndGenImage(getBitmap(imgPath), outPath, maxSize);
if (needsDelete) {
File file =
new File (imgPath);
if (file.exists()) {
file.delete();
}
}
}
/**
* Ratio and generate thumb to the path specified
*
* @param image
* @param outPath
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @throws FileNotFoundException
*/
public static void ratioAndGenThumb(Bitmap image, String outPath,
float pixelW,
float pixelH)
throws FileNotFoundException {
Bitmap bitmap = ratio(image, pixelW, pixelH);
storeImage( bitmap, outPath);
}
/**
* Ratio and generate thumb to the path specified
*
* @param outPath
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @param needsDelete Whether delete original file after compress
* @throws FileNotFoundException
*/
public void ratioAndGenThumb(String imgPath, String outPath,
float pixelW,
float pixelH,
boolean needsDelete)
throws FileNotFoundException {
Bitmap bitmap = ratio(imgPath, pixelW, pixelH);
storeImage( bitmap, outPath);
if (needsDelete) {
File file =
new File (imgPath);
if (file.exists()) {
file.delete();
}
}
}
public static void compressBmpToFile(Bitmap bmp,File file){
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
int options =
80;
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
while (baos.toByteArray().length /
1024 >
100) {
baos.reset();
options -=
10;
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
try {
FileOutputStream fos =
new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
private Bitmap
compressImageFromFile(String srcPath) {
BitmapFactory.Options newOpts =
new BitmapFactory.Options();
newOpts.inJustDecodeBounds =
true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
newOpts.inJustDecodeBounds =
false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh =
800f;
float ww =
480f;
int be =
1;
if (w > h && w > ww) {
be = (
int) (newOpts.outWidth / ww);
}
else if (w < h && h > hh) {
be = (
int) (newOpts.outHeight / hh);
}
if (be <=
0)
be =
1;
newOpts.inSampleSize = be;
newOpts.inPreferredConfig = Bitmap.Config.ARGB_8888;
newOpts.inPurgeable =
true;
newOpts.inInputShareable =
true;
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return bitmap;
}
private Bitmap
compressBmpFromBmp(Bitmap image) {
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
int options =
100;
image.compress(Bitmap.CompressFormat.JPEG,
100, baos);
while (baos.toByteArray().length /
1024 >
100) {
baos.reset();
options -=
10;
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
ByteArrayInputStream isBm =
new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(isBm,
null,
null);
return bitmap;
}
}