Android Image Processing series four-add a border to a picture (top)

Source: Internet
Author: User

Picture processing, sometimes you need to add a few borders to the picture, here is a method for adding a simple border to a picture.

The basic idea is: to cut the border picture into eight small pictures (picture size is best consistent, otherwise the processing will be very troublesome), respectively, corresponding to the upper left corner, left, lower left corner, bottom, right, right, upper right corner, top, which only need an effective length, like rewriting the level of progress bar, Only need a valid length, and then tiling, to achieve the final desired effect, good, the right and left on the bottom of the use of such ideas. You can also group eight pictures together, then read the entire picture, cut in code, the following will give the corresponding code. The following code mainly gives the first method, the latter gives the code, interested can try it yourself. Note that the picture should not be placed under the drawable directory, because the screen resolution will affect the size of the picture, so it is best to put in the assets directory. The following code is not done in order to be simple. The next article will also cover another way to add a border picture.

The following pictures are posted:

Original Picture:


After processing:


The code (res parameter is the eight-border combo picture resource mentioned above):

[Java]View PlainCopy
  1. /**
  2. * Picture and border combination
  3. * @param BM Original picture
  4. * @param res Border Resource
  5. * @return
  6. */
  7. private Bitmap Combinateframe (Bitmap BM, int[] res)
  8. {
  9. Bitmap bmp = Decodebitmap (res[0]);
  10. //Width height of border
  11. final int smallw = Bmp.getwidth ();
  12. final int smallh = Bmp.getheight ();
  13. //Width height of original picture
  14. final int bigw = Bm.getwidth ();
  15. final int bigh = Bm.getheight ();
  16. int wcount = (int) Math.ceil (BIGW * 1.0/SMALLW);
  17. int hcount = (int) Math.ceil (Bigh * 1.0/SMALLH);
  18. //The width height of the picture after the combination
  19. int neww = (Wcount + 2) * SMALLW;
  20. int NEWH = (Hcount + 2) * SMALLH;
  21. //Redefine size
  22. Bitmap Newbitmap = Bitmap.createbitmap (Neww, NEWH, config.argb_8888);
  23. Canvas canvas = new Canvas (NEWBITMAP);
  24. Paint p = new paint ();
  25. P.setcolor (color.transparent);
  26. Canvas.drawrect (new Rect (0, 0, NEWW, NEWH), p);
  27. Rect rect = new Rect (SMALLW, Smallh, NEWW-SMALLW, NEWH-SMALLH);
  28. Paint paint = new paint ();
  29. Paint.setcolor (Color.White);
  30. Canvas.drawrect (rect, paint);
  31. //Painting artwork
  32. Canvas.drawbitmap (BM, (NEWW-BIGW- 2 * smallw)/ 2 + SMALLW, (Newh-bigh- 2 * smallh)/ 2 + SMALLH, null);
  33. //Draw Border
  34. //Draw Four Corners
  35. int startw = NEWW-SMALLW;
  36. int starth = NEWH-SMALLH;
  37. Bitmap LEFTTOPBM = decodebitmap (res[0]); //upper left corner
  38. Bitmap LEFTBOTTOMBM = Decodebitmap (res[2]); //lower left corner
  39. Bitmap RIGHTBOTTOMBM = Decodebitmap (res[4]); //Lower right corner
  40. Bitmap RIGHTTOPBM = Decodebitmap (res[6]); //upper right corner
  41. Canvas.drawbitmap (LEFTTOPBM, 0, 0, null);
  42. Canvas.drawbitmap (LEFTBOTTOMBM, 0, Starth, null);
  43. Canvas.drawbitmap (RIGHTBOTTOMBM, STARTW, Starth, null);
  44. Canvas.drawbitmap (RIGHTTOPBM, STARTW, 0, null);
  45. Lefttopbm.recycle ();
  46. LEFTTOPBM = null;
  47. Leftbottombm.recycle ();
  48. LEFTBOTTOMBM = null;
  49. Rightbottombm.recycle ();
  50. RIGHTBOTTOMBM = null;
  51. Righttopbm.recycle ();
  52. RIGHTTOPBM = null;
  53. //Draw left and right border
  54. Bitmap LEFTBM = Decodebitmap (res[1]);
  55. Bitmap RIGHTBM = Decodebitmap (res[5]);
  56. for (int i = 0, length = hcount; i < length; i++)
  57. {
  58. int h = SMALLH * (i + 1);
  59. Canvas.drawbitmap (LEFTBM, 0, H, null);
  60. Canvas.drawbitmap (RIGHTBM, Startw, H, null);
  61. }
  62. Leftbm.recycle ();
  63. LEFTBM = null;
  64. Rightbm.recycle ();
  65. RIGHTBM = null;
  66. //Draw up/down border
  67. Bitmap BOTTOMBM = Decodebitmap (res[3]);
  68. Bitmap TOPBM = Decodebitmap (res[7]);
  69. for (int i = 0, length = wcount; i < length; i++)
  70. {
  71. int w = SMALLW * (i + 1);
  72. Canvas.drawbitmap (BOTTOMBM, W, Starth, null);
  73. Canvas.drawbitmap (TOPBM, W, 0, null);
  74. }
  75. Bottombm.recycle ();
  76. BOTTOMBM = null;
  77. Topbm.recycle ();
  78. TOPBM = null;
  79. Canvas.save (Canvas.all_save_flag);
  80. Canvas.restore ();
  81. return newbitmap;
  82. }


If the border is in a picture, here is an area where you take the middle 200x200 from a picture. How to like too many borders, you can write information about the clip to the specified file, cropping can first read the border picture information, and then cut out the border. If the original picture being processed is too large, a memory overflow may occur. You can reduce the image to a certain size and then process, the specific reduction method, see the Android image processing series of the second-image rotation, zoom, inverted image scaling.

[Java]View PlainCopy
  1. /**
  2. * Capture the area of the 200x200 in the middle of the picture
  3. * @param BM
  4. * @return
  5. */
  6. Private Bitmap Cropcenter (Bitmap BM)
  7. {
  8. int dstwidth = 200;
  9. int dstheight = 200;
  10. int startwidth = (bm.getwidth ()-dstwidth)/2;
  11. int startheight = ((Bm.getheight ()-dstheight)/ 2);
  12. Rect src = new rect (Startwidth, startheight, Startwidth + dstwidth, startheight + dstheight);
  13. return Dividepart (BM, SRC);
  14. }
  15. /**
  16. * Cut Pictures
  17. * @param bmp Cut Pictures
  18. * @param src cut-off position
  19. * @return the picture after cut
  20. */
  21. Private Bitmap Dividepart (Bitmap bmp, Rect src)
  22. {
  23. int width = src.width ();
  24. int height = src.height ();
  25. Rect des = new Rect (0, 0, width, height);
  26. Bitmap croppedimage = bitmap.createbitmap (width, height, Bitmap.Config.RGB_565);
  27. Canvas canvas = new Canvas (croppedimage);
  28. Canvas.drawbitmap (BMP, Src, DES, null);
  29. return croppedimage;
  30. }


After processing the picture (the original picture or the picture above):

Android Image Processing series four-add a border to a picture (top)

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.