海報工廠之(一)android 如何給圖片添加浮水印和文字

來源:互聯網
上載者:User

標籤:tle   res   log   scale   turn   term   .net   ons   option   

在Android中如何給圖片添加浮水印,下面截取了部分核心代碼,僅供參考:

 

/**
     * 擷取圖片縮小的圖片
     * @param src
     * @return
     */
    public static Bitmap scaleBitmap(String src)
    {
        //擷取圖片的高和寬
        BitmapFactory.Options options = new BitmapFactory.Options();
        //這一個設定使 BitmapFactory.decodeFile獲得的圖片是空的,但是會將圖片資訊寫到options中
        options.inJustDecodeBounds = true;        
        BitmapFactory.decodeFile(src, options); 
        options.inSampleSize = 1;
        //設定可以擷取資料
        options.inJustDecodeBounds = false;
        //擷取圖片
        return BitmapFactory.decodeFile(src, options);        
    }
    /**
     * 加浮水印 也可以加文字
     * @param src
     * @param watermark
     * @param title
     * @return
     */
    public static Bitmap watermarkBitmap(Bitmap src, Bitmap watermark,
            String title) {
        if (src == null) {
            return null;
        }
        int w = src.getWidth();
        int h = src.getHeight(); 
        //需要處理圖片太大造成的記憶體超過的問題,這裡我的圖片很小所以不寫相應代碼了        
        Bitmap newb= Bitmap.createBitmap(w, h, Config.ARGB_8888);// 建立一個新的和SRC長度寬度一樣的位元影像
        Canvas cv = new Canvas(newb);
        cv.drawBitmap(src, 0, 0, null);// 在 0,0座標開始畫入src    
        Paint paint=new Paint();
        //加入圖片
        if (watermark != null) {
            int ww = watermark.getWidth();
            int wh = watermark.getHeight();
            paint.setAlpha(50);
//            cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, paint);// 在src的右下角畫入浮水印         
            cv.drawBitmap(watermark, 0, 0, paint);// 在src的左上方畫入浮水印      
        }else{
        Log.i("i", "water mark failed");
        }
        //加入文字
        if(title!=null)
        {
            String familyName ="宋體";
            Typeface font = Typeface.create(familyName,Typeface.NORMAL);            
            TextPaint textPaint=new TextPaint();
            textPaint.setColor(Color.RED);
            textPaint.setTypeface(font);
            textPaint.setTextSize(40);
            //這裡是自動換行的
//            StaticLayout layout = new StaticLayout(title,textPaint,w,Alignment.ALIGN_OPPOSITE,1.0F,0.0F,true);
//            layout.draw(cv);
            //文字就加左上方算了
            cv.drawText(title,w-400,h-40,textPaint); 
        }
        cv.save(Canvas.ALL_SAVE_FLAG);// 儲存
        cv.restore();// 儲存
        return newb;
    }

海報工廠之(一)android 如何給圖片添加浮水印和文字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.