IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android中Bitmap对象和字节流之间的相互转换(转) -> 正文阅读

[移动开发]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;
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;
? ? }
}

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-28 07:56:17  更:2021-07-28 07:57:29 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 0:20:39-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码