Android Watermark a picture

Source: Internet
Author: User

Speaking of the watermark is actually very simple, is to draw on a picture of another picture or text

The implementation is as follows:

[Java]View Plaincopy
  1. Package Com.tang.watermark;
  2. Import android.app.Activity;
  3. Import Android.graphics.Bitmap;
  4. Import Android.graphics.BitmapFactory;
  5. Import Android.graphics.Canvas;
  6. Import Android.graphics.PixelFormat;
  7. Import Android.graphics.Bitmap.Config;
  8. Import android.graphics.drawable.BitmapDrawable;
  9. Import android.graphics.drawable.Drawable;
  10. Import Android.os.Bundle;
  11. Import Android.view.View;
  12. Import Android.view.View.OnClickListener;
  13. Import Android.widget.Button;
  14. Import Android.widget.ImageView;
  15. Public class Mainactivity extends Activity {
  16. ImageView ImageView;
  17. Bitmap Mark;
  18. @Override
  19. protected void OnCreate (Bundle savedinstancestate) {
  20. super.oncreate (savedinstancestate);
  21. Setcontentview (R.layout.activity_main);
  22. ImageView = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);
  23. Imageview.setimageresource (R.drawable.heihei);
  24. Mark = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);
  25. Button button = (button) Findviewbyid (R.id.button1);
  26. Button.setonclicklistener (new Onclicklistener () {
  27. @Override
  28. public void OnClick (View v) {
  29. //TODO auto-generated method stub
  30. Bitmap temp = watermark (Drawabletobitmap (imageview.getdrawable ()), Mark,100);
  31. Imageview.setimagebitmap (temp);
  32. }
  33. });
  34. }
  35. /** 
  36. * Convert Bitmap to Drawable
  37. * @param bitmap
  38. * @return
  39. */
  40. public static drawable bitmaptodrawable (Bitmap Bitmap)
  41. {
  42. bitmapdrawable bd = new bitmapdrawable (bitmap);
  43. return BD;
  44. }
  45. /** 
  46. * @param src
  47. * @param BG
  48. * @param color
  49. * @return
  50. */
  51. public static Bitmap watermark (Bitmap src,bitmap mark,int x,int y)
  52. {
  53. int w = src.getwidth ();
  54. int h = src.getheight ();
  55. Bitmap newb = Bitmap.createbitmap (W, H, config.argb_8888);
  56. Canvas CV = new canvas (NEWB);
  57. Cv.drawbitmap (SRC, 0, 0, null);
  58. Cv.drawbitmap (mark,x, y, null);
  59. Cv.save (Canvas.all_save_flag); //Save
  60. Cv.restore (); //Storage
  61. return newb;
  62. }
  63. /** 
  64. * Drawable converted to bitmap
  65. * @param drawable
  66. * @return
  67. */
  68. public static Bitmap Drawabletobitmap (drawable drawable) {
  69. //Take drawable of the length of the width
  70. int w = drawable.getintrinsicwidth ();
  71. int h = drawable.getintrinsicheight ();
  72. //Take drawable color format
  73. Bitmap.config Config = drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888
  74. : Bitmap.Config.RGB_565;
  75. Bitmap Bitmap = Bitmap.createbitmap (w, H, config);
  76. Canvas canvas = new canvas (bitmap);
  77. Drawable.setbounds (0, 0, W, h);
  78. Drawable.draw (canvas);
  79. return bitmap;
  80. }

Android Watermark a picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.