drawable,bitmap,base64,file之间的转换

2018-02-27 11:19:10来源:https://www.jianshu.com/p/acfa031779ab作者:某人_Valar人点击

分享



作者:某人_Valar
如需转载请保留原文链接;
不断整理中,大家有好的方法也可以在评论中写出。



drawable与bitmap之间转换
/**
* drawable转为bitmap
*/
//drawable文件夹下的文件res直接转为bitmap
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
//res转为drawable
Resource res = gerResource();
Drawable drawable = res.getDrawable(R.drawable.icon);
//普通drawable到bitmap
BitmapDrawable btDrawable = (BitmapDrawable) drawable ;
Bitmap bitmap = drawable.getBitmap();
//ImageView中的图像转为BitmapDrawable再到bitmap
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();

/**
*bitmap转为drawable
*/
//不过现在这种方法官方已经不推荐使用了
Drawable drawable1 = new BitmapDrawable(bitmap);
//取代new BitmapDrawable(bitmap)的也很简单
Drawable drawable2 = new BitmapDrawable(getResources(),bitmap);

bitmap与base64之间转换

bitmap与base64之间转化需要一个中间变量byte[]


/**
*bitmap转为base64
*/
//先将bitmap转为byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,baos);
byte[] bytes = baos.toByteArray();
//将byte[]转为base64
String myBase64 = Base64.encodeToString(bytes,Base64.DEFAULT);

/**
*base64转为bitmap
*/
byte[] bytes = Base64.decode(myBase64 , Base64.DEFAULT);
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

drawable与base64之间转换
需要通过bitmap充当中间介质,方法可参考上面所写的。
base64与file之间转换
/**
* base64字符串转化成文件(图片)
*/
public static boolean base64ToFile(String imgStr,String filePath) {
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(filePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}

/**
* 图片文件转化成base64字符串
*/
public static String fileToBase64(String imgFile) {
InputStream in = null;
String base64 = null;
// 读取图片字节数组
try {
if(imgFile==null||"".equals(imgFile)){
imgFile="uploaddir/file/default.png";
}
in = new FileInputStream(imgFile);
byte[] bytes = new byte[in.available()];
int length = in.read(bytes);
base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭输入流
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64;
}

bitmap与file之间转换
/**
* bitmap保存为file
*/
public static void bitmapToFile(String filePath,
Bitmap bitmap, int quality) throws IOException {
if (bitmap != null) {
File file = new File(filePath.substring(0,
filePath.lastIndexOf(File.separator)));
if (!file.exists()) {
file.mkdirs();
}
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(filePath));
bitmap.compress(CompressFormat.PNG, quality, bos);
bos.flush();
bos.close();
}
}

/**
* file转为bitmap
*/
public static Bitmap fileToBitmap(String filePath) {
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
return bitmap;
}
//如果图片过大,可能导致Bitmap对象装不下图片
public static Bitmap fileToBitmap(String filePath, int scale) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inSampleSize = scale;
//将图片的长和宽缩小味原来的1/scale
Bitmap bitmap=BitmapFactory.decodeFile(filePath,options);
}

/**
* file转为bitmap(网上发现的另一种写法,主要区别在与这种是计算得到的scale)
*/
public static Bitmap fileToBitmap(String filePath) throws IOException{
Bitmap b = null;
int IMAGE_MAX_SIZE = 600;

File f = new File(filePath);
if (f == null){
return null;
}
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;

FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();

int scale = 1;
//如果图片过大,可能导致Bitmap对象装不下图片
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
//scale的计算
scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}

//以一定的比例转为bitmap
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
return b;
}
}







最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台