android 將圖片內容解析成字節數組,將字節數組轉換為ImageView可調用的Bitmap對象,圖片縮放,把字節數組保存為一個文件,把Bitmap轉Byte
https://blog.csdn.net/z104207/article/details/6634774
- package com.bingo.util;
- 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
- * @Author HEH
- * @EditTime 2010-07-19 上午11:45:56
- */
- public static byte[] Bitmap2Bytes(Bitmap bm){
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
- return baos.toByteArray();
- }
- /**
- * 把字節數組保存為一個文件
- * @Author HEH
- * @EditTime 2010-07-19 上午11:45:56
- */
- 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;
- }
- }
最後更新:2017-04-02 17:51:24
上一篇:
android 登錄前檢查網絡狀態
下一篇:
android Gallery3D效果
Java 25天基礎-DAY 05-麵向對象封裝
TOMCAT修改默認編碼問題
android 反編譯 apk 分享 smali2java 1.0.0.558
互聯網企業安全高級指南3.11 業務持續性管理
2017 OpenStack峰會:k8S搶盡風頭?
《Linux From Scratch》第二部分:準備構建 第五章:構建臨時文件係統- 5.24. Grep-2.21
利用Doug Lea的並發包實現帶超時機製的線程池
揭開知識庫問答KB-QA的麵紗5·深度學習上篇
php之上傳小案例,根據時間:月日分創建目錄並隨機生成文件名
【大數據新手上路】“零基礎”係列課程--MySQL 數據整庫遷移到 MaxCompute