File fImage = new File("/sdcard/dcim","beijing.jpeg"); FileOutputStream iStream = new FileOutputStream(fImage); * 取出Bitmap oriBmp oriBmp.compress(CompressFormat.JPEG, 100, iStream); int w = 320,h = 240; String mstrTitle = “感受Android帶給我們的新體驗”; Bitmap mbmpTest = Bitmap.createBitmap(w,h, Config.ARGB_8888); Canvas canvasTemp = new Canvas(mbmpTest); canvasTemp.drawColor(Color.WHITE); Paint p = new Paint(); String familyName = “宋體”; Typeface font = Typeface.create(familyName,Typeface.BOLD); p.setColor(Color.RED); p.setTypeface(font); p.setTextSize(22); canvasTemp.drawText(mstrTitle,0,100,p); 6.圖片水印的生成方法 生成水印的過程。其實分為三個環節:第一,載入原始圖片;第二,載入水印圖片;第三,保存新的圖片。 /** * 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; } 轉自:http://blog.csdn.net/hachirou/archive/2010/04/11/5473312.aspx
原文地址,http://www.devdiv.com/home.php?mod=space&uid=19970&do=blog&id=3868
//根據我自己的需要改進了一下
//獲取圖片縮小的圖片 public static Bitmap scaleBitmap(String src,int max) { //獲取圖片的高和寬 BitmapFactory.Options options = new BitmapFactory.Options(); //這一個設置使 BitmapFactory.decodeFile獲得的圖片是空的,但是會將圖片信息寫到options中 options.inJustDecodeBounds = true; BitmapFactory.decodeFile(src, options); // 計算比例 為了提高精度,本來是要640 這里縮為64 max=max/10; int be = options.outWidth / max; if(be%10 !=0) be+=10; be=be/10; if (be <= 0) be = 1; options.inSampleSize = be; //設置可以獲取數據 options.inJustDecodeBounds = false; //獲取圖片 return BitmapFactory.decodeFile(src, options); } // 加水印 也可以加文字 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的右下角畫入水印 } //加入文字 if(title!=null) { String familyName ="宋體"; Typeface font = Typeface.create(familyName,Typeface.BOLD); TextPaint textPaint=new TextPaint(); textPaint.setColor(Color.RED); textPaint.setTypeface(font); textPaint.setTextSize(22); //這里是自動換行的 StaticLayout layout = new StaticLayout(title,textPaint,w,Alignment.ALIGN_NORMAL,1.0F,0.0F,true); layout.draw(cv); //文字就加左上角算了 //cv.drawText(title,0,40,paint); } cv.save(Canvas.ALL_SAVE_FLAG);// 保存 cv.restore();// 存儲 return newb; }