android 文字图片合成

时间:2023-03-08 19:01:43

引用:http://blog.csdn.net/cq361106306/article/details/8142526

  1. 两种方法:
  2. 1.直接在图片上写文字
  3. String str = "PICC要写的文字";
  4. ImageView image = (ImageView) this.findViewById(R.id.ImageView);
  5. Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
  6. int width = photo.getWidth(), hight = photo.getHeight();
  7. System.out.println("宽"+width+"高"+hight);
  8. icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); //建立一个空的BItMap
  9. Canvas canvas = new Canvas(icon);//初始化画布绘制的图像到icon上
  10. Paint photoPaint = new Paint(); //建立画笔
  11. photoPaint.setDither(true); //获取跟清晰的图像采样
  12. photoPaint.setFilterBitmap(true);//过滤一些
  13. Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());//创建一个指定的新矩形的坐标
  14. Rect dst = new Rect(0, 0, width, hight);//创建一个指定的新矩形的坐标
  15. canvas.drawBitmap(photo, src, dst, photoPaint);//将photo 缩放或则扩大到 dst使用的填充区photoPaint
  16. Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//设置画笔
  17. textPaint.setTextSize(20.0f);//字体大小
  18. textPaint.setTypeface(Typeface.DEFAULT_BOLD);//采用默认的宽度
  19. textPaint.setColor(Color.RED);//采用的颜色
  20. //textPaint.setShadowLayer(3f, 1, 1,this.getResources().getColor(android.R.color.background_dark));//影音的设置
  21. canvas.drawText(str, 20, 26, textPaint);//绘制上去字,开始未知x,y采用那只笔绘制
  22. canvas.save(Canvas.ALL_SAVE_FLAG);
  23. canvas.restore();
  24. image.setImageBitmap(icon);
  25. saveMyBitmap(icon);
  26. 2.将两个图片合成
  27. onCreat方法里面{
  28. Bitmap mark = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
  29. Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
  30. Bitmap a = createBitmap(photo,mark);
  31. image.setImageBitmap(a);
  32. saveMyBitmap(a);
  33. }
  34. private Bitmap createBitmap( Bitmap src, Bitmap watermark )
  35. {
  36. String tag = "createBitmap";
  37. // Log.d( tag, "create a new bitmap" );
  38. if( src == null )
  39. {
  40. return null;
  41. }
  42. int w = src.getWidth();
  43. int h = src.getHeight();
  44. int ww = watermark.getWidth();
  45. int wh = watermark.getHeight();
  46. //create the new blank bitmap
  47. Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
  48. //创建一个新的和SRC长度宽度一样的位图
  49. Canvas cv = new Canvas( newb );
  50. //draw src into
  51. cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src
  52. //draw watermark into
  53. cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印
  54. //save all clip
  55. cv.save( Canvas.ALL_SAVE_FLAG );//保存
  56. //store
  57. cv.restore();//存储
  58. return newb;
  59. }
  60. //保存图片到data下面
  61. public void saveMyBitmap(Bitmap bmp){
  62. FileOutputStream fos = null;
  63. try {
  64. fos = openFileOutput("image1.jpg", Context.MODE_PRIVATE);
  65. bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  66. } catch (FileNotFoundException e) {
  67. } finally {
  68. if (fos != null) {
  69. try {
  70. fos.flush();
  71. fos.close();
  72. } catch (IOException e) {
  73. }
  74. }
  75. }
  76. }