bitmap相关工具类

一,bitmap工具

封装了以下方法:

1,获取activity屏幕截图,保存为图片文件

2,从文件中获取截图,返回bitmap对象

package com.ctbri.weather.utils;



import java.io.File;

import java.io.FileOutputStream;



import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.BitmapFactory.Options;

import android.graphics.Rect;

import android.os.Environment;

import android.view.View;



public class BitmapUtil {

    /**

     * 从文件中加载图片的缩略图

     * 

     * @param path

     *            文件路径

     * @return

     */

    public static Bitmap loadThumbnail(String path) {



        BitmapFactory.Options opts = new Options();

        opts.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(path, opts);

        int width = opts.outWidth;

        int height = opts.outHeight;

        int windowwidth = 48;

        int windowheigth = 85;



        int scalex = width / windowwidth;

        int scaley = height / windowheigth;



        if (scalex > scaley && scaley > 1) { // 姘村钩鏂瑰悜鐨勭缉鏀炬瘮渚嬫瘮杈冨ぇ

            opts.inSampleSize = scalex;

        }

        if (scaley > scalex && scalex > 1) { // 绔栫洿鏂瑰悜鐨勭缉鏀炬瘮渚嬫瘮杈冨ぇ

            opts.inSampleSize = scaley;

        }

        opts.inJustDecodeBounds = false;

        Bitmap bitmap = BitmapFactory.decodeFile(path, opts);



        return bitmap;



    }

    /**

     * 获取acitivy屏幕截图工具

     * @param activity需要截图的ctivity

     * @param v 该acitivy上的一个view控件

     * @return 返回截图的存储路径

     */

    public static String getscreen(Activity activity,View v) {

        String fname = "/mnt/sdcard/";



        File file = new File(

                Environment

                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),

                "eweather");

        if (!file.exists()) {

            file.mkdirs();

        }

        fname = new File(file.getPath() + File.separator 

                + "shareimg" + ".jpg").getAbsolutePath();

        View view = v.getRootView();

        view.setDrawingCacheEnabled(true);

        view.buildDrawingCache();

        Bitmap bitmap = view.getDrawingCache();



        if (bitmap != null) {

            try {

                FileOutputStream out = new FileOutputStream(fname);

                //鑾峰彇鐘舵�鏍忛珮搴�

                Rect frame = new Rect(); 

                activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 

                int statusBarHeight = frame.top; 

                //鑾峰彇灞忓箷闀垮拰楂�

                 int width = activity.getWindowManager().getDefaultDisplay().getWidth(); 

                int height = activity.getWindowManager().getDefaultDisplay().getHeight(); 

                //鍘绘帀鏍囬鏍�

                bitmap = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width, height - statusBarHeight); 

                view.destroyDrawingCache(); 

                

                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

                bitmap.recycle();

                bitmap=null;



            } catch (Exception e) {

                e.printStackTrace();

            }



        } else {

            System.out.println("bitmapis NULL!");

        }

        return fname;



    }



}

二、实现bitmap,drawable,byte【】之间的转换

package com.ctbri.weather.utils;



import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;



import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.PixelFormat;

import android.graphics.drawable.Drawable;



public class ByteUtil {



    public static byte[] read(String path) {

        ByteArrayOutputStream outputStream = null;

        try {

            File imageFile = new File(path);

            FileInputStream inputStream = new FileInputStream(imageFile);

            outputStream = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];

            int len = 0;

            while ((len = inputStream.read(buffer)) != -1) {

                outputStream.write(buffer, 0, len);

            }

            inputStream.close();

        } catch (FileNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return outputStream.toByteArray();

    }



    public static byte[] Bitmap2Bytes(Bitmap bm) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

        return baos.toByteArray();

    }



    public synchronized static byte[] drawableToByte(Drawable drawable) {



        if (drawable != null) {

            Bitmap bitmap = Bitmap

                    .createBitmap(

                            drawable.getIntrinsicWidth(),

                            drawable.getIntrinsicHeight(),

                            drawable.getOpacity() != PixelFormat.JPEG ? Bitmap.Config.ARGB_8888

                                    : Bitmap.Config.RGB_565);

            Canvas canvas = new Canvas(bitmap);

            bitmap.recycle();

            bitmap = null;

            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),

                    drawable.getIntrinsicHeight());

            drawable.draw(canvas);

            int size = bitmap.getWidth() * bitmap.getHeight() * 4;

            ByteArrayOutputStream baos = new ByteArrayOutputStream(size);

            bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos);

            byte[] imagedata = baos.toByteArray();

            return imagedata;

        }

        return null;

    }

}

 关于这一点,自己的另外一篇博客有更详细的讨论,包括view,bitmap,drawable,byte[]之间的相互转换

http://www.cnblogs.com/bobodeboke/archive/2013/05/09/3069415.html

 

 

你可能感兴趣的:(bitmap)