d
This commit is contained in:
750
app/src/main/java/com/fenghoo/seven/utils/AbImageUtil.java
Normal file
750
app/src/main/java/com/fenghoo/seven/utils/AbImageUtil.java
Normal file
@@ -0,0 +1,750 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package com.fenghoo.seven.utils;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Bitmap.Config;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Matrix;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.graphics.PorterDuff.Mode;
|
||||
import android.graphics.PorterDuffXfermode;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.Base64;
|
||||
import android.view.View;
|
||||
import android.view.View.MeasureSpec;
|
||||
import android.widget.ImageView;
|
||||
|
||||
|
||||
import com.fenghoo.seven.R;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
|
||||
/**
|
||||
* 描述:图片处理类.
|
||||
*
|
||||
*/
|
||||
public class AbImageUtil {
|
||||
|
||||
/**
|
||||
* The tag.
|
||||
*/
|
||||
private static String TAG = "AbImageUtil";
|
||||
|
||||
|
||||
|
||||
private static OnImageUtilsListener onImageUtilsListener;
|
||||
|
||||
public void setOnImageUtilsListener(OnImageUtilsListener listener) {
|
||||
onImageUtilsListener = listener;
|
||||
}
|
||||
|
||||
public interface OnImageUtilsListener {
|
||||
void backBitmap(Bitmap bt);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述:缩放图片.压缩
|
||||
*
|
||||
* @param file File对象
|
||||
* @param newWidth 新图片的宽
|
||||
* @param newHeight 新图片的高
|
||||
* @return Bitmap 新图片
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public static Bitmap scaleImg(File file, int newWidth, int newHeight) {
|
||||
Bitmap resizeBmp = null;
|
||||
try {
|
||||
BitmapFactory.Options opts = new BitmapFactory.Options();
|
||||
// 设置为true,decodeFile先不创建内存 只获取一些解码边界信息即图片大小信息
|
||||
opts.inJustDecodeBounds = true;
|
||||
// BitmapFactory.decodeFile(file.getPath(), opts);
|
||||
BitmapFactory.decodeFileDescriptor(new FileInputStream(file.getPath()).getFD(), null, opts);
|
||||
if (newWidth != -1 && newHeight != -1) {
|
||||
// inSampleSize=2表示图片宽高都为原来的二分之一,即图片为原来的四分之一
|
||||
// 缩放可以将像素点打薄
|
||||
int srcWidth = opts.outWidth; // 获取图片的原始宽度
|
||||
int srcHeight = opts.outHeight;// 获取图片原始高度
|
||||
int destWidth = 0;
|
||||
int destHeight = 0;
|
||||
// 缩放的比例
|
||||
double ratio = 0.0;
|
||||
if (srcWidth < newWidth || srcHeight < newHeight) {
|
||||
ratio = 0.0;
|
||||
destWidth = srcWidth;
|
||||
destHeight = srcHeight;
|
||||
// 按比例计算缩放后的图片大小
|
||||
} else if (srcWidth > srcHeight) {
|
||||
ratio = (double) srcWidth / newWidth;
|
||||
destWidth = newWidth;
|
||||
destHeight = (int) (srcHeight / ratio);
|
||||
} else {
|
||||
ratio = (double) srcHeight / newHeight;
|
||||
destHeight = newHeight;
|
||||
destWidth = (int) (srcWidth / ratio);
|
||||
}
|
||||
// 缩放的比例,缩放是很难按准备的比例进行缩放的,目前我只发现只能通过inSampleSize来进行缩放,其值表明缩放的倍数,SDK中建议其值是2的指数值
|
||||
opts.inSampleSize = (int) ratio + 1;
|
||||
// 设置大小
|
||||
opts.outHeight = destHeight;
|
||||
opts.outWidth = destWidth;
|
||||
} else {
|
||||
opts.inSampleSize = 1;
|
||||
}
|
||||
// 创建内存
|
||||
opts.inJustDecodeBounds = false;
|
||||
// 使图片不抖动
|
||||
opts.inDither = false;
|
||||
resizeBmp = BitmapFactory.decodeFileDescriptor(new FileInputStream(file.getPath()).getFD(), null, opts);
|
||||
// resizeBmp = BitmapFactory.decodeFile(file.getPath(), opts);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return resizeBmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述:缩放图片,不压缩的缩放.
|
||||
*
|
||||
* @param bitmap the bitmap
|
||||
* @param newWidth 新图片的宽
|
||||
* @param newHeight 新图片的高
|
||||
* @return Bitmap 新图片
|
||||
*/
|
||||
public static Bitmap scaleImg(Bitmap bitmap, int newWidth, int newHeight) {
|
||||
if (bitmap == null) {
|
||||
return null;
|
||||
}
|
||||
if (newHeight <= 0 || newWidth <= 0) {
|
||||
return bitmap;
|
||||
}
|
||||
// 获得图片的宽高
|
||||
int width = bitmap.getWidth();
|
||||
int height = bitmap.getHeight();
|
||||
|
||||
if (width <= 0 || height <= 0) {
|
||||
return null;
|
||||
}
|
||||
// 计算缩放比例
|
||||
float scaleWidth = ((float) newWidth) / width;
|
||||
float scaleHeight = ((float) newHeight) / height;
|
||||
// 取得想要缩放的matrix参数
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.postScale(scaleWidth, scaleHeight);
|
||||
// 得到新的图片
|
||||
Bitmap newBm = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
|
||||
return newBm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述:缩放图片.
|
||||
*
|
||||
* @param bitmap the bitmap
|
||||
* @param scale 比例
|
||||
* @return Bitmap 新图片
|
||||
*/
|
||||
public static Bitmap scaleImg(Bitmap bitmap, float scale) {
|
||||
Bitmap resizeBmp = null;
|
||||
try {
|
||||
// 获取Bitmap资源的宽和高
|
||||
int bmpW = bitmap.getWidth();
|
||||
int bmpH = bitmap.getHeight();
|
||||
// 注意这个Matirx是android.graphics底下的那个
|
||||
Matrix mt = new Matrix();
|
||||
// 设置缩放系数,分别为原来的0.8和0.8
|
||||
mt.postScale(scale, scale);
|
||||
resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bmpW, bmpH, mt, true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return resizeBmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述:裁剪图片.
|
||||
*
|
||||
* @param file File对象
|
||||
* @param newWidth 新图片的宽
|
||||
* @param newHeight 新图片的高
|
||||
* @return Bitmap 新图片
|
||||
*/
|
||||
public static Bitmap cutImg(File file, int newWidth, int newHeight) {
|
||||
Bitmap newBitmap = null;
|
||||
try {
|
||||
BitmapFactory.Options opts = new BitmapFactory.Options();
|
||||
// 设置为true,decodeFile先不创建内存 只获取一些解码边界信息即图片大小信息
|
||||
opts.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeFileDescriptor(new FileInputStream(file.getPath()).getFD(), null, opts);
|
||||
// BitmapFactory.decodeFile(file.getPath(), opts);
|
||||
if (newWidth != -1 && newHeight != -1) {
|
||||
// inSampleSize=2表示图片宽高都为原来的二分之一,即图片为原来的四分之一
|
||||
// 缩放可以将像素点打薄,裁剪前将图片缩放一些
|
||||
int srcWidth = opts.outWidth; // 获取图片的原始宽度
|
||||
int srcHeight = opts.outHeight;// 获取图片原始高度
|
||||
int destWidth = 0;
|
||||
int destHeight = 0;
|
||||
int cutSrcWidth = newWidth * 2;
|
||||
int cutSrcHeight = newHeight * 2;
|
||||
|
||||
// 缩放的比例
|
||||
double ratio = 0.0;
|
||||
if (srcWidth < cutSrcWidth || srcHeight < cutSrcHeight) {
|
||||
ratio = 0.0;
|
||||
destWidth = srcWidth;
|
||||
destHeight = srcHeight;
|
||||
// 按比例计算缩放后的图片大小
|
||||
} else if (srcWidth > srcHeight) {
|
||||
ratio = (double) srcWidth / cutSrcWidth;
|
||||
destWidth = cutSrcWidth;
|
||||
destHeight = (int) (srcHeight / ratio);
|
||||
} else {
|
||||
ratio = (double) srcHeight / cutSrcHeight;
|
||||
destHeight = cutSrcHeight;
|
||||
destWidth = (int) (srcWidth / ratio);
|
||||
}
|
||||
// 缩放的比例,缩放是很难按准备的比例进行缩放的,目前我只发现只能通过inSampleSize来进行缩放,其值表明缩放的倍数,SDK中建议其值是2的指数值
|
||||
opts.inSampleSize = (int) ratio + 1;
|
||||
// 设置大小
|
||||
opts.outHeight = destHeight;
|
||||
opts.outWidth = destWidth;
|
||||
} else {
|
||||
opts.inSampleSize = 1;
|
||||
}
|
||||
// 创建内存
|
||||
opts.inJustDecodeBounds = false;
|
||||
// 使图片不抖动
|
||||
opts.inDither = false;
|
||||
// Bitmap resizeBmp = BitmapFactory.decodeFile(file.getPath(), opts);
|
||||
Bitmap resizeBmp = BitmapFactory.decodeFileDescriptor(new FileInputStream(file.getPath()).getFD(), null, opts);
|
||||
if (resizeBmp != null) {
|
||||
newBitmap = cutImg(resizeBmp, newWidth, newHeight);
|
||||
}
|
||||
if (newBitmap != null) {
|
||||
return newBitmap;
|
||||
} else {
|
||||
return resizeBmp;
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return newBitmap;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return newBitmap;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述:裁剪图片.
|
||||
*
|
||||
* @param bitmap the bitmap
|
||||
* @param newWidth 新图片的宽
|
||||
* @param newHeight 新图片的高
|
||||
* @return Bitmap 新图片
|
||||
*/
|
||||
public static Bitmap cutImg(Bitmap bitmap, int newWidth, int newHeight) {
|
||||
if (bitmap == null) {
|
||||
return null;
|
||||
}
|
||||
Bitmap newBitmap = null;
|
||||
if (newHeight <= 0 || newWidth <= 0) {
|
||||
return bitmap;
|
||||
}
|
||||
int width = bitmap.getWidth();
|
||||
int height = bitmap.getHeight();
|
||||
|
||||
if (width <= 0 || height <= 0) {
|
||||
return null;
|
||||
}
|
||||
int offsetX = 0;
|
||||
int offsetY = 0;
|
||||
|
||||
if (width > newWidth) {
|
||||
offsetX = (width - newWidth) / 2;
|
||||
}
|
||||
if (height > newHeight) {
|
||||
offsetY = (height - newHeight) / 2;
|
||||
}
|
||||
|
||||
newBitmap = Bitmap.createBitmap(bitmap, offsetX, offsetY, newWidth, newHeight);
|
||||
return newBitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawable转Bitmap.
|
||||
*
|
||||
* @param drawable 要转化的Drawable
|
||||
* @return Bitmap
|
||||
*/
|
||||
public static Bitmap drawableToBitmap(Drawable drawable) {
|
||||
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888 : Config.RGB_565);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
|
||||
drawable.draw(canvas);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawable对象转换Bitmap对象.
|
||||
*
|
||||
* @param bitmap 要转化的Bitmap对象
|
||||
* @return Drawable 转化完成的Drawable对象
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static Drawable bitmapToDrawable(Bitmap bitmap) {
|
||||
BitmapDrawable mBitmapDrawable = null;
|
||||
try {
|
||||
if (bitmap == null) {
|
||||
return null;
|
||||
}
|
||||
mBitmapDrawable = new BitmapDrawable(bitmap);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return mBitmapDrawable;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Bitmap转换为byte[].
|
||||
*
|
||||
* @param bitmap the bitmap
|
||||
* @param mCompressFormat 图片格式 Bitmap.CompressFormat.JPEG,CompressFormat.PNG
|
||||
* @param needRecycle 是否需要回收
|
||||
* @return byte[] 图片的byte[]
|
||||
*/
|
||||
public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat, final boolean needRecycle) {
|
||||
byte[] result = null;
|
||||
ByteArrayOutputStream output = null;
|
||||
try {
|
||||
output = new ByteArrayOutputStream();
|
||||
bitmap.compress(mCompressFormat, 100, output);
|
||||
result = output.toByteArray();
|
||||
if (needRecycle) {
|
||||
bitmap.recycle();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (output != null) {
|
||||
try {
|
||||
output.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述:将byte[]转换为Bitmap.
|
||||
*
|
||||
* @param b 图片格式的byte[]数组
|
||||
* @return bitmap 得到的Bitmap
|
||||
*/
|
||||
public static Bitmap bytes2Bimap(byte[] b) {
|
||||
Bitmap bitmap = null;
|
||||
try {
|
||||
if (b.length != 0) {
|
||||
bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将ImageView转换为Bitmap.
|
||||
*
|
||||
* @param view 要转换为bitmap的View
|
||||
* @return byte[] 图片的byte[]
|
||||
*/
|
||||
public static Bitmap imageView2Bitmap(ImageView view) {
|
||||
Bitmap bitmap = null;
|
||||
try {
|
||||
bitmap = Bitmap.createBitmap(view.getDrawingCache());
|
||||
view.setDrawingCacheEnabled(false);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将View转换为Drawable.需要最上层布局为Linearlayout
|
||||
*
|
||||
* @param view 要转换为Drawable的View
|
||||
* @return BitmapDrawable Drawable
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static Drawable view2Drawable(View view) {
|
||||
BitmapDrawable mBitmapDrawable = null;
|
||||
try {
|
||||
Bitmap newbmp = view2Bitmap(view);
|
||||
if (newbmp != null) {
|
||||
mBitmapDrawable = new BitmapDrawable(newbmp);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return mBitmapDrawable;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将View转换为Bitmap.需要最上层布局为Linearlayout
|
||||
*
|
||||
* @param view 要转换为bitmap的View
|
||||
* @return byte[] 图片的byte[]
|
||||
*/
|
||||
public static Bitmap view2Bitmap(View view) {
|
||||
Bitmap bitmap = null;
|
||||
try {
|
||||
if (view != null) {
|
||||
view.setDrawingCacheEnabled(true);
|
||||
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
|
||||
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
|
||||
view.buildDrawingCache();
|
||||
bitmap = view.getDrawingCache();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将View转换为byte[].
|
||||
*
|
||||
* @param view 要转换为byte[]的View
|
||||
* @param compressFormat the compress format
|
||||
* @return byte[] View图片的byte[]
|
||||
*/
|
||||
public static byte[] view2Bytes(View view, Bitmap.CompressFormat compressFormat) {
|
||||
byte[] b = null;
|
||||
try {
|
||||
Bitmap bitmap = AbImageUtil.view2Bitmap(view);
|
||||
b = AbImageUtil.bitmap2Bytes(bitmap, compressFormat, true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述:旋转Bitmap为一定的角度.
|
||||
*
|
||||
* @param bitmap the bitmap
|
||||
* @param degrees the degrees
|
||||
* @return the bitmap
|
||||
*/
|
||||
public static Bitmap rotateBitmap(Bitmap bitmap, float degrees) {
|
||||
Bitmap mBitmap = null;
|
||||
try {
|
||||
Matrix m = new Matrix();
|
||||
m.setRotate(degrees % 360);
|
||||
mBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, false);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return mBitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述:旋转Bitmap为一定的角度并四周暗化处理.
|
||||
*
|
||||
* @param bitmap the bitmap
|
||||
* @param degrees the degrees
|
||||
* @return the bitmap
|
||||
*/
|
||||
public static Bitmap rotateBitmapTranslate(Bitmap bitmap, float degrees) {
|
||||
Bitmap mBitmap = null;
|
||||
int width;
|
||||
int height;
|
||||
try {
|
||||
Matrix matrix = new Matrix();
|
||||
if ((degrees / 90) % 2 != 0) {
|
||||
width = bitmap.getWidth();
|
||||
height = bitmap.getHeight();
|
||||
} else {
|
||||
width = bitmap.getHeight();
|
||||
height = bitmap.getWidth();
|
||||
}
|
||||
int cx = width / 2;
|
||||
int cy = height / 2;
|
||||
matrix.preTranslate(-cx, -cy);
|
||||
matrix.postRotate(degrees);
|
||||
matrix.postTranslate(cx, cy);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return mBitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换图片转换成圆形.
|
||||
*
|
||||
* @param bitmap 传入Bitmap对象
|
||||
* @return the bitmap
|
||||
*/
|
||||
public static Bitmap toRoundBitmap(Bitmap bitmap) {
|
||||
if (bitmap == null) {
|
||||
return null;
|
||||
}
|
||||
int width = bitmap.getWidth();
|
||||
int height = bitmap.getHeight();
|
||||
float roundPx;
|
||||
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
|
||||
if (width <= height) {
|
||||
roundPx = width / 2;
|
||||
top = 0;
|
||||
bottom = width;
|
||||
left = 0;
|
||||
right = width;
|
||||
height = width;
|
||||
dst_left = 0;
|
||||
dst_top = 0;
|
||||
dst_right = width;
|
||||
dst_bottom = width;
|
||||
} else {
|
||||
roundPx = height / 2;
|
||||
float clip = (width - height) / 2;
|
||||
left = clip;
|
||||
right = width - clip;
|
||||
top = 0;
|
||||
bottom = height;
|
||||
width = height;
|
||||
dst_left = 0;
|
||||
dst_top = 0;
|
||||
dst_right = height;
|
||||
dst_bottom = height;
|
||||
}
|
||||
|
||||
Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(output);
|
||||
final int color = 0xff424242;
|
||||
final Paint paint = new Paint();
|
||||
final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
|
||||
final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
|
||||
final RectF rectF = new RectF(dst);
|
||||
paint.setAntiAlias(true);
|
||||
canvas.drawARGB(0, 0, 0, 0);
|
||||
paint.setColor(color);
|
||||
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
|
||||
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
|
||||
canvas.drawBitmap(bitmap, src, dst, paint);
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static byte[] imagePathToByte(String path) {
|
||||
byte[] data = null;
|
||||
FileInputStream input = null;
|
||||
try {
|
||||
input = new FileInputStream(new File(path));
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
byte[] buf = new byte[1024];
|
||||
int numBytesRead = 0;
|
||||
while ((numBytesRead = input.read(buf)) != -1) {
|
||||
output.write(buf, 0, numBytesRead);
|
||||
}
|
||||
data = output.toByteArray();
|
||||
output.close();
|
||||
input.close();
|
||||
} catch (FileNotFoundException ex1) {
|
||||
ex1.printStackTrace();
|
||||
} catch (IOException ex1) {
|
||||
ex1.printStackTrace();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param bitmap 原图
|
||||
* @param edgeLength 希望得到的正方形部分的边长
|
||||
* @return 缩放截取正中部分后的位图。
|
||||
*/
|
||||
public static Bitmap centerSquareScaleBitmap(Bitmap bitmap, int edgeLength) {
|
||||
if (null == bitmap || edgeLength <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Bitmap result = bitmap;
|
||||
int widthOrg = bitmap.getWidth();
|
||||
int heightOrg = bitmap.getHeight();
|
||||
|
||||
if (widthOrg > edgeLength && heightOrg > edgeLength) {
|
||||
// 压缩到一个最小长度是edgeLength的bitmap
|
||||
int longerEdge = edgeLength * Math.max(widthOrg, heightOrg) / Math.min(widthOrg, heightOrg);
|
||||
int scaledWidth = widthOrg > heightOrg ? longerEdge : edgeLength;
|
||||
int scaledHeight = widthOrg > heightOrg ? edgeLength : longerEdge;
|
||||
Bitmap scaledBitmap;
|
||||
|
||||
try {
|
||||
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 从图中截取正中间的正方形部分。
|
||||
int xTopLeft = (scaledWidth - edgeLength) / 2;
|
||||
int yTopLeft = (scaledHeight - edgeLength) / 2;
|
||||
|
||||
try {
|
||||
result = Bitmap.createBitmap(scaledBitmap, xTopLeft, yTopLeft, edgeLength, edgeLength);
|
||||
scaledBitmap.recycle();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bitmap 原图
|
||||
* @param edgeLength 希望得到的正方形部分的边长
|
||||
* @return 缩放截取上面部分后的位图。
|
||||
*/
|
||||
public static Bitmap topSquareScaleBitmap(Bitmap bitmap, int edgeLength) {
|
||||
if (null == bitmap || edgeLength <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Bitmap result = bitmap;
|
||||
int widthOrg = bitmap.getWidth();
|
||||
int heightOrg = bitmap.getHeight();
|
||||
|
||||
if (widthOrg > edgeLength && heightOrg > edgeLength) {
|
||||
// 压缩到一个最小长度是edgeLength的bitmap
|
||||
int longerEdge = edgeLength * Math.max(widthOrg, heightOrg) / Math.min(widthOrg, heightOrg);
|
||||
int scaledWidth = widthOrg > heightOrg ? longerEdge : edgeLength;
|
||||
int scaledHeight = widthOrg > heightOrg ? edgeLength : longerEdge;
|
||||
Bitmap scaledBitmap;
|
||||
|
||||
try {
|
||||
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 从图中截取正中间的正方形部分。
|
||||
int xTopLeft = (scaledWidth - edgeLength) / 2;
|
||||
// int yTopLeft = (scaledHeight - edgeLength) / 2;
|
||||
int yTopLeft = 0;
|
||||
|
||||
try {
|
||||
result = Bitmap.createBitmap(scaledBitmap, xTopLeft, yTopLeft, edgeLength, edgeLength);
|
||||
scaledBitmap.recycle();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换Resources图片转换成Bitmap.
|
||||
*/
|
||||
public static Bitmap ResourcesToBitmap(Resources resId) {
|
||||
Bitmap bmp = BitmapFactory.decodeResource(resId, R.mipmap.ic_launcher);
|
||||
return bmp;
|
||||
}
|
||||
/**
|
||||
* bitmap转为base64
|
||||
* @param bitmap
|
||||
* @return
|
||||
*/
|
||||
public static String bitmapToBase64(Bitmap bitmap) {
|
||||
|
||||
String result = null;
|
||||
ByteArrayOutputStream baos = null;
|
||||
try {
|
||||
if (bitmap != null) {
|
||||
baos = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
|
||||
|
||||
baos.flush();
|
||||
baos.close();
|
||||
|
||||
byte[] bitmapBytes = baos.toByteArray();
|
||||
result ="data:image/jpg;base64," + Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (baos != null) {
|
||||
baos.flush();
|
||||
baos.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* base64转为bitmap
|
||||
* @param base64Data
|
||||
* @return
|
||||
*/
|
||||
public static Bitmap base64ToBitmap(String base64Data) {
|
||||
byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
|
||||
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
||||
}
|
||||
/**
|
||||
* bitmap转成string
|
||||
*
|
||||
* @param bitmap
|
||||
* @return
|
||||
*/
|
||||
public static String bitmapToString(Bitmap bitmap)
|
||||
{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
|
||||
byte[] appicon = baos.toByteArray();// 转为byte数组
|
||||
return Base64.encodeToString(appicon, Base64.DEFAULT);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* string转成bitmap
|
||||
*
|
||||
* @param st
|
||||
*/
|
||||
public static Bitmap stringToBitmap(String st)
|
||||
{
|
||||
Bitmap bitmap = null;
|
||||
try
|
||||
{
|
||||
byte[] bitmapArray;
|
||||
bitmapArray = Base64.decode(st, Base64.DEFAULT);
|
||||
bitmap =
|
||||
BitmapFactory.decodeByteArray(bitmapArray, 0,
|
||||
bitmapArray.length);
|
||||
return bitmap;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
54
app/src/main/java/com/fenghoo/seven/utils/CloseUtils.java
Normal file
54
app/src/main/java/com/fenghoo/seven/utils/CloseUtils.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package com.fenghoo.seven.utils;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* author: Blankj
|
||||
* blog : http://blankj.com
|
||||
* time : 2016/10/09
|
||||
* desc : 关闭相关工具类
|
||||
* </pre>
|
||||
*/
|
||||
public final class CloseUtils {
|
||||
|
||||
private CloseUtils() {
|
||||
throw new UnsupportedOperationException("u can't instantiate me...");
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭IO
|
||||
*
|
||||
* @param closeables closeables
|
||||
*/
|
||||
public static void closeIO(final Closeable... closeables) {
|
||||
if (closeables == null) return;
|
||||
for (Closeable closeable : closeables) {
|
||||
if (closeable != null) {
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 安静关闭IO
|
||||
*
|
||||
* @param closeables closeables
|
||||
*/
|
||||
public static void closeIOQuietly(final Closeable... closeables) {
|
||||
if (closeables == null) return;
|
||||
for (Closeable closeable : closeables) {
|
||||
if (closeable != null) {
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
674
app/src/main/java/com/fenghoo/seven/utils/FileIOUtils.java
Normal file
674
app/src/main/java/com/fenghoo/seven/utils/FileIOUtils.java
Normal file
@@ -0,0 +1,674 @@
|
||||
package com.fenghoo.seven.utils;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* author: Blankj
|
||||
* blog : http://blankj.com
|
||||
* time : 2017/06/22
|
||||
* desc : 文件读写相关工具类
|
||||
* </pre>
|
||||
*/
|
||||
public final class FileIOUtils {
|
||||
|
||||
private FileIOUtils() {
|
||||
throw new UnsupportedOperationException("u can't instantiate me...");
|
||||
}
|
||||
|
||||
private static final String LINE_SEP = System.getProperty("line.separator");
|
||||
|
||||
private static int sBufferSize = 8192;
|
||||
|
||||
/**
|
||||
* 将输入流写入文件
|
||||
*
|
||||
* @param filePath 路径
|
||||
* @param is 输入流
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromIS(final String filePath, final InputStream is) {
|
||||
return writeFileFromIS(getFileByPath(filePath), is, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将输入流写入文件
|
||||
*
|
||||
* @param filePath 路径
|
||||
* @param is 输入流
|
||||
* @param append 是否追加在文件末
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromIS(final String filePath, final InputStream is, final boolean append) {
|
||||
return writeFileFromIS(getFileByPath(filePath), is, append);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将输入流写入文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param is 输入流
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromIS(final File file, final InputStream is) {
|
||||
return writeFileFromIS(file, is, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将输入流写入文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param is 输入流
|
||||
* @param append 是否追加在文件末
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromIS(final File file, final InputStream is, final boolean append) {
|
||||
if (!createOrExistsFile(file) || is == null) return false;
|
||||
OutputStream os = null;
|
||||
try {
|
||||
os = new BufferedOutputStream(new FileOutputStream(file, append));
|
||||
byte data[] = new byte[sBufferSize];
|
||||
int len;
|
||||
while ((len = is.read(data, 0, sBufferSize)) != -1) {
|
||||
os.write(data, 0, len);
|
||||
}
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
CloseUtils.closeIO(is, os);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param bytes 字节数组
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByStream(final String filePath, final byte[] bytes) {
|
||||
return writeFileFromBytesByStream(getFileByPath(filePath), bytes, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param bytes 字节数组
|
||||
* @param append 是否追加在文件末
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByStream(final String filePath, final byte[] bytes, final boolean append) {
|
||||
return writeFileFromBytesByStream(getFileByPath(filePath), bytes, append);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param bytes 字节数组
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByStream(final File file, final byte[] bytes) {
|
||||
return writeFileFromBytesByStream(file, bytes, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param bytes 字节数组
|
||||
* @param append 是否追加在文件末
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByStream(final File file, final byte[] bytes, final boolean append) {
|
||||
if (bytes == null || !createOrExistsFile(file)) return false;
|
||||
BufferedOutputStream bos = null;
|
||||
try {
|
||||
bos = new BufferedOutputStream(new FileOutputStream(file, append));
|
||||
bos.write(bytes);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
CloseUtils.closeIO(bos);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param bytes 字节数组
|
||||
* @param isForce 是否写入文件
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByChannel(final String filePath, final byte[] bytes, final boolean isForce) {
|
||||
return writeFileFromBytesByChannel(getFileByPath(filePath), bytes, false, isForce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param bytes 字节数组
|
||||
* @param append 是否追加在文件末
|
||||
* @param isForce 是否写入文件
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByChannel(final String filePath, final byte[] bytes, final boolean append, final boolean isForce) {
|
||||
return writeFileFromBytesByChannel(getFileByPath(filePath), bytes, append, isForce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param bytes 字节数组
|
||||
* @param isForce 是否写入文件
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByChannel(final File file, final byte[] bytes, final boolean isForce) {
|
||||
return writeFileFromBytesByChannel(file, bytes, false, isForce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param bytes 字节数组
|
||||
* @param append 是否追加在文件末
|
||||
* @param isForce 是否写入文件
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByChannel(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
|
||||
if (bytes == null) return false;
|
||||
FileChannel fc = null;
|
||||
try {
|
||||
fc = new FileOutputStream(file, append).getChannel();
|
||||
fc.position(fc.size());
|
||||
fc.write(ByteBuffer.wrap(bytes));
|
||||
if (isForce) fc.force(true);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
CloseUtils.closeIO(fc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param bytes 字节数组
|
||||
* @param isForce 是否写入文件
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByMap(final String filePath, final byte[] bytes, final boolean isForce) {
|
||||
return writeFileFromBytesByMap(filePath, bytes, false, isForce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param bytes 字节数组
|
||||
* @param append 是否追加在文件末
|
||||
* @param isForce 是否写入文件
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByMap(final String filePath, final byte[] bytes, final boolean append, final boolean isForce) {
|
||||
return writeFileFromBytesByMap(getFileByPath(filePath), bytes, append, isForce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param bytes 字节数组
|
||||
* @param isForce 是否写入文件
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByMap(final File file, final byte[] bytes, final boolean isForce) {
|
||||
return writeFileFromBytesByMap(file, bytes, false, isForce);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字节数组写入文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param bytes 字节数组
|
||||
* @param append 是否追加在文件末
|
||||
* @param isForce 是否写入文件
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromBytesByMap(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
|
||||
if (bytes == null || !createOrExistsFile(file)) return false;
|
||||
FileChannel fc = null;
|
||||
try {
|
||||
fc = new FileOutputStream(file, append).getChannel();
|
||||
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, fc.size(), bytes.length);
|
||||
mbb.put(bytes);
|
||||
if (isForce) mbb.force();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
CloseUtils.closeIO(fc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串写入文件
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param content 写入内容
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromString(final String filePath, final String content) {
|
||||
return writeFileFromString(getFileByPath(filePath), content, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串写入文件
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param content 写入内容
|
||||
* @param append 是否追加在文件末
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromString(final String filePath, final String content, final boolean append) {
|
||||
return writeFileFromString(getFileByPath(filePath), content, append);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串写入文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param content 写入内容
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromString(final File file, final String content) {
|
||||
return writeFileFromString(file, content, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串写入文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param content 写入内容
|
||||
* @param append 是否追加在文件末
|
||||
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
||||
*/
|
||||
public static boolean writeFileFromString(final File file, final String content, final boolean append) {
|
||||
if (file == null || content == null) return false;
|
||||
if (!createOrExistsFile(file)) return false;
|
||||
BufferedWriter bw = null;
|
||||
try {
|
||||
bw = new BufferedWriter(new FileWriter(file, append));
|
||||
bw.write(content);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
CloseUtils.closeIO(bw);
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// the divide line of write and read
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 读取文件到字符串链表中
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @return 字符串链表中
|
||||
*/
|
||||
public static List<String> readFile2List(final String filePath) {
|
||||
return readFile2List(getFileByPath(filePath), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串链表中
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param charsetName 编码格式
|
||||
* @return 字符串链表中
|
||||
*/
|
||||
public static List<String> readFile2List(final String filePath, final String charsetName) {
|
||||
return readFile2List(getFileByPath(filePath), charsetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串链表中
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 字符串链表中
|
||||
*/
|
||||
public static List<String> readFile2List(final File file) {
|
||||
return readFile2List(file, 0, 0x7FFFFFFF, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串链表中
|
||||
*
|
||||
* @param file 文件
|
||||
* @param charsetName 编码格式
|
||||
* @return 字符串链表中
|
||||
*/
|
||||
public static List<String> readFile2List(final File file, final String charsetName) {
|
||||
return readFile2List(file, 0, 0x7FFFFFFF, charsetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串链表中
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param st 需要读取的开始行数
|
||||
* @param end 需要读取的结束行数
|
||||
* @return 字符串链表中
|
||||
*/
|
||||
public static List<String> readFile2List(final String filePath, final int st, final int end) {
|
||||
return readFile2List(getFileByPath(filePath), st, end, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串链表中
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param st 需要读取的开始行数
|
||||
* @param end 需要读取的结束行数
|
||||
* @param charsetName 编码格式
|
||||
* @return 字符串链表中
|
||||
*/
|
||||
public static List<String> readFile2List(final String filePath, final int st, final int end, final String charsetName) {
|
||||
return readFile2List(getFileByPath(filePath), st, end, charsetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串链表中
|
||||
*
|
||||
* @param file 文件
|
||||
* @param st 需要读取的开始行数
|
||||
* @param end 需要读取的结束行数
|
||||
* @return 字符串链表中
|
||||
*/
|
||||
public static List<String> readFile2List(final File file, final int st, final int end) {
|
||||
return readFile2List(file, st, end, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串链表中
|
||||
*
|
||||
* @param file 文件
|
||||
* @param st 需要读取的开始行数
|
||||
* @param end 需要读取的结束行数
|
||||
* @param charsetName 编码格式
|
||||
* @return 字符串链表中
|
||||
*/
|
||||
public static List<String> readFile2List(final File file, final int st, final int end, final String charsetName) {
|
||||
if (!isFileExists(file)) return null;
|
||||
if (st > end) return null;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
String line;
|
||||
int curLine = 1;
|
||||
List<String> list = new ArrayList<>();
|
||||
if (isSpace(charsetName)) {
|
||||
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
|
||||
} else {
|
||||
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
|
||||
}
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (curLine > end) break;
|
||||
if (st <= curLine && curLine <= end) list.add(line);
|
||||
++curLine;
|
||||
}
|
||||
return list;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
CloseUtils.closeIO(reader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串中
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String readFile2String(final String filePath) {
|
||||
return readFile2String(getFileByPath(filePath), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串中
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @param charsetName 编码格式
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String readFile2String(final String filePath, final String charsetName) {
|
||||
return readFile2String(getFileByPath(filePath), charsetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串中
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String readFile2String(final File file) {
|
||||
return readFile2String(file, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字符串中
|
||||
*
|
||||
* @param file 文件
|
||||
* @param charsetName 编码格式
|
||||
* @return 字符串
|
||||
*/
|
||||
public static String readFile2String(final File file, final String charsetName) {
|
||||
if (!isFileExists(file)) return null;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (isSpace(charsetName)) {
|
||||
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
|
||||
} else {
|
||||
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
|
||||
}
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line).append(LINE_SEP);
|
||||
}
|
||||
// delete the last line separator
|
||||
return sb.delete(sb.length() - LINE_SEP.length(), sb.length()).toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
CloseUtils.closeIO(reader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字节数组中
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @return 字符数组
|
||||
*/
|
||||
public static byte[] readFile2BytesByStream(final String filePath) {
|
||||
return readFile2BytesByStream(getFileByPath(filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字节数组中
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 字符数组
|
||||
*/
|
||||
public static byte[] readFile2BytesByStream(final File file) {
|
||||
if (!isFileExists(file)) return null;
|
||||
FileInputStream fis = null;
|
||||
ByteArrayOutputStream os = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
os = new ByteArrayOutputStream();
|
||||
byte[] b = new byte[sBufferSize];
|
||||
int len;
|
||||
while ((len = fis.read(b, 0, sBufferSize)) != -1) {
|
||||
os.write(b, 0, len);
|
||||
}
|
||||
return os.toByteArray();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
CloseUtils.closeIO(fis, os);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字节数组中
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @return 字符数组
|
||||
*/
|
||||
public static byte[] readFile2BytesByChannel(final String filePath) {
|
||||
return readFile2BytesByChannel(getFileByPath(filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字节数组中
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 字符数组
|
||||
*/
|
||||
public static byte[] readFile2BytesByChannel(final File file) {
|
||||
if (!isFileExists(file)) return null;
|
||||
FileChannel fc = null;
|
||||
try {
|
||||
fc = new RandomAccessFile(file, "r").getChannel();
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate((int) fc.size());
|
||||
while (true) {
|
||||
if (!((fc.read(byteBuffer)) > 0)) break;
|
||||
}
|
||||
return byteBuffer.array();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
CloseUtils.closeIO(fc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字节数组中
|
||||
*
|
||||
* @param filePath 文件路径
|
||||
* @return 字符数组
|
||||
*/
|
||||
public static byte[] readFile2BytesByMap(final String filePath) {
|
||||
return readFile2BytesByMap(getFileByPath(filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件到字节数组中
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 字符数组
|
||||
*/
|
||||
public static byte[] readFile2BytesByMap(final File file) {
|
||||
if (!isFileExists(file)) return null;
|
||||
FileChannel fc = null;
|
||||
try {
|
||||
fc = new RandomAccessFile(file, "r").getChannel();
|
||||
int size = (int) fc.size();
|
||||
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
|
||||
byte[] result = new byte[size];
|
||||
mbb.get(result, 0, size);
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
CloseUtils.closeIO(fc);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓冲区尺寸
|
||||
*
|
||||
* @param bufferSize 缓冲区大小
|
||||
*/
|
||||
public static void setBufferSize(final int bufferSize) {
|
||||
sBufferSize = bufferSize;
|
||||
}
|
||||
|
||||
private static File getFileByPath(final String filePath) {
|
||||
return isSpace(filePath) ? null : new File(filePath);
|
||||
}
|
||||
|
||||
private static boolean createOrExistsFile(final String filePath) {
|
||||
return createOrExistsFile(getFileByPath(filePath));
|
||||
}
|
||||
|
||||
private static boolean createOrExistsFile(final File file) {
|
||||
if (file == null) return false;
|
||||
if (file.exists()) return file.isFile();
|
||||
if (!createOrExistsDir(file.getParentFile())) return false;
|
||||
try {
|
||||
return file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean createOrExistsDir(final File file) {
|
||||
return file != null && (file.exists() ? file.isDirectory() : file.mkdirs());
|
||||
}
|
||||
|
||||
private static boolean isFileExists(final File file) {
|
||||
return file != null && file.exists();
|
||||
}
|
||||
|
||||
private static boolean isSpace(final String s) {
|
||||
if (s == null) return true;
|
||||
for (int i = 0, len = s.length(); i < len; ++i) {
|
||||
if (!Character.isWhitespace(s.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
1133
app/src/main/java/com/fenghoo/seven/utils/FileUtils.java
Normal file
1133
app/src/main/java/com/fenghoo/seven/utils/FileUtils.java
Normal file
File diff suppressed because it is too large
Load Diff
1526
app/src/main/java/com/fenghoo/seven/utils/ImageUtils.java
Normal file
1526
app/src/main/java/com/fenghoo/seven/utils/ImageUtils.java
Normal file
File diff suppressed because it is too large
Load Diff
192
app/src/main/java/com/fenghoo/seven/utils/Utils.java
Normal file
192
app/src/main/java/com/fenghoo/seven/utils/Utils.java
Normal file
@@ -0,0 +1,192 @@
|
||||
package com.fenghoo.seven.utils;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Bundle;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Base64;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* author: Blankj
|
||||
* blog : http://blankj.com
|
||||
* time : 16/12/08
|
||||
* desc : Utils初始化相关
|
||||
* </pre>
|
||||
*/
|
||||
public final class Utils {
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static Context context;
|
||||
|
||||
private Utils() {
|
||||
throw new UnsupportedOperationException("u can't instantiate me...");
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化工具类
|
||||
*
|
||||
* @param context 上下文
|
||||
*/
|
||||
public static void init(@NonNull final Context context) {
|
||||
Utils.context = context.getApplicationContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ApplicationContext
|
||||
*
|
||||
* @return ApplicationContext
|
||||
*/
|
||||
public static Context getContext() {
|
||||
if (context != null) return context;
|
||||
throw new NullPointerException("u should init first");
|
||||
}
|
||||
public static int dip2px(Context context, float dpValue) {
|
||||
final float scale = context.getResources().getDisplayMetrics().density;
|
||||
return (int) (dpValue * scale);
|
||||
}
|
||||
|
||||
public static int px2dip(Context context, float pxValue) {
|
||||
final float scale = context.getResources().getDisplayMetrics().density;
|
||||
return (int) (pxValue / scale);
|
||||
}
|
||||
|
||||
public static int getVisiblePercent(View pView) {
|
||||
if (pView != null && pView.isShown()) {
|
||||
DisplayMetrics displayMetrics = pView.getContext().getResources().getDisplayMetrics();
|
||||
int displayWidth = displayMetrics.widthPixels;
|
||||
Rect rect = new Rect();
|
||||
pView.getGlobalVisibleRect(rect);
|
||||
if ((rect.top > 0) && (rect.left < displayWidth)) {
|
||||
double areaVisible = rect.width() * rect.height();
|
||||
double areaTotal = pView.getWidth() * pView.getHeight();
|
||||
return (int) ((areaVisible / areaTotal) * 100);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//is wifi connected
|
||||
public static boolean isWifiConnected(Context context) {
|
||||
if (context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_WIFI_STATE)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
return false;
|
||||
}
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager)
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
|
||||
if (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取对应应用的版本号
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static String getAppVersion(Context context) {
|
||||
String version = "1.0.0"; //默认1.0.0版本
|
||||
PackageManager manager = context.getPackageManager();
|
||||
try {
|
||||
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
|
||||
version = info.versionName;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
|
||||
public static DisplayMetrics getDisplayMetrics(Context context) {
|
||||
DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
if (windowManager == null) {
|
||||
return displayMetrics;
|
||||
}
|
||||
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
|
||||
return displayMetrics;
|
||||
}
|
||||
|
||||
public static BitmapDrawable decodeImage(String base64drawable) {
|
||||
byte[] rawImageData = Base64.decode(base64drawable, 0);
|
||||
return new BitmapDrawable(null, new ByteArrayInputStream(rawImageData));
|
||||
}
|
||||
|
||||
public static boolean isPad(Context context) {
|
||||
|
||||
//如果能打电话那可能是平板或手机,再根据配置判断
|
||||
if (canTelephone(context)) {
|
||||
//能打电话可能是手机也可能是平板
|
||||
return (context.getResources().getConfiguration().
|
||||
screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
|
||||
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
|
||||
} else {
|
||||
return true; //不能打电话一定是平板
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean canTelephone(Context context) {
|
||||
TelephonyManager telephony = (TelephonyManager)
|
||||
context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
return (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) ? false : true;
|
||||
}
|
||||
|
||||
public static boolean containString(String source, String destation) {
|
||||
|
||||
if (source.equals("") || destation.equals("")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (source.contains(destation)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取view的屏幕属性
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static final String VIEW_INFO_EXTRA = "view_into_extra";
|
||||
public static final String PROPNAME_SCREENLOCATION_LEFT = "propname_sreenlocation_left";
|
||||
public static final String PROPNAME_SCREENLOCATION_TOP = "propname_sreenlocation_top";
|
||||
public static final String PROPNAME_WIDTH = "propname_width";
|
||||
public static final String PROPNAME_HEIGHT = "propname_height";
|
||||
|
||||
public static Bundle getViewProperty(View view) {
|
||||
Bundle bundle = new Bundle();
|
||||
int[] screenLocation = new int[2];
|
||||
view.getLocationOnScreen(screenLocation); //获取view在整个屏幕中的位置
|
||||
bundle.putInt(PROPNAME_SCREENLOCATION_LEFT, screenLocation[0]);
|
||||
bundle.putInt(PROPNAME_SCREENLOCATION_TOP, screenLocation[1]);
|
||||
bundle.putInt(PROPNAME_WIDTH, view.getWidth());
|
||||
bundle.putInt(PROPNAME_HEIGHT, view.getHeight());
|
||||
|
||||
Log.e("Utils", "Left: " + screenLocation[0] + " Top: " + screenLocation[1]
|
||||
+ " Width: " + view.getWidth() + " Height: " + view.getHeight());
|
||||
return bundle;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user