Android中Bitmap对象和字节流之间的相互转换

DSSAT模型实战:从基础配置到气候情景模拟 本文详细介绍了DSSAT模型在农业模拟中的应用,从基础配置到气候情景模拟的全流程实战指南。通过土壤-植物-大气连续体的动态关系建模,DSSAT能精准预测作物产量,误差率低至8%。文章涵盖气象数据处理、土壤参数配置、管理措施调控等核心技巧,并提供了Python脚本可视化方法,助力农业科研与决策。 阅读详情
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;  
    }  
          
}  
ROS新手必看:如何用URDF快速搭建UR5机械臂模型(附完整代码) 本文为ROS新手提供了一份详细的UR5机械臂URDF建模实战指南。通过解析URDF核心概念,逐步讲解如何定义link与joint,并整合官方网格文件,最终构建出完整的、可在RViz中可视化与控制的UR5模型。文章还介绍了模型验证、调试技巧以及如何添加碰撞与惯性属性,为后续的物理仿真与算法开发奠定坚实基础。 阅读详情

相关推荐

android_Drawable、Bitmapbyte[]之间转换.doc

详细的介绍了android_Drawable、Bitmapbyte[]之间转换android开发中非常实用

AndroidBitmap对象字节流之间相互转换

android 将图片内容解析成字节数组;将字节数组转换为ImageView可调用的Bitmap对象;图片缩放;把字节数组保存为一个文件;把BitmapByte 1 import java.io.BufferedOutputStream; 2 import java.io.ByteArrayOutputStream; 3 import java.io.Fi...

aibo8196的博客 554

最新版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

qq_47529104的博客 3327

Android学习之Bitmap对象与字节数组相互转换

1、将Bitmap对象读到字节数组中 [java] view plain copy ByteArrayOutputStream baos = new ByteArrayOutputStream();   bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);   byte[] datas

BillyLu的博客 1万+

Android 图片压缩,Bitmapbitmapbyte[]之间相互转换Bitmap与String互

压缩前后。图片大小 2.22MB——>200KB 1、图片压缩方法: Bitmap bitmap; byte[] buff; buff = Bitmap2Bytes(bitmap); BitmapFactory.Options ontain = new BitmapFactory.Options(); ontain.inSampleSize = 7;//...

meixi_android的博客 4103

安卓bitmap与字节数组关系深入研究应用

Bitmap Byte 安卓

HAPP NEW JAVA 1825

AndroidBitmap对象字节流之间相互转换

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;

weixin_47884471的博客 551

AndroidBitmap对象字节流之间相互转换

将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把BitmapByte [code="java"]import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; ...

疯狂.NET 1132

Drawable、Bitmap、CanvasPaint的关系以及部分使用方法

刚刚开始学习Android平台,对于Drawable、Bitmap、CanvasPaint它们之间的概念不是很清楚,其实它们除了Drawable外早在Sun的J2ME中就已经出现了,但是在Android平台中,Bitmap、Canvas相关的都有所变化。 首先让我们理解下Android平台中的显示类是View,但是还提供了底层图形类android.graphics,今天所说的这些均为gra

fngy123的专栏 808

[Android]图片二进制与Bitmap、Drawable之间相互转换

1、将图片转换成二进制流 public static byte[] getBitmapByte(Bitmap bitmap){ ByteArrayOutputStream out = new ByteArrayOutputStream(); //参数1转换类型,参数2压缩质量,参数3字节流资源 bitmap.compress(Bitmap.CompressForma...

姚鑫国的博客 1万+

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.

Jeyden_827的博客 3051

Android 图片与Byte[]数组之间相互转换

载大佬 一、将图片转换byte[]数组 public static byte[] bitmap2Bytes(Bitmap bitmap){ if( null != bitmap ){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); //注意压缩pngjpg的格式质量 100 是质量

qq_42538455的博客 5053

DrawableBitmap转换

一、BitmapDrawableBitmap bm=xxx; //xxx根据你的情况获取BitmapDrawable bd=new BitmapDrawable(bm);因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。二、 DrawableBitmapBitmap对象后,可以将Drawable对象通过Android的SK库存成一个字节输出流,最终还可以保

Alen的博客 781

读取到的bitmap成buffer并保存

bitmap成buffer并保存,读取文件成buffer再bitmap

huangjinhu1的专栏 6413

AndroidBitmap,byte[],Drawable相互

一、相关概念 1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象 2、Canvas画布,绘图的目的区域,用于绘图 3、Bitmap位图,用于图的处理 4、Matrix矩阵 很多Android开发者可能发现,

LightSun----让成功成为可能! 2978

Android:Base64生产Bitmap压缩byte[]

最近在做微信分享的时候遇到了分享图片的大小限制问题,需要对图片进行压缩。在过程中遇到几个有趣的地方在此记录。 Bitmap.getByteCount的大小化为byte[]的大小差很多不是8倍,而是几十倍,我自测的为67倍 压缩Bitmap直接根据长宽比进行调用 createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHei...

stven_king的专栏 2460

Android开发之Bitmap各种转换-ImageUtils

对于在项目中,经常对图片进行处理,来满足不同功能需要,整理了部分常见需求代码,分享一下,后续不断更新中 bitmap字节数组 public static byte[] bitmap2Bytes(final Bitmap bitmap, final CompressFormat format) { if (bitmap == null) return null; ...

Rocky Kou的博客 5899

bitmapbyte android,Android bitmapbyte

private static byte[] bitmap2byte(Bitmap bmp) throwsException{return bitmap2byte(bmp, null);}private static byte[] bitmap2byte(Bitmap bmp, StarReverseConfig config) throwsException{Long s=System.curre...

weixin_29138993的博客 971

Android Drawable、Bitmapbyte[]之间转换

Drawable –> BitmapBitmap drawable2Bitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (draw

来啊 快活啊 1212
下一篇: Java中的volatile关键字
itheima6
博客等级 码龄14年 4粉丝 3原创
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值