zoukankan      html  css  js  c++  java
  • android bitmap转换

    1. public class BitmapHelper {  
    2.     //图片转byte   
    3.     public static byte[] Bitmap2BytesJpeg(Bitmap bitmap) {  
    4.         byte[] result = null;  
    5.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
    6.         if (bitmap != null) {  
    7.             try {  
    8.                 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);  
    9.                 result = bos.toByteArray();  
    10.                 bos.close();  
    11.             } catch (OutOfMemoryError ex) {  
    12.                 ex.printStackTrace();  
    13.             } catch (IOException e) {  
    14.                 e.printStackTrace();  
    15.             }  
    16.         }  
    17.         return result;  
    18.     }  
    19.   
    20.     public static byte[] Bitmap2BytesPng(Bitmap bitmap) {  
    21.         byte[] result = null;  
    22.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
    23.         if (bitmap != null) {  
    24.             try {  
    25.                 bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);  
    26.                 result = bos.toByteArray();  
    27.                 bos.close();  
    28.             } catch (OutOfMemoryError ex) {  
    29.                 ex.printStackTrace();  
    30.             } catch (IOException e) {  
    31.                 e.printStackTrace();  
    32.             }  
    33.         }  
    34.         return result;  
    35.     }  
    36.   
    37.     public static Bitmap Bytes2Bitmap(byte[] data) {  
    38.         Bitmap bitmap = null;  
    39.         if (data != null) {  
    40.             try {  
    41.                 BitmapFactory.Options options = new BitmapFactory.Options();  
    42.                 options.inPreferredConfig = Bitmap.Config.RGB_565;  
    43.                 bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,  
    44.                         options);  
    45.             } catch (OutOfMemoryError e) {  
    46.                 e.printStackTrace();  
    47.             }  
    48.         }  
    49.         return bitmap;  
    50.     }  
    51.   
    52.     //按比例生成图片   
    53.     public static Bitmap bitmapToFixedBitmap(Bitmap bitmap, int width,  
    54.             int height) {  
    55.         Bitmap result = null;  
    56.         if (bitmap != null) {  
    57.             int x = bitmap.getWidth();  
    58.             int y = bitmap.getHeight();  
    59.             float f1 = width / x;  
    60.             float f2 = height / y;  
    61.             Matrix matrix = new Matrix();  
    62.             matrix.postScale(f1, f2);  
    63.             Bitmap.createBitmap(bitmap, 00, x, y, matrix, true);  
    64.         }  
    65.         return result;  
    66.     }  
    67.   
    68.     //位图缩放   
    69.     public static Bitmap bitmapToScaleBitmap(Bitmap bitmap, int width,  
    70.             int height) {  
    71.         Bitmap result = null;  
    72.         if (bitmap != null) {  
    73.             int x = bitmap.getWidth();  
    74.             int y = bitmap.getHeight();  
    75.             float f1 = width / Math.max(x, y);  
    76.             float f2 = height / Math.max(x, y);  
    77.             Matrix matrix = new Matrix();  
    78.             matrix.postScale(f1, f2);  
    79.             Bitmap.createBitmap(bitmap, 00, x, y, matrix, true);  
    80.         }  
    81.         return result;  
    82.     }  
    83.   
    84.     public static Bitmap drawableToBitmap(Drawable drawable) {  
    85.         Bitmap bitmap = null;  
    86.         int width = drawable.getIntrinsicWidth();  
    87.         int height = drawable.getIntrinsicHeight();  
    88.         if (drawable.getOpacity() != -1) {  
    89.             bitmap = Bitmap  
    90.                     .createBitmap(  
    91.                             width,  
    92.                             height,  
    93.                             drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
    94.                                     : Bitmap.Config.RGB_565);  
    95.             Canvas canvas = new Canvas(bitmap);  
    96.             drawable.setBounds(00, width, height);  
    97.             drawable.draw(canvas);  
    98.         }  
    99.         return bitmap;  
    100.     }  
    101.   
    102.     public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {  
    103.         return getRoundCornerBitmap(bitmap, 6.0F);  
    104.     }  
    105.   
    106.     /** 
    107.      * 生成圆角图片 
    108.      * @param bitmap 
    109.      * @param radius 
    110.      */  
    111.     public static Bitmap getRoundCornerBitmap(Bitmap bitmap, float radius) {  
    112.         Bitmap result = null;  
    113.         if (bitmap != null) {  
    114.             result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),  
    115.                     Bitmap.Config.ARGB_8888);  
    116.             Canvas canvas = new Canvas(result);  
    117.             Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
    118.             paint.setColor(-12434878);  
    119.             Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());  
    120.             RectF rectF = new RectF(rect);  
    121.             canvas.drawRoundRect(rectF, radius, radius, paint);  
    122.             paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
    123.             canvas.drawBitmap(bitmap, rect, rect, paint);  
    124.         }  
    125.   
    126.         return result;  
    127.     }  
    128.   
    129.     /** 
    130.      * 生成带倒影图片 
    131.      */  
    132.     public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {  
    133.         final int reflectionGap = 4;  
    134.         int width = bitmap.getWidth();  
    135.         int height = bitmap.getHeight();  
    136.         Matrix matrix = new Matrix();  
    137.         matrix.preScale(1, -1);  
    138.         Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,  
    139.                 width, height / 2, matrix, false);  
    140.   
    141.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  
    142.                 (height + height / 2), Bitmap.Config.ARGB_8888);  
    143.         ;  
    144.         Canvas canvas = new Canvas(bitmapWithReflection);  
    145.         canvas.drawBitmap(bitmap, 00null);  
    146.         Paint paint = new Paint();  
    147.         canvas.drawRect(0, height, width, height + reflectionGap, paint);  
    148.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
    149.         Paint p = new Paint();  
    150.         LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,  
    151.                 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,  
    152.                 0x00ffffff, TileMode.CLAMP);  
    153.         p.setShader(shader);  
    154.         p.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
    155.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
    156.                 + reflectionGap, p);  
    157.         return bitmapWithReflection;  
    158.     }  
    159. }  
  • 相关阅读:
    古代军队的官的从大到小的排序
    [转]DAO、RDO、ADO、OLE DB 、ODBC and JDB
    JSP页面之间参数传递中文出现乱码
    重置VS2008插件环境
    PB7中调用VC6的DLL
    Visual Studio统计有效代码行数
    php.ini 中文版
    IDEA Plugin JB* Components
    [转]你还在为怎么查看字节码指令而担忧吗?
    战地2, 2142解决Win10运行闪退问题
  • 原文地址:https://www.cnblogs.com/wangshengl9263/p/2835290.html
Copyright © 2011-2022 走看看