Android-ImageUtilsツールクラス

2124 ワード

画像関連ツールクラス
public class ImageUtils {
    public static boolean saveImage(Bitmap photo, String spath) {
        try {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(spath, false));
            photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        //   drawable    
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();

        //   drawable      
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        //      bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        //      bitmap    
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        //   drawable        
        drawable.draw(canvas);
        return bitmap;
    }

}