1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| private boolean addWatermarkBitmap(Bitmap bitmap, String str, int w, int h) { int destWidth = w; //此处的bitmap已经限定好宽高 int destHeight = h; Log.v("tag", "width = " + destWidth + " height = " + destHeight);
Bitmap icon = Bitmap.createBitmap(destWidth, destHeight, 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, bitmap.getWidth(), bitmap.getHeight());//创建一个指定的新矩形的坐标 Rect dst = new Rect(0, 0, destWidth, destHeight);//创建一个指定的新矩形的坐标 canvas.drawBitmap(bitmap, src, dst, photoPaint);//将photo 缩放或则扩大到 dst使用的填充区photoPaint
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//设置画笔 textPaint.setTextSize(destWidth / 20);//字体大小 textPaint.setTextAlign(Paint.Align.CENTER); textPaint.setTypeface(Typeface.DEFAULT_BOLD);//采用默认的宽度 textPaint.setAntiAlias(true); //抗锯齿 textPaint.setStrokeWidth(1); textPaint.setAlpha(30); // textPaint.setStyle(Paint.Style.STROKE); //空心 textPaint.setColor(Color.WHITE);//采用的颜色 // textPaint.setShadowLayer(1f, 0f, 3f, Color.LTGRAY); // textPaint.setShadowLayer(3f, 1, 1,getResources().getColor(android.R.color.white));//影音的设置 canvas.drawText(str, destWidth / 2, destHeight - 45, textPaint);//绘制上去字,开始未知x,y采用那只笔绘制 // canvas.save(Canvas.ALL_SAVE_FLAG); canvas.save(); canvas.restore(); bitmap.recycle(); btnOpenCamera.setImageBitmap(icon); return FileUtil.getInstance().saveMyBitmap(icon, tvResult.getText().toString()); //保存至文件 // return true; }
|