diff --git a/README.md b/README.md
index c809a79..ad19df8 100644
--- a/README.md
+++ b/README.md
@@ -70,6 +70,49 @@ OnDisposeOuterListener.startPhoto();
OnDisposeOuterListener.clear()
```
+**3:ImageDispose image manipulation tool object, providing a series of static methods to manipulate the image or image path.**
+
+```java
+//Converts the input stream into a byte array
+ImageDispose.readStream(InputStream inStream);
+
+//Converted to Bitmap by byte array and BitmapFactory
+ImageDispose.getPicFromBytes(byte[] bytes,BitmapFactory.Options opts);
+
+//Convert to a specified size Bitmap
+ImageDispose.zoomBitmap(Bitmap bitmap, int w, int h);
+
+//Convert bitmap to byte array
+ImageDispose.bitmap2Bytes(Bitmap bm, int quality);
+
+//Cut a specified size Bitmap object
+ImageDispose.cutterBitmap(Bitmap srcBitmap, int limitWidth,int limitHeight);
+
+//Image compression, return bitmap byte array
+ImageDispose.compressBmpFromByte(Bitmap bitmap, long maxsize);
+
+//Image compression, return compressed bitmap
+ImageDispose.compressBmpGetBmp(Bitmap bitmap, long maxsize);
+
+//Get the specified size of Bitmap by address, the quality of compression
+ImageDispose.acquireBitmap(String path, int compressSize);
+
+//Drawable to Bitmap
+ImageDispose.drawableToBitmap(Drawable drawable);
+
+//Drawable The specified size of the zoom
+ImageDispose.zoomDrawable(Drawable drawable, int w, int h);
+
+//Byte array conversion File object
+ImageDispose.acquireByteFile(byte[] byteOne, String filePath);
+
+//Get the image path to cut out the name of the picture, the path needs to be extended
+ImageDispose.getImageName(String path);
+
+//Generate a random unique picture name, the path needs to be extended
+ImageDispose.getImageRandomName(String path);
+```
+
6.0 system above need to add their own dynamic permissions
-----
##### If you feel good, please star give me motivation.
thank you very much.
diff --git a/README_CHINESE.md b/README_CHINESE.md
index 86d5776..2ca369f 100644
--- a/README_CHINESE.md
+++ b/README_CHINESE.md
@@ -66,6 +66,47 @@ OnDisposeOuterListener.startPhoto();
//注:为了防止内存泄漏,请在Activity生命周期onDestroy()调用。
OnDisposeOuterListener.clear()
```
+**3:ImageDispose图片操作工具对象,提供一系列静态方法对图片或者图片路径进行操作。**
+```java
+//通过输入流转换成字节数组
+ImageDispose.readStream(InputStream inStream);
+
+//通过字节数组和BitmapFactory转换成Bitmap
+ImageDispose.getPicFromBytes(byte[] bytes,BitmapFactory.Options opts);
+
+//转换成指定大小的Bitmap
+ImageDispose.zoomBitmap(Bitmap bitmap, int w, int h);
+
+//把bitmap转换成字节数组
+ImageDispose.bitmap2Bytes(Bitmap bm, int quality);
+
+//切割指定大小的Bitmap对象
+ImageDispose.cutterBitmap(Bitmap srcBitmap, int limitWidth,int limitHeight);
+
+//图片压缩,返回bitmap字节数组
+ImageDispose.compressBmpFromByte(Bitmap bitmap, long maxsize);
+
+//图片压缩,返回压缩的bitmap
+ImageDispose.compressBmpGetBmp(Bitmap bitmap, long maxsize);
+
+//通过地址获取指定大小的Bitmap(质量压缩)
+ImageDispose.acquireBitmap(String path, int compressSize);
+
+//Drawable转Bitmap
+ImageDispose.drawableToBitmap(Drawable drawable);
+
+//Drawable 指定大小的缩放
+ImageDispose.zoomDrawable(Drawable drawable, int w, int h);
+
+//字节数组转换File对象
+ImageDispose.acquireByteFile(byte[] byteOne, String filePath);
+
+//获取图片路径切割出来的图片名字,路径需要带扩展名
+ImageDispose.getImageName(String path);
+
+//产生随机唯一图片名称,路径需要带扩展名
+ImageDispose.getImageRandomName(String path);
+```
6.0系统以上需要自己动态添加权限
-----
diff --git a/utils/src/main/java/com/liql/photograph/PhotographDispose.java b/utils/src/main/java/com/liql/photograph/PhotographDispose.java
index 459ba4c..714f7a5 100644
--- a/utils/src/main/java/com/liql/photograph/PhotographDispose.java
+++ b/utils/src/main/java/com/liql/photograph/PhotographDispose.java
@@ -113,13 +113,13 @@ public void run() {
switch (requestCode) {
case SDK_19_BOTTOM:
if (null != data)
- file = getFile(new PhotoSDKBottomListener(activity,
+ file = getFile(new PhotoSDKBottomListener<>(activity,
PhotographDispose.this), data.getData());
break;
case SDK_19_TOP:
if (null != data)
- file = getFile(new PhotoSDKTopListener(activity,
+ file = getFile(new PhotoSDKTopListener<>(activity,
PhotographDispose.this), data.getData());
break;
case SDK_PHOTOGRAPH:
@@ -200,7 +200,7 @@ public File getPhotographDisposeData(String path) {
if (mPhotographConfigura == null)
return null;
- File file = getPath(getImageName(path), mPhotographConfigura.getCompressPath());
+ File file = getPath(ImageDispose.getImageName(path), mPhotographConfigura.getCompressPath());
// 判断用户选择的图片是否已经压缩过
if (!file.exists()) {
Bitmap bitmap = ImageDispose.acquireBitmap(path, 0, 0);
@@ -248,25 +248,12 @@ private File getPath(String imageName, String path) {
}
File file = new File(mSystemPath + File.separator + path);
// 判断存储图片的文件夹是否存在
- if (!file.exists())
+ if (!file.exists()) {
file.mkdirs();
-
+ }
return new File(file.getPath(), imageName);
}
- /**
- * 获取图片路径切割出来的图片名字
- *
- * @param path 图片路径
- * @return
- */
- private String getImageName(String path) {
- String imageName = "";
- String[] split = path.split("/");
- if (null != split)
- imageName = split[split.length - 1];
- return imageName;
- }
/**
* 获取手机存储图片保存路径
@@ -274,7 +261,7 @@ private String getImageName(String path) {
* @return
*/
private String getSystemPath() {
- String path = "";
+ String path;
// 判断是否安装有SD卡
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
diff --git a/utils/src/main/java/com/liql/photograph/utils/ImageDispose.java b/utils/src/main/java/com/liql/photograph/utils/ImageDispose.java
index 2d5fdd4..603a7b3 100644
--- a/utils/src/main/java/com/liql/photograph/utils/ImageDispose.java
+++ b/utils/src/main/java/com/liql/photograph/utils/ImageDispose.java
@@ -22,381 +22,387 @@
import java.net.HttpURLConnection;
import java.net.URL;
+/**
+ * 图片一系列操作处理对象
+ */
public class ImageDispose {
- /**
- * 通过Ulr路径获取图片
- *
- * @param urlStr
- * :图片的Url
- * @return :byte[]
- */
- public static byte[] getImageFromBuffer(String urlStr) throws Exception {
- // 从网络获取,在返回
- URL url = new URL(urlStr);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- conn.setUseCaches(false);
- conn.setConnectTimeout(10 * 1000);
- // 开辟输出流,用于写图片到缓存
- System.out.println("从网络获取图片");
- if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
- System.out.println("从网络获取图片++++++++++++++++");
- return readStream(conn.getInputStream());// 获取图片的输入流
- } else {
- Log.e("网络错误", conn.getErrorStream().toString());
- return null;
- }
- }
+ /**
+ * 通过输入流转换成字节数组
+ *
+ * @param inStream
+ * @return
+ * @throws Exception
+ */
+ public static byte[] readStream(InputStream inStream) throws Exception {
+ byte[] buffer = new byte[1024];
+ int len = -1;
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+ while ((len = inStream.read(buffer)) != -1) {
+ outStream.write(buffer, 0, len);
+ }
+ byte[] data = outStream.toByteArray();
+ outStream.close();
+ inStream.close();
+ System.out.println("data转换" + data == null);
+ return data;
- /**
- * 通过输入流转换成字节数组
- *
- * @param inStream
- * @return
- * @throws Exception
- */
- public static byte[] readStream(InputStream inStream) throws Exception {
- byte[] buffer = new byte[1024];
- int len = -1;
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- while ((len = inStream.read(buffer)) != -1) {
- outStream.write(buffer, 0, len);
- }
- byte[] data = outStream.toByteArray();
- outStream.close();
- inStream.close();
- System.out.println("data转换" + data == null);
- return data;
+ }
- }
+ /**
+ * 通过字节数组和BitmapFactory转换成Bitmap
+ *
+ * @param bytes
+ * @param opts
+ * @return
+ */
+ public static Bitmap getPicFromBytes(byte[] bytes,
+ BitmapFactory.Options opts) {
+ if (bytes != null)
+ if (opts != null) {
+ opts.inJustDecodeBounds = false;
+ return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
+ opts);
+ } else
+ return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
+ return null;
+ }
- /**
- * 通过字节数组和BitmapFactory转换成Bitmap
- *
- * @param bytes
- * @param opts
- * @return
- */
- public static Bitmap getPicFromBytes(byte[] bytes,
- BitmapFactory.Options opts) {
- if (bytes != null)
- if (opts != null) {
- opts.inJustDecodeBounds = false;
- return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
- opts);
- } else
- return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
- return null;
- }
+ /**
+ * 转换成指定大小的Bitmap
+ *
+ * @param bitmap
+ * @param w
+ * @param h
+ * @return
+ */
+ public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
+ int width = bitmap.getWidth();
+ int height = bitmap.getHeight();
+ Matrix matrix = new Matrix();
+ float scaleWidth = ((float) w / width);
+ float scaleHeight = ((float) h / height);
+ matrix.postScale(scaleWidth, scaleHeight);
+ Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
+ matrix, true);
+ return newBmp;
+ }
- /**
- * 转换成指定大小的Bitmap
- *
- * @param bitmap
- * @param w
- * @param h
- * @return
- */
- public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
- int width = bitmap.getWidth();
- int height = bitmap.getHeight();
- Matrix matrix = new Matrix();
- float scaleWidth = ((float) w / width);
- float scaleHeight = ((float) h / height);
- matrix.postScale(scaleWidth, scaleHeight);
- Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
- matrix, true);
- return newBmp;
- }
+ /**
+ * 把bitmap转换成字节数组
+ *
+ * @param bm bitmap
+ * @param quality 压缩质量百分比
+ * @return
+ */
+ public static byte[] bitmap2Bytes(Bitmap bm, int quality) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
+ return baos.toByteArray();
+ }
- /**
- * 把bitmap转换成字节数组
- *
- * @param bm
- * bitmap
- * @param quality
- * 压缩质量百分比
- * @return
- */
- public static byte[] Bitmap2Bytes(Bitmap bm, int quality) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
- return baos.toByteArray();
- }
+ public static void getFileFromBytes(byte[] b, File file) {
+ BufferedOutputStream stream = null;
+ try {
+ FileOutputStream fstream = new FileOutputStream(file);
+ stream = new BufferedOutputStream(fstream);
+ stream.write(b);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ if (stream != null) {
+ try {
+ stream.close();
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ }
+ }
+ }
+ }
- public static void getFileFromBytes(byte[] b, File file) {
- BufferedOutputStream stream = null;
- try {
- FileOutputStream fstream = new FileOutputStream(file);
- stream = new BufferedOutputStream(fstream);
- stream.write(b);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
+ /**
+ * 切割指定大小的Bitmap对象
+ *
+ * @param srcBitmap
+ * @param limitWidth
+ * @param limitHeight
+ * @return
+ */
+ public static Bitmap cutterBitmap(Bitmap srcBitmap, int limitWidth,
+ int limitHeight) {
+ float width = srcBitmap.getWidth();
+ float height = srcBitmap.getHeight();
- /**
- * 切割指定大小的Bitmap对象
- *
- * @param srcBitmap
- * @param limitWidth
- * @param limitHeight
- * @return
- */
- public static Bitmap cutterBitmap(Bitmap srcBitmap, int limitWidth,
- int limitHeight) {
- float width = srcBitmap.getWidth();
- float height = srcBitmap.getHeight();
+ float limitScale = limitWidth / limitHeight;
+ float srcScale = width / height;
- float limitScale = limitWidth / limitHeight;
- float srcScale = width / height;
+ if (limitScale > srcScale) {
+ // 高小了,所以要去掉多余的高度
+ height = limitHeight / ((float) limitWidth / width);
+ } else {
+ // 宽度小了,所以要去掉多余的宽度
+ width = limitWidth / ((float) limitHeight / height);
+ }
- if (limitScale > srcScale) {
- // 高小了,所以要去掉多余的高度
- height = limitHeight / ((float) limitWidth / width);
- } else {
- // 宽度小了,所以要去掉多余的宽度
- width = limitWidth / ((float) limitHeight / height);
- }
+ Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height,
+ Config.ARGB_8888);
+ Canvas canvas = new Canvas(bitmap);
+ canvas.drawBitmap(srcBitmap, 0, 0, new Paint());
+ return bitmap;
+ }
- Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height,
- Config.ARGB_8888);
- Canvas canvas = new Canvas(bitmap);
- canvas.drawBitmap(srcBitmap, 0, 0, new Paint());
- return bitmap;
- }
+ /**
+ * 图片压缩->返回bitmap字节数组
+ *
+ * @param bitmap
+ * @param maxsize 指定多大
+ * @return
+ */
+ public static byte[] compressBmpFromByte(Bitmap bitmap, long maxsize) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ int compress = 100;
+ bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
+ System.out.println("没有压缩前>>>>" + baos.toByteArray().length);
+ while (baos.toByteArray().length > maxsize) {
+ baos.reset();
+ compress -= 10;
+ bitmap.compress(Bitmap.CompressFormat.JPEG, compress, baos);
+ }
+ System.out.println("压缩之后>>>>" + baos.toByteArray().length);
+ return baos.toByteArray();
+ }
- /**
- * 图片压缩->返回bitmap字节数组
- *
- * @param bitmap
- * @param maxsize
- * 指定多大
- * @return
- */
- public static byte[] compressBmpFromByte(Bitmap bitmap, long maxsize) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int compress = 100;
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- System.out.println("没有压缩前>>>>" + baos.toByteArray().length);
- while (baos.toByteArray().length > maxsize) {
- baos.reset();
- compress -= 10;
- bitmap.compress(Bitmap.CompressFormat.JPEG, compress, baos);
- }
- System.out.println("压缩之后>>>>" + baos.toByteArray().length);
- return baos.toByteArray();
- }
+ /**
+ * 图片压缩->返回压缩的bitmap
+ *
+ * @param bitmap
+ * @param maxsize 指定多大
+ * @return
+ */
+ public static Bitmap compressBmpGetBmp(Bitmap bitmap, long maxsize) {
+ if (null != bitmap) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ int compress = 100;
+ bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
+ while (baos.toByteArray().length > maxsize) {
+ baos.reset();
+ compress -= 10;
+ bitmap.compress(Bitmap.CompressFormat.JPEG, compress, baos);
+ }
+ }
+ return bitmap;
+ }
- /**
- * 图片压缩->返回压缩的bitmap
- *
- * @param bitmap
- * @param maxsize
- * 指定多大
- * @return
- */
- public static Bitmap compressBmpGetBmp(Bitmap bitmap, long maxsize) {
- if (null != bitmap) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int compress = 100;
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- while (baos.toByteArray().length > maxsize) {
- baos.reset();
- compress -= 10;
- bitmap.compress(Bitmap.CompressFormat.JPEG, compress, baos);
- }
- }
- return bitmap;
- }
+ /**
+ * 通过地址获取指定大小的Bitmap(没有压缩)
+ *
+ * @param path 地址
+ * @param reqWidth 指定宽(无效果)
+ * @param reqHeight 指定高(无效果)
+ * @return
+ */
+ public static Bitmap acquireBitmap(String path, int reqWidth, int reqHeight) {
+ final BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inDither = false; // 不进行图片抖动处理
+ options.inPreferredConfig = null; // 设置让解码器以最佳方式解码
+ if (reqWidth > 0 || reqHeight > 0) {
+ options.inJustDecodeBounds = true;
+ options.inSampleSize = calculateInSampleSize(options, reqWidth,
+ reqHeight);
+ }
+ options.inJustDecodeBounds = false;
- /**
- * 通过地址获取指定大小的Bitmap(没有压缩)
- *
- * @param path
- * 地址
- * @param reqWidth
- * 指定宽(无效果)
- * @param reqHeight
- * 指定高(无效果)
- * @return
- */
- public static Bitmap acquireBitmap(String path, int reqWidth, int reqHeight) {
- final BitmapFactory.Options options = new BitmapFactory.Options();
- options.inDither = false; // 不进行图片抖动处理
- options.inPreferredConfig = null; // 设置让解码器以最佳方式解码
- if (reqWidth > 0 || reqHeight > 0) {
- options.inJustDecodeBounds = true;
- options.inSampleSize = calculateInSampleSize(options, reqWidth,
- reqHeight);
- }
- options.inJustDecodeBounds = false;
+ return BitmapFactory.decodeFile(path, options);
+ }
- return BitmapFactory.decodeFile(path, options);
- }
+ /**
+ * 通过地址获取指定大小的Bitmap(质量压缩)
+ *
+ * @param path 本地路径
+ * @param compressSize 设置位图缩放比例 width,hight设为原来的四分一(该参数请使用2的整数倍),这也减小了位图占用的内存大小;
+ * 例如,一张
+ * 分辨率为2048*1536px的图像使用inSampleSize值为4的设置来解码,产生的Bitmap大小约为512
+ * *384px。 相较于完整图片占用12M的内存,这种方式只需0.75M内存(假设Bitmap配置为//ARGB_8888)。
+ * @return
+ */
+ public static Bitmap acquireBitmap(String path, int compressSize) {
+ FileInputStream is = null;
+ try {
+ is = new FileInputStream(path);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ }
+ // 2.为位图设置100K的缓存
+ BitmapFactory.Options opts = new BitmapFactory.Options();
+ opts.inTempStorage = new byte[100 * 1024];
+ // 3.设置位图颜色显示优化方式
+ // ALPHA_8:每个像素占用1byte内存(8位)
+ // ARGB_4444:每个像素占用2byte内存(16位)
+ // ARGB_8888:每个像素占用4byte内存(32位)
+ // RGB_565:每个像素占用2byte内存(16位)
+ // Android默认的颜色模式为ARGB_8888,这个颜色模式色彩最细腻,显示质量最高。但同样的,占用的内存//也最大。也就意味着一个像素点占用4个字节的内存。我们来做一个简单的计算题:3200*2400*4
+ // bytes //=30M。如此惊人的数字!哪怕生命周期超不过10s,Android也不会答应的。
+ opts.inPreferredConfig = Config.RGB_565;
+ // 4.设置图片可以被回收,创建Bitmap用于存储Pixel的内存空间在系统内存不足时可以被回收
+ opts.inPurgeable = true;
+ // 5.设置位图缩放比例
+ // width,hight设为原来的四分一(该参数请使用2的整数倍),这也减小了位图占用的内存大小;例如,一张//分辨率为2048*1536px的图像使用inSampleSize值为4的设置来解码,产生的Bitmap大小约为//512*384px。相较于完整图片占用12M的内存,这种方式只需0.75M内存(假设Bitmap配置为//ARGB_8888)。
+ opts.inSampleSize = compressSize;
+ // 6.设置解码位图的尺寸信息
+ opts.inInputShareable = true;
+ // 7.解码位图
+ Bitmap btp = BitmapFactory.decodeStream(is, null, opts);
+ try {
+ if (is != null) {
+ is.close();
+ is = null;
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return btp;
+ }
- /**
- * 通过地址获取指定大小的Bitmap(质量压缩)
- *
- * @param path
- * 本地路径
- * @param compressSize
- * 设置位图缩放比例 width,hight设为原来的四分一(该参数请使用2的整数倍),这也减小了位图占用的内存大小;
- * 例如,一张
- * 分辨率为2048*1536px的图像使用inSampleSize值为4的设置来解码,产生的Bitmap大小约为512
- * *384px。 相较于完整图片占用12M的内存,这种方式只需0.75M内存(假设Bitmap配置为//ARGB_8888)。
- * @return
- */
- public static Bitmap acquireBitmap(String path, int compressSize) {
- FileInputStream is = null;
- try {
- is = new FileInputStream(path);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- // 2.为位图设置100K的缓存
- BitmapFactory.Options opts = new BitmapFactory.Options();
- opts.inTempStorage = new byte[100 * 1024];
- // 3.设置位图颜色显示优化方式
- // ALPHA_8:每个像素占用1byte内存(8位)
- // ARGB_4444:每个像素占用2byte内存(16位)
- // ARGB_8888:每个像素占用4byte内存(32位)
- // RGB_565:每个像素占用2byte内存(16位)
- // Android默认的颜色模式为ARGB_8888,这个颜色模式色彩最细腻,显示质量最高。但同样的,占用的内存//也最大。也就意味着一个像素点占用4个字节的内存。我们来做一个简单的计算题:3200*2400*4
- // bytes //=30M。如此惊人的数字!哪怕生命周期超不过10s,Android也不会答应的。
- opts.inPreferredConfig = Config.RGB_565;
- // 4.设置图片可以被回收,创建Bitmap用于存储Pixel的内存空间在系统内存不足时可以被回收
- opts.inPurgeable = true;
- // 5.设置位图缩放比例
- // width,hight设为原来的四分一(该参数请使用2的整数倍),这也减小了位图占用的内存大小;例如,一张//分辨率为2048*1536px的图像使用inSampleSize值为4的设置来解码,产生的Bitmap大小约为//512*384px。相较于完整图片占用12M的内存,这种方式只需0.75M内存(假设Bitmap配置为//ARGB_8888)。
- opts.inSampleSize = compressSize;
- // 6.设置解码位图的尺寸信息
- opts.inInputShareable = true;
- // 7.解码位图
- Bitmap btp = BitmapFactory.decodeStream(is, null, opts);
- try {
- if (is!=null) {
- is.close();
- is = null;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return btp;
- }
+ // 计算图片的缩放值
+ private static int calculateInSampleSize(BitmapFactory.Options options,
+ int reqWidth, int reqHeight) {
+ final int height = options.outHeight;
+ final int width = options.outWidth;
+ int inSampleSize = 1;
- // 计算图片的缩放值
- private static int calculateInSampleSize(BitmapFactory.Options options,
- int reqWidth, int reqHeight) {
- final int height = options.outHeight;
- final int width = options.outWidth;
- int inSampleSize = 1;
+ if (height > reqHeight || width > reqWidth) {
+ final double heightRatio = Math.ceil((float) height
+ / (float) reqHeight);
+ final double widthRatio = Math.ceil((float) width
+ / (float) reqWidth);
+ inSampleSize = (int) Math.ceil(Math.max(heightRatio, widthRatio));
+ }
+ return inSampleSize;
+ }
- if (height > reqHeight || width > reqWidth) {
- final double heightRatio = Math.ceil((float) height
- / (float) reqHeight);
- final double widthRatio = Math.ceil((float) width
- / (float) reqWidth);
- inSampleSize = (int) Math.ceil(Math.max(heightRatio, widthRatio));
- }
- return inSampleSize;
- }
+ /**
+ * Drawable转Bitmap
+ *
+ * @param drawable
+ * @return
+ */
+ public static Bitmap drawableToBitmap(Drawable drawable) {
+ // 取 drawable 的长宽
+ int w = drawable.getIntrinsicWidth();
+ int h = drawable.getIntrinsicHeight();
- /**
- * Drawable转Bitmap
- *
- * @param drawable
- * @return
- */
- public static Bitmap drawableToBitmap(Drawable drawable) {
- // 取 drawable 的长宽
- int w = drawable.getIntrinsicWidth();
- int h = drawable.getIntrinsicHeight();
+ // 取 drawable 的颜色格式
+ Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888
+ : Config.RGB_565;
+ // 建立对应 bitmap
+ Bitmap bitmap = Bitmap.createBitmap(w, h, config);
+ // 建立对应 bitmap 的画布
+ Canvas canvas = new Canvas(bitmap);
+ drawable.setBounds(0, 0, w, h);
+ // 把 drawable 内容画到画布中
+ drawable.draw(canvas);
+ return bitmap;
+ }
- // 取 drawable 的颜色格式
- Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888
- : Config.RGB_565;
- // 建立对应 bitmap
- Bitmap bitmap = Bitmap.createBitmap(w, h, config);
- // 建立对应 bitmap 的画布
- Canvas canvas = new Canvas(bitmap);
- drawable.setBounds(0, 0, w, h);
- // 把 drawable 内容画到画布中
- drawable.draw(canvas);
- return bitmap;
- }
+ /**
+ * Drawable 指定大小的缩放
+ *
+ * @param drawable
+ * @param w
+ * @param h
+ * @return
+ */
+ public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
+ int width = drawable.getIntrinsicWidth();
+ int height = drawable.getIntrinsicHeight();
+ // drawable转换成bitmap
+ Bitmap oldbmp = drawableToBitmap(drawable);
+ // 创建操作图片用的Matrix对象
+ Matrix matrix = new Matrix();
+ // 计算缩放比例
+ float sx = ((float) w / width);
+ float sy = ((float) h / height);
+ // 设置缩放比例
+ matrix.postScale(sx, sy);
+ // 建立新的bitmap,其内容是对原bitmap的缩放后的图
+ Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
+ matrix, true);
+ return new BitmapDrawable(newbmp);
+ }
- /**
- * Drawable 指定大小的缩放
- *
- * @param drawable
- * @param w
- * @param h
- * @return
- */
- public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
- int width = drawable.getIntrinsicWidth();
- int height = drawable.getIntrinsicHeight();
- // drawable转换成bitmap
- Bitmap oldbmp = drawableToBitmap(drawable);
- // 创建操作图片用的Matrix对象
- Matrix matrix = new Matrix();
- // 计算缩放比例
- float sx = ((float) w / width);
- float sy = ((float) h / height);
- // 设置缩放比例
- matrix.postScale(sx, sy);
- // 建立新的bitmap,其内容是对原bitmap的缩放后的图
- Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
- matrix, true);
- return new BitmapDrawable(newbmp);
- }
+ /**
+ * 字节数组转换File对象
+ *
+ * @param byteOne
+ * @param filePath
+ * @return
+ * @throws Exception
+ */
+ public static File acquireByteFile(byte[] byteOne, String filePath) {
+ File file = null;
+ FileOutputStream fos = null;
+ BufferedOutputStream bos = null;
+ try {
+ file = new File(filePath);
+ fos = new FileOutputStream(file);
+ bos = new BufferedOutputStream(fos);
+ bos.write(byteOne);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ if (fos != null) {
+ try {
+ fos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (bos != null) {
+ try {
+ bos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
- /**
- * 字节数组转换File对象
- *
- * @param byteOne
- * @param filePath
- * @return
- * @throws Exception
- */
- public static File acquireByteFile(byte[] byteOne, String filePath) {
- File file = null;
- FileOutputStream fos = null;
- BufferedOutputStream bos = null;
- try {
- file = new File(filePath);
- fos = new FileOutputStream(file);
- bos = new BufferedOutputStream(fos);
- bos.write(byteOne);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (bos != null) {
- try {
- bos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
+ return file;
+ }
- return file;
- }
+ /**
+ * 获取图片路径切割出来的图片名字
+ *
+ * @param path 图片路径-->需要带扩展名
+ * @return
+ */
+ public static String getImageName(String path) {
+ String imageName = "";
+ String[] split = path.split("/");
+ if (split.length > 0) {
+ imageName = split[split.length - 1];
+ }
+ return imageName;
+ }
+ /**
+ * 产生随机唯一图片名称
+ *
+ * @param path 图片路径-->需要带扩展名
+ * @return
+ */
+ public static String getImageRandomName(String path) {
+ String randomName = System.currentTimeMillis() + "";
+ String imageName = getImageName(path);
+ if (!"".equals(imageName.trim())) {
+ String[] split = imageName.split("\\.");
+ if (split.length > 1) {
+ String extendName = split[split.length - 1];
+ if (!"".equals(split[split.length - 1].trim())) {
+ randomName += "." + extendName;
+ }
+ }
+ } else {
+ randomName += ".jpg";
+ }
+ return randomName;
+ }
}