android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
public class ImageDispose {
/**
* @param 将图片内容解析成字节数组
* @param inStream
* @return byte[]
* @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();
return data;
}
/**
* @param 将字节数组转换为ImageView可调用的Bitmap对象
* @param bytes
* @param opts
* @return Bitmap
*/
public static Bitmap getPicFromBytes(byte[] bytes,
BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
/**
* @param 图片缩放
* @param bitmap 对象
* @param w 要缩放的宽度
* @param h 要缩放的高度
* @return newBmp 新 Bitmap对象
*/
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转Byte
*/
public static byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
/**
* 把字节数组保存为一个文件
*/
public static File getFileFromBytes(byte[] b, String outputFile) {
BufferedOutputStream stream = null;
File file = null;
try {
file = new File(outputFile);
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();
}
}
}
return file;
}
}
相关推荐
android_Drawable、Bitmap、byte[]之间的转换.doc
详细的介绍了android_Drawable、Bitmap、byte[]之间的转换 在android开发中非常实用
(转)Android中Bitmap对象和字节流之间的相互转换
android 将图片内容解析成字节数组;将字节数组转换为ImageView可调用的Bitmap对象;图片缩放;把字节数组保存为一个文件;把Bitmap转Byte 1 import java.io.BufferedOutputStream; 2 import java.io.ByteArrayOutputStream; 3 import java.io.Fi...
最新版windows mongodb-windows-x86_64-5.0.1-signed.msi
最新版windows mongodb-windows-x86_64-5.0.1-signed.msi最新版windows mongodb-windows-x86_64-5.0.1-signed.msi
安卓将Bitmap图片转换为byte[]数组
BItmap转换为byte[]数组 photo对象是一个Bitmap对象 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//创建对应的流对象 photo.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);//将流对象与Bitmap对象进行关联。 byte [] xx=byteArrayOutputStream.toByteArr
Android学习之Bitmap对象与字节数组相互转换
1、将Bitmap对象读到字节数组中 [java] view plain copy ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] datas
Android 图片压缩,Bitmap旋转,bitmap与byte[]之间相互转换,Bitmap与String互转
压缩前后。图片大小 2.22MB——>200KB 1、图片压缩方法: Bitmap bitmap; byte[] buff; buff = Bitmap2Bytes(bitmap); BitmapFactory.Options ontain = new BitmapFactory.Options(); ontain.inSampleSize = 7;//...
安卓bitmap与字节数组关系深入研究应用
Bitmap Byte 安卓
Android中Bitmap对象和字节流之间的相互转换(转)
import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory;
Android之Bitmap对象和字节流之间的相互转换
将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte [code="java"]import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; ...
Drawable、Bitmap、Canvas和Paint的关系以及部分使用方法
刚刚开始学习Android平台,对于Drawable、Bitmap、Canvas和Paint它们之间的概念不是很清楚,其实它们除了Drawable外早在Sun的J2ME中就已经出现了,但是在Android平台中,Bitmap、Canvas相关的都有所变化。 首先让我们理解下Android平台中的显示类是View,但是还提供了底层图形类android.graphics,今天所说的这些均为gra
[Android]图片二进制与Bitmap、Drawable之间的相互转换
1、将图片转换成二进制流 public static byte[] getBitmapByte(Bitmap bitmap){ ByteArrayOutputStream out = new ByteArrayOutputStream(); //参数1转换类型,参数2压缩质量,参数3字节流资源 bitmap.compress(Bitmap.CompressForma...
byte字节流和bitmap互相转换
/**把byte字节流转成bitmap * @param bytes */ public void byteToBitmap(byte[] bytes) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = false;//为true时,返回的bitmap为null Bitmap bitmap = BitmapFactory.decodeByteArray(by.
Android 图片与Byte[]数组之间的相互转换
转载大佬 一、将图片转换成byte[]数组 public static byte[] bitmap2Bytes(Bitmap bitmap){ if( null != bitmap ){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); //注意压缩png和jpg的格式和质量 100 是质量
Drawable和Bitmap转换
一、Bitmap转DrawableBitmap bm=xxx; //xxx根据你的情况获取BitmapDrawable bd=new BitmapDrawable(bm);因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。二、 Drawable转Bitmap转成Bitmap对象后,可以将Drawable对象通过Android的SK库存成一个字节输出流,最终还可以保
读取到的bitmap,转成buffer并保存
bitmap转成buffer并保存,读取文件转成buffer再转成bitmap
Android中Bitmap,byte[],Drawable相互转化
一、相关概念 1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象 2、Canvas画布,绘图的目的区域,用于绘图 3、Bitmap位图,用于图的处理 4、Matrix矩阵 很多Android开发者可能发现,
Android:Base64生产Bitmap压缩和转byte[]
最近在做微信分享的时候遇到了分享图片的大小限制问题,需要对图片进行压缩。在过程中遇到几个有趣的地方在此记录。 Bitmap.getByteCount的大小和转化为byte[]的大小差很多不是8倍,而是几十倍,我自测的为67倍 压缩Bitmap直接根据长宽比进行调用 createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHei...
Android开发之Bitmap各种转换-ImageUtils
对于在项目中,经常对图片进行处理,来满足不同功能需要,整理了部分常见需求代码,分享一下,后续不断更新中 bitmap转字节数组 public static byte[] bitmap2Bytes(final Bitmap bitmap, final CompressFormat format) { if (bitmap == null) return null; ...
bitmap转byte android,Android bitmap转byte
private static byte[] bitmap2byte(Bitmap bmp) throwsException{return bitmap2byte(bmp, null);}private static byte[] bitmap2byte(Bitmap bmp, StarReverseConfig config) throwsException{Long s=System.curre...
Android Drawable、Bitmap、byte[]之间的转换
Drawable –> BitmapBitmap drawable2Bitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (draw
554




被折叠的 条评论
为什么被折叠?



