|
@@ -1,12 +1,22 @@
|
|
|
package com.sunwin.visitorapp.utils;
|
|
|
|
|
|
import android.content.Context;
|
|
|
+import android.graphics.Bitmap;
|
|
|
+import android.graphics.BitmapFactory;
|
|
|
+import android.graphics.ImageFormat;
|
|
|
+import android.graphics.Matrix;
|
|
|
+import android.graphics.Rect;
|
|
|
+import android.graphics.YuvImage;
|
|
|
+import android.util.Log;
|
|
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
public class FileUtil {
|
|
|
+ private static final String TAG = "FileUtil";
|
|
|
+
|
|
|
public static void copyFilesFromAssets(Context context, String assetsPath, String savePath){
|
|
|
try {
|
|
|
String fileNames[] = context.getAssets().list(assetsPath);// 获取assets目录下的所有文件及目录名
|
|
@@ -35,4 +45,55 @@ public class FileUtil {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static void saveFile(byte[] date, int width, int height, int rotation,String saveFile) {
|
|
|
+ Bitmap bitmap = getBitmapFromYUV(date, width, height, rotation);
|
|
|
+ double size = 100;
|
|
|
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
|
+ int options = 100;
|
|
|
+ bitmap.compress(Bitmap.CompressFormat.JPEG, options, stream);
|
|
|
+
|
|
|
+ while (stream.toByteArray().length / 1024 > size && options > 6) {
|
|
|
+ stream.reset();
|
|
|
+ options -= 6;
|
|
|
+ bitmap.compress(Bitmap.CompressFormat.JPEG, options, stream);
|
|
|
+ }
|
|
|
+
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ try {
|
|
|
+ fos = new FileOutputStream(saveFile);
|
|
|
+ fos.write(stream.toByteArray());
|
|
|
+ fos.flush();
|
|
|
+ fos.close();
|
|
|
+ stream.close();
|
|
|
+ bitmap.recycle();
|
|
|
+ LogUtil.e(TAG, "存入照片 " + saveFile);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static Bitmap getBitmapFromYUV(byte[] date, int width, int height, int rotation) {
|
|
|
+ //使用YuvImage---》NV21
|
|
|
+ YuvImage yuvImage = new YuvImage(date, ImageFormat.NV21, width, height, null);
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+
|
|
|
+ yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, baos);
|
|
|
+ byte[] jdate = baos.toByteArray();
|
|
|
+ BitmapFactory.Options bitmapFatoryOptions = new BitmapFactory.Options();
|
|
|
+ bitmapFatoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;
|
|
|
+ bitmapFatoryOptions.inSampleSize = 2;
|
|
|
+ if (rotation == 0) {
|
|
|
+ Bitmap bmp = BitmapFactory.decodeByteArray(jdate, 0, jdate.length, bitmapFatoryOptions);
|
|
|
+ return bmp;
|
|
|
+ } else {
|
|
|
+ Matrix m = new Matrix();
|
|
|
+ m.postRotate(rotation);
|
|
|
+ Bitmap bmp = BitmapFactory.decodeByteArray(jdate, 0, jdate.length, bitmapFatoryOptions);
|
|
|
+ Bitmap bml = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m, true);
|
|
|
+ return bml;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|