码迷,mamicode.com
首页 > 其他好文 > 详细

PhotoshopManager(缩放图片file---->byte[])

时间:2016-04-22 11:53:38      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

package com.k1.doctor.media;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import android.graphics.BitmapFactory;

public class PhotoshopManager {
//计算缩放比例
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        int height = options.outHeight;
        int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    
//得到压缩后图片的byte[]
    public static byte[] getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, 480, 800);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bit = BitmapFactory.decodeFile(filePath, options);

        String name = UUID.randomUUID().toString();
        return bitmapToByte(bit, name);
    }
 //bitmap---->byte[]
    public static byte[] bitmapToByte(Bitmap bmp, String name) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();// 初始化一个流对象
        bmp.compress(CompressFormat.JPEG, 100, output);// 把bitmap100%高质量压缩 到
                                                        // output对象里

        bmp.recycle();// 自由选择是否进行回收

        byte[] result = output.toByteArray();// 转换成功了
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

 

PhotoshopManager(缩放图片file---->byte[])

标签:

原文地址:http://www.cnblogs.com/g-sheng/p/5420680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!