社区
Android
帖子详情
java中 把byte[]数组用decodeByteArray转化成bitmap之后,怎样才能把bitmap再转化成原来的byte[]
米悠悠
2017-05-02 04:13:25
java中 把byte[]数组用decodeByteArray转化成bitmap之后,怎样才能把bitmap再转化成原来的byte[]
...全文
708
3
打赏
收藏
java中 把byte[]数组用decodeByteArray转化成bitmap之后,怎样才能把bitmap再转化成原来的byte[]
java中 把byte[]数组用decodeByteArray转化成bitmap之后,怎样才能把bitmap再转化成原来的byte[]
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
guwei4037
2017-05-02
打赏
举报
回复
public static byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray();
http://blog.csdn.net/itheima6/article/details/7516631/
}
米悠悠
2017-05-02
打赏
举报
回复
没有人知道吗
米悠悠
2017-05-02
打赏
举报
回复
我试了几个bitmap转成byte[]的方法都和原来的字节数组内容不一样,求指教~~~~~~~~~~
android
Bitmap
用法总结
android
Bitmap
用法总结
Bitmap
用法总结 1、Drawable →
Bitmap
public static
Bitmap
drawableTo
Bitmap
(Drawable drawable) {
Bitmap
bitmap
=
Bitmap
.create
Bitmap
( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap
.Config.ARGB_8888 :
Bitmap
.Config.RGB_565); Canvas canvas = new Canvas(
bitmap
); // canvas.set
Bitmap
(
bitmap
); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return
bitmap
; } 2、从资源
中
获取
Bitmap
Resources res=getResources();
Bitmap
bmp=
Bitmap
Factory.
decode
Resource(res, R.drawable.pic); 3、
Bitmap
→
byte
[] private
byte
[]
Bitmap
2
Byte
s(
Bitmap
bm){
Byte
Array
OutputStream baos = new
Byte
Array
OutputStream(); bm.compress(
Bitmap
.CompressFormat.PNG, 100, baos); return baos.to
Byte
Array
(); } 4、
byte
[] →
Bitmap
private
Bitmap
Byte
s2Bimap(
byte
[] b){ if(b.length!=0){ return
Bitmap
Factory.
decode
Byte
Array
(b, 0, b.length); } else { return null; } } 5、保存
bitmap
static boolean save
Bitmap
2file(
Bitmap
bmp,String filename){ CompressFormat format=
Bitmap
.CompressFormat.JPEG; int quality = 100; OutputStream stream = null; try { stream = new FileOutputStream("/sdcard/" + filename); } catch (FileNotFoundException e) { // TODO Auto-generated catch block Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. e.printStackTrace(); } return bmp.compress(format, quality, stream); } 6、将图片按自己的要求缩放 // 图片源
Bitmap
bm =
Bitmap
Factory.
decode
Stream(getResources() .openRawResource(R.drawable.dog)); // 获得图片的宽高 int width = bm.getWidth(); int height = bm.getHeight(); // 设置想要的大小 int newWidth = 320; int newHeight = 480; // 计算缩放比例 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 取得想要缩放的matrix参数 Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // 得到新的图片
Bitmap
newbm =
Bitmap
.create
Bitmap
(bm, 0, 0, width, height, matrix, true); // 放在画布上 canvas.draw
Bitmap
(newbm, 0, 0, paint); 相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html 7、
bitmap
的用法小结
Bitmap
Factory.Options option = new
Bitmap
Factory.Options(); option.inSampleSize = 2; //将图片设为
原来
宽高的1/2,防止内存溢出
Bitmap
bm =
Bitmap
Factory.
decode
File("",option);//文件流 URL url = new URL(""); InputStream is = url.openStream();
Bitmap
bm =
Bitmap
Factory.
decode
Stream(is); android:scaleType: android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别: CENTER /center 按图片的
原来
size居
中
显示,当图片长/宽超过View的长/宽,则截取图片的居
中
部分 显示 CENTER_CROP / centerCrop 按比例扩大图片的size居
中
显示,使得图片长(宽)等于或大于View的长 (宽) CENTER_INSIDE / centerInside 将图片的内容完整居
中
显示,通过按比例缩小或
原来
的size使得图片 长/宽等于或小于View的长/宽 Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居
中
显示 FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置 FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置 FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示 MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。 //放大缩小图片 public static
Bitmap
zoom
Bitmap
(
Bitmap
bitmap
,int w,int h){ int width =
bitmap
.getWidth(); int height =
bitmap
.getHeight(); Matrix matrix = new Matrix(); float scaleWidht = ((float)w / width); float scaleHeight = ((float)h / height); matrix.postScale(scaleWidht, scaleHeight);
Bitmap
newbmp =
Bitmap
.create
Bitmap
(
bitmap
, 0, 0, width, height, matrix, true); return newbmp; } //将Drawable转化为
Bitmap
public static
Bitmap
drawableTo
Bitmap
(Drawable drawable){ int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight();
Bitmap
bitmap
=
Bitmap
.create
Bitmap
(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap
.Config.ARGB_8888 :
Bitmap
.Config.RGB_565); Canvas canvas = new Canvas(
bitmap
); drawable.setBounds(0,0,width,height); drawable.draw(canvas); return
bitmap
; Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. } //获得圆角图片的方法 public static
Bitmap
getRoundedCorner
Bitmap
(
Bitmap
bitmap
,float roundPx){
Bitmap
output =
Bitmap
.create
Bitmap
(
bitmap
.getWidth(),
bitmap
.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0,
bitmap
.getWidth(),
bitmap
.getHeight()); final RectF rectF = new RectF(rect); 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.draw
Bitmap
(
bitmap
, rect, rect, paint); return output; } //获得带倒影的图片方法 public static
Bitmap
createReflectionImageWithOrigin(
Bitmap
bitmap
){ final int reflectionGap = 4; int width =
bitmap
.getWidth(); int height =
bitmap
.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1);
Bitmap
reflectionImage =
Bitmap
.create
Bitmap
(
bitmap
, 0, height/2, width, height/2, matrix, false);
Bitmap
bitmap
WithReflection =
Bitmap
.create
Bitmap
(width, (height + height/2), Config.ARGB_8888); Canvas canvas = new Canvas(
bitmap
WithReflection); canvas.draw
Bitmap
(
bitmap
, 0, 0, null); Paint deafalutPaint = new Paint(); Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. canvas.drawRect(0, height,width,height + reflectionGap, deafalutPaint); canvas.draw
Bitmap
(reflectionImage, 0, height + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0,
bitmap
.getHeight(), 0,
bitmap
WithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, height, width,
bitmap
WithReflection.getHeight() + reflectionGap, paint); return
bitmap
WithReflection; } }
读取sd卡图片
读取sd卡图片并且显示到出来 package cn.itcast.test; import
java
.io.File; import
java
.io.FileInputStream; import
java
.io.InputStream; import android.graphics.
Bitmap
; import android.graphics.
Bitmap
Factory; public class ImageUtil { /** * 获取网络address地址对应的图片 * @param address * @return
bitmap
的类型 */ public static
Bitmap
getImage(String address) throws Exception{ File file = new File(address); FileInputStream fis = new FileInputStream(file);
byte
[] image
byte
s = StreamUtil.get
Byte
s(fis);
Bitmap
bitmap
=
Bitmap
Factory.
decode
Byte
Array
(image
byte
s, 0, image
byte
s.length); return
bitmap
; } }
java
byte
to
bitmap
_
Byte
[]
Bitmap
转化
public
Bitmap
get
Bitmap
(
byte
[]
byte
s) {Options opt = new Options();opt.inJust
Decode
Bounds = true; //只分配边界不分配像素
Decode
Bitmap
时候
decode
Bitmap
(
byte
s, opt);if (opt.outHeight * opt.outWidth < MAX_IMAGE_PIX...
Activity之间传递
Bitmap
,将
Bitmap
转换为
Byte
数组
后传递
Activity之间传递数据的大小是有限制的,
Bitmap
又往往比较大,如果直接传递
Bitmap
的话总是会出现各种问题,因此需要将
Bitmap
转换为
Byte
数组
后再传递 转换后发送:
Byte
Array
OutputStream baos=new
Byte
Array
OutputStream();
bitmap
.compress(
Bitmap
.CompressFormat.PNG
java
bitmap
转换成
byte
数组
_Android
中
Bitmap
、Drawable、
byte
s
数组
之间相互转换
package com.uperone.resources;import
java
.io.
Byte
Array
OutputStream;import android.content.Context;import android.graphics.
Bitmap
;import android.graphics.
Bitmap
Factory;import android.graphics.Canvas;im...
Android
80,350
社区成员
91,289
社区内容
发帖
与我相关
我的任务
Android
移动平台 Android
复制链接
扫一扫
分享
社区描述
移动平台 Android
android
android-studio
androidx
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章