commit
df4c49aa4e
@ -0,0 +1,121 @@
|
||||
package com.facepp.api.test;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Environment;
|
||||
import android.util.Base64;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author by licheng on 2017/8/8.
|
||||
*/
|
||||
|
||||
public class BitmapUtil {
|
||||
|
||||
//将字符串转换成Bitmap类型
|
||||
public static Bitmap stringtoBitmap(String string) {
|
||||
Bitmap bitmap = null;
|
||||
try {
|
||||
byte[] bitmapArray;
|
||||
bitmapArray = Base64.decode(string, Base64.DEFAULT);
|
||||
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
//将Bitmap转换成字符串
|
||||
public static String bitmaptoString(Bitmap bitmap) {
|
||||
String string;
|
||||
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
|
||||
byte[] bytes = bStream.toByteArray();
|
||||
string = Base64.encodeToString(bytes, Base64.DEFAULT);
|
||||
return string;
|
||||
}
|
||||
|
||||
public static File saveBitmapFile(Bitmap bitmap) {
|
||||
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp.jpg");//将要保存图片的路径
|
||||
try {
|
||||
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
|
||||
bos.flush();
|
||||
bos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
public static byte[] toByteArray(Bitmap bitmap) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
public static byte[] File2byte(String filePath) {
|
||||
byte[] buffer = null;
|
||||
try {
|
||||
File file = new File(filePath);
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
byte[] b = new byte[1024];
|
||||
int n;
|
||||
while ((n = fis.read(b)) != -1) {
|
||||
bos.write(b, 0, n);
|
||||
}
|
||||
fis.close();
|
||||
bos.close();
|
||||
buffer = bos.toByteArray();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public static void byte2File(byte[] buf, String filePath) {
|
||||
BufferedOutputStream bos = null;
|
||||
FileOutputStream fos = null;
|
||||
File file;
|
||||
try {
|
||||
File dir = new File(filePath);
|
||||
if (!dir.exists() && dir.isDirectory()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
file = new File(filePath);
|
||||
fos = new FileOutputStream(file);
|
||||
bos = new BufferedOutputStream(fos);
|
||||
bos.write(buf);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (bos != null) {
|
||||
try {
|
||||
bos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue