Android画图并保存图片

Android画图并保存图片 


项目中遇到了一个图片合成的问题,搞很长时间也没搞定,最后使用Canvas和Bitmap解决了画图的问题。

如果你想保存图片的话,最好是Bitmap是一个新的,而不是从某个文件中读入进来的,或者是Drawable对象。

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, 0, 0, null );//在 0,0坐标开始画入src   

26.        //draw watermark into   

27.        cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印   

28.        //save all clip   

29.        cv.save( Canvas.ALL_SAVE_FLAG );//保存   

30.        //store   

31.        cv.restore();//存储   

32.        return newb;   

33.    }   

 /**

     * create the bitmap from a byte array

     *

     * @param src the bitmap object you want proecss

     * @param watermark the water mark above the src

     * @return return a bitmap object ,if paramter's length is 0,return null

     */

    private Bitmap createBitmap( Bitmap src, Bitmap watermark )

    {

        String tag = "createBitmap";

        Log.d( tag, "create a new bitmap" );

        if( src == null )

        {

            return null;

        }

 

        int w = src.getWidth();

        int h = src.getHeight();

        int ww = watermark.getWidth();

        int wh = watermark.getHeight();

        //create the new blank bitmap

        Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图

        Canvas cv = new Canvas( newb );

        //draw src into

        cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src

        //draw watermark into

        cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印

        //save all clip

        cv.save( Canvas.ALL_SAVE_FLAG );//保存

        //store

        cv.restore();//存储

        return newb;

    }

 


Java代码 

1.   

2. /**  

3.     * lessen the bitmap  

4.     *  

5.     * @param src bitmap  

6.     * @param destWidth the dest bitmap width  

7.     * @param destHeigth  

8.     * @return new bitmap if successful ,oherwise null  

9.     */  

10.    private Bitmap lessenBitmap( Bitmap src, int destWidth, int destHeigth )   

11.    {   

12.        String tag = "lessenBitmap";   

13.        if( src == null )   

14.        {   

15.            return null;   

16.        }   

17.        int w = src.getWidth();//源文件的大小   

18.        int h = src.getHeight();   

19.        // calculate the scale - in this case = 0.4f   

20.        float scaleWidth = ( ( float ) destWidth ) / w;//宽度缩小比例   

21.        float scaleHeight = ( ( float ) destHeigth ) / h;//高度缩小比例   

22.        Log.d( tag, "bitmap width is :" + w );   

23.        Log.d( tag, "bitmap height is :" + h );   

24.        Log.d( tag, "new width is :" + destWidth );   

25.        Log.d( tag, "new height is :" + destHeigth );   

26.        Log.d( tag, "scale width is  :" + scaleWidth );   

27.        Log.d( tag, "scale height is  :" + scaleHeight );   

28.        Matrix m = new Matrix();//矩阵   

29.        m.postScale( scaleWidth, scaleHeight );//设置矩阵比例   

30.        Bitmap resizedBitmap = Bitmap.createBitmap( src, 0, 0, w, h, m, true );//直接按照矩阵的比例把源文件画入进行   

31.        return resizedBitmap;   

32.    }   

 

 /**

     * lessen the bitmap

     *

     * @param src bitmap

     * @param destWidth the dest bitmap width

     * @param destHeigth

     * @return new bitmap if successful ,oherwise null

     */

    private Bitmap lessenBitmap( Bitmap src, int destWidth, int destHeigth )

    {

        String tag = "lessenBitmap";

        if( src == null )

        {

            return null;

        }

        int w = src.getWidth();//源文件的大小

        int h = src.getHeight();

        // calculate the scale - in this case = 0.4f

        float scaleWidth = ( ( float ) destWidth ) / w;//宽度缩小比例

        float scaleHeight = ( ( float ) destHeigth ) / h;//高度缩小比例

        Log.d( tag, "bitmap width is :" + w );

        Log.d( tag, "bitmap height is :" + h );

        Log.d( tag, "new width is :" + destWidth );

        Log.d( tag, "new height is :" + destHeigth );

        Log.d( tag, "scale width is  :" + scaleWidth );

        Log.d( tag, "scale height is  :" + scaleHeight );

        Matrix m = new Matrix();//矩阵

        m.postScale( scaleWidth, scaleHeight );//设置矩阵比例

        Bitmap resizedBitmap = Bitmap.createBitmap( src, 0, 0, w, h, m, true );//直接按照矩阵的比例把源文件画入进行

        return resizedBitmap;

    }

 

  
原文网址: http://www.javaeye.com/topic/484289

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值