zoukankan      html  css  js  c++  java
  • Android画图并保存图片(转载)

    Canvas是一个画布,你可以建立一个空白的画布,就直接new一个Canvas对象,不需要参数。
    也可以先使用BitmapFactory创建一个Bitmap对象,作为新的Canvas对象的参数,也就是说这个画布不是空白的,
    如果你想保存图片的话,最好是Bitmap是一个新的,而不是从某个文件中读入进来的,或者是Drawable对象。
     
    然后使用Canvas画第一张图上去,在画第二张图上去,最后使用Canvas.save(int flag)的方法进行保存,注意save方法里面的参数可以保存单个图层,
    如果是保存全部图层的 话使用 save( Canvas.ALL_SAVE_FLAG )。
     
    最后所有的信息都会保存在第一个创建的Bitmap中。代码如下:
    Java代码
    1. /** 
    2.     * create the bitmap from a byte array 
    3.     * 
    4.     * @param src the bitmap object you want proecss 
    5.     * @param watermark the water mark above the src 
    6.     * @return return a bitmap object ,if paramter's length is 0,return null 
    7.     */  
    8.    private Bitmap createBitmap( Bitmap src, Bitmap watermark )  
    9.    {  
    10.        String tag = "createBitmap";  
    11.        Log.d( tag, "create a new bitmap" );  
    12.        if( src == null )  
    13.        {  
    14.            return null;  
    15.        }  
    16.   
    17.        int w = src.getWidth();  
    18.        int h = src.getHeight();  
    19.        int ww = watermark.getWidth();  
    20.        int wh = watermark.getHeight();  
    21.        //create the new blank bitmap  
    22.        Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图  
    23.        Canvas cv = new Canvas( newb );  
    24.        //draw src into  
    25.        cv.drawBitmap( src, 00null );//在 0,0坐标开始画入src  
    26.        //draw watermark into  
    27.        cv.drawBitmap( watermark, w - ww + 5, h - wh + 5null );//在src的右下角画入水印  
    28.        //save all clip  
    29.        cv.save( Canvas.ALL_SAVE_FLAG );//保存  
    30.        //store  
    31.        cv.restore();//存储  
    32.        return newb;  
    33.    }  
     
     对图片进行缩小的方法:
    Java代码
    1. /** 
    2.     * lessen the bitmap 
    3.     * 
    4.     * @param src bitmap 
    5.     * @param destWidth the dest bitmap width 
    6.     * @param destHeigth 
    7.     * @return new bitmap if successful ,oherwise null 
    8.     */  
    9.    private Bitmap lessenBitmap( Bitmap src, int destWidth, int destHeigth )  
    10.    {  
    11.        String tag = "lessenBitmap";  
    12.        if( src == null )  
    13.        {  
    14.            return null;  
    15.        }  
    16.        int w = src.getWidth();//源文件的大小  
    17.        int h = src.getHeight();  
    18.        // calculate the scale - in this case = 0.4f  
    19.        float scaleWidth = ( ( float ) destWidth ) / w;//宽度缩小比例  
    20.        float scaleHeight = ( ( float ) destHeigth ) / h;//高度缩小比例  
    21.        Log.d( tag, "bitmap width is :" + w );  
    22.        Log.d( tag, "bitmap height is :" + h );  
    23.        Log.d( tag, "new width is :" + destWidth );  
    24.        Log.d( tag, "new height is :" + destHeigth );  
    25.        Log.d( tag, "scale width is  :" + scaleWidth );  
    26.        Log.d( tag, "scale height is  :" + scaleHeight );  
    27.        Matrix m = new Matrix();//矩阵  
    28.        m.postScale( scaleWidth, scaleHeight );//设置矩阵比例  
    29.        Bitmap resizedBitmap = Bitmap.createBitmap( src, 00, w, h, m, true );//直接按照矩阵的比例把源文件画入进行  
    30.        return resizedBitmap;  
    31.    } 
  • 相关阅读:
    PAT乙级-1037. 在霍格沃茨找零钱(20)
    PAT乙级-1041. 考试座位号(15)
    PAT乙级-1047. 编程团体赛(20)
    css3 实现 背景图片显示
    块级元素与行内元素(内联元素)的区别和联系
    JS变量
    导航条菜单的制作 滑动缓慢
    HTML中Id和Name的区别
    全面理解Javascript中Function对象的属性和方法
    理解盒子模型
  • 原文地址:https://www.cnblogs.com/mahang/p/2144406.html
Copyright © 2011-2022 走看看