android图片加水印,文字

  1. 云栖社区>
  2. 博客>
  3. 正文

android图片加水印,文字

范大脚脚 2017-11-21 16:16:00 浏览926
展开阅读全文
两种方法:

1.直接在图片上写文字

 

        String str = "PICC要写的文字";

       ImageView image = (ImageView) this.findViewById(R.id.ImageView);
       Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
       int width = photo.getWidth(), hight = photo.getHeight();
       System.out.println("宽"+width+"高"+hight);
       icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); //建立一个空的BItMap  
       Canvas canvas = new Canvas(icon);//初始化画布绘制的图像到icon上  
        
       Paint photoPaint = new Paint(); //建立画笔  
       photoPaint.setDither(true); //获取跟清晰的图像采样  
       photoPaint.setFilterBitmap(true);//过滤一些  
        
       Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());//创建一个指定的新矩形的坐标  
       Rect dst = new Rect(0, 0, width, hight);//创建一个指定的新矩形的坐标  
       canvas.drawBitmap(photo, src, dst, photoPaint);//将photo 缩放或则扩大到 dst使用的填充区photoPaint  
        
       Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//设置画笔  
       textPaint.setTextSize(20.0f);//字体大小  
       textPaint.setTypeface(Typeface.DEFAULT_BOLD);//采用默认的宽度  
       textPaint.setColor(Color.RED);//采用的颜色  
       //textPaint.setShadowLayer(3f, 1, 1,this.getResources().getColor(android.R.color.background_dark));//影音的设置  
       canvas.drawText(str, 20, 26, textPaint);//绘制上去字,开始未知x,y采用那只笔绘制 
       canvas.save(Canvas.ALL_SAVE_FLAG); 
       canvas.restore(); 
       image.setImageBitmap(icon);
       saveMyBitmap(icon);

 

2.将两个图片合成

      onCreat方法里面{

         Bitmap mark = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon); 
            Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);

        Bitmap a = createBitmap(photo,mark);
            image.setImageBitmap(a);
            saveMyBitmap(a);

       }

 

      

    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;

    }

 

//保存图片到data下面

    

    public void saveMyBitmap(Bitmap bmp){
     FileOutputStream fos = null;
        try {
            fos = openFileOutput("image1.jpg", Context.MODE_PRIVATE);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        } catch (FileNotFoundException e) {
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                }
            }
        }

    }
复制代码

 


本文转自java豆子博客园博客,原文链接:http://www.cnblogs.com/error404/archive/2013/01/25/2876896.html,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
范大脚脚
+ 关注