Android Image Processing Series VI-Add a border to a picture (bottom)-Picture overlay

Source: Internet
Author: User
Tags border image

Here is a way to add a lace border with a transparent picture overlay, and here's another way to add a lace border to a picture overlay. The previous method has a disadvantage, is to make a PNG picture, the picture volume will be very large, not the general large, three times times larger than the same JPG, if the project can ignore the size of the package, then you can use the previous treatment method, although this article can reduce the volume of the picture, but the processing is more complex, and I am not very mature research This article is for JPG border processing, but the disadvantage is not mature enough, only provide ideas.

Idea: You can make the border into a jpg picture, no border place, is generally the middle area to adjust to special colors, such as black. In the two pictures overlay, the border picture to do special processing, such as the black Point filter out, change the border image pixel transparency, you can complete the effect of the previous PNG picture.

Here's how it works:

+ =

At present, the code processing is not very good, non-black dots and black point of the junction did not do fuzzy processing, the specific method behind the article will be discussed. The same border picture is suggested to be placed in the assets directory.

Paste the following code:

[Java]View PlainCopy
  1. <pre name="code" class="java" >/**
  2. * Overlay Border Picture useful parts
  3. * @param bmp
  4. * @return
  5. */
  6. private Bitmap Alphalayer (Bitmap bmp)
  7. {
  8. int width = bmp.getwidth ();
  9. int height = bmp.getheight ();
  10. Bitmap Bitmap = bitmap.createbitmap (width, height, Bitmap.Config.RGB_565);
  11. //Border picture
  12. Bitmap overlay = Bitmapfactory.decoderesource (Mcontext.getresources (), r.drawable.black);
  13. int w = overlay.getwidth ();
  14. int h = overlay.getheight ();
  15. float ScaleX = width * 1F/W;
  16. float ScaleY = height * 1f/h;
  17. Matrix matrix = new Matrix ();
  18. Matrix.postscale (ScaleX, ScaleY);
  19. Bitmap overlaycopy = Bitmap.createbitmap (overlay, 0, 0, W, h, Matrix, true);
  20. int pixcolor = 0;
  21. int laycolor = 0;
  22. int newcolor = 0;
  23. int pixr = 0;
  24. int pixg = 0;
  25. int pixb = 0;
  26. int pixA = 0;
  27. int newr = 0;
  28. int NEWG = 0;
  29. int newb = 0;
  30. int Newa = 0;
  31. int Layr = 0;
  32. int layg = 0;
  33. int layb = 0;
  34. int layA = 0;
  35. float alpha = 0.3F;
  36. float Alphar = 0F;
  37. float alphag = 0F;
  38. float AlphaB = 0F;
  39. For (int i = 0; i < width; i++)
  40. {
  41. For (int k = 0; k < height; k++)
  42. {
  43. Pixcolor = Bmp.getpixel (i, k);
  44. Laycolor = Overlaycopy.getpixel (i, k);
  45. //Get the RGBA value of the original picture
  46. PIXR = Color.Red (Pixcolor);
  47. Pixg = Color.green (Pixcolor);
  48. PIXB = Color.Blue (Pixcolor);
  49. PixA = Color.alpha (Pixcolor);
  50. //Get the RGBA value of the border picture
  51. Layr = Color.Red (Laycolor);
  52. LAYG = Color.green (Laycolor);
  53. Layb = Color.Blue (Laycolor);
  54. LayA = Color.alpha (Laycolor);
  55. //colors similar to pure black dots
  56. if (Layr < && Layg < && Layb < )
  57. {
  58. Alpha = 1F;
  59. }
  60. Else
  61. {
  62. Alpha = 0.3F;
  63. }
  64. Alphar = Alpha;
  65. Alphag = Alpha;
  66. AlphaB = Alpha;
  67. //Two colors overlay
  68. NEWR = (int) (PIXR * Alphar + Layr * (1-alphar));
  69. NEWG = (int) (PIXG * alphag + LAYG * (1-alphag));
  70. Newb = (int) (PIXB * AlphaB + Layb * (1-alphab));
  71. LayA = (int) (PixA * alpha + LayA * (1-alpha));
  72. //value between 0~255
  73. NEWR = Math.min (255, Math.max (0, NEWR));
  74. NEWG = Math.min (255, Math.max (0, NEWG));
  75. Newb = Math.min (255, Math.max (0, newb));
  76. Newa = Math.min (255, Math.max (0, LayA));
  77. Newcolor = Color.argb (Newa, NEWR, NEWG, newb);
  78. Bitmap.setpixel (i, K, Newcolor);
  79. }
  80. }
  81. return bitmap;
  82. }


This way the picture processing is relatively slow, because the pixel points to a getpixel (), the composition of the new picture when a setpixel (). In addition you can use Getpixels (), and setpixels () method, this processing to a little faster, I have other similar effects of the treatment test, the back processing way than the front 3 times faster. Because this image overlay is for pixel processing, it is the next special effect processing series, because it involves the picture overlay, so put it in front. How to use it, it will be discussed later, and readers can study it themselves. The optimized code will be posted later, so please look forward to it.

Android Image Processing Series VI-Add a border to a picture (bottom)-Picture overlay

Related Article

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.