android圖片加水印,文字


兩種方法:

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) {
                }
            }
        }

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM