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
- /**
- * Picture and border combination
- * @param BM Original picture
- * @param res Border Resource
- * @return
- */
- private Bitmap Combinateframe (Bitmap BM, int[] res)
- {
- Bitmap bmp = Decodebitmap (res[0]);
- //Width height of border
- final int smallw = Bmp.getwidth ();
- final int smallh = Bmp.getheight ();
- //Width height of original picture
- final int bigw = Bm.getwidth ();
- final int bigh = Bm.getheight ();
- int wcount = (int) Math.ceil (BIGW * 1.0/SMALLW);
- int hcount = (int) Math.ceil (Bigh * 1.0/SMALLH);
- //The width height of the picture after the combination
- int neww = (Wcount + 2) * SMALLW;
- int NEWH = (Hcount + 2) * SMALLH;
- //Redefine size
- Bitmap Newbitmap = Bitmap.createbitmap (Neww, NEWH, config.argb_8888);
- Canvas canvas = new Canvas (NEWBITMAP);
- Paint p = new paint ();
- P.setcolor (color.transparent);
- Canvas.drawrect (new Rect (0, 0, NEWW, NEWH), p);
- Rect rect = new Rect (SMALLW, Smallh, NEWW-SMALLW, NEWH-SMALLH);
- Paint paint = new paint ();
- Paint.setcolor (Color.White);
- Canvas.drawrect (rect, paint);
- //Painting artwork
- Canvas.drawbitmap (BM, (NEWW-BIGW- 2 * smallw)/ 2 + SMALLW, (Newh-bigh- 2 * smallh)/ 2 + SMALLH, null);
- //Draw Border
- //Draw Four Corners
- int startw = NEWW-SMALLW;
- int starth = NEWH-SMALLH;
- Bitmap LEFTTOPBM = decodebitmap (res[0]); //upper left corner
- Bitmap LEFTBOTTOMBM = Decodebitmap (res[2]); //lower left corner
- Bitmap RIGHTBOTTOMBM = Decodebitmap (res[4]); //Lower right corner
- Bitmap RIGHTTOPBM = Decodebitmap (res[6]); //upper right corner
- Canvas.drawbitmap (LEFTTOPBM, 0, 0, null);
- Canvas.drawbitmap (LEFTBOTTOMBM, 0, Starth, null);
- Canvas.drawbitmap (RIGHTBOTTOMBM, STARTW, Starth, null);
- Canvas.drawbitmap (RIGHTTOPBM, STARTW, 0, null);
- Lefttopbm.recycle ();
- LEFTTOPBM = null;
- Leftbottombm.recycle ();
- LEFTBOTTOMBM = null;
- Rightbottombm.recycle ();
- RIGHTBOTTOMBM = null;
- Righttopbm.recycle ();
- RIGHTTOPBM = null;
- //Draw left and right border
- Bitmap LEFTBM = Decodebitmap (res[1]);
- Bitmap RIGHTBM = Decodebitmap (res[5]);
- for (int i = 0, length = hcount; i < length; i++)
- {
- int h = SMALLH * (i + 1);
- Canvas.drawbitmap (LEFTBM, 0, H, null);
- Canvas.drawbitmap (RIGHTBM, Startw, H, null);
- }
- Leftbm.recycle ();
- LEFTBM = null;
- Rightbm.recycle ();
- RIGHTBM = null;
- //Draw up/down border
- Bitmap BOTTOMBM = Decodebitmap (res[3]);
- Bitmap TOPBM = Decodebitmap (res[7]);
- for (int i = 0, length = wcount; i < length; i++)
- {
- int w = SMALLW * (i + 1);
- Canvas.drawbitmap (BOTTOMBM, W, Starth, null);
- Canvas.drawbitmap (TOPBM, W, 0, null);
- }
- Bottombm.recycle ();
- BOTTOMBM = null;
- Topbm.recycle ();
- TOPBM = null;
- Canvas.save (Canvas.all_save_flag);
- Canvas.restore ();
- return newbitmap;
- }
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
- /**
- * Capture the area of the 200x200 in the middle of the picture
- * @param BM
- * @return
- */
- Private Bitmap Cropcenter (Bitmap BM)
- {
- int dstwidth = 200;
- int dstheight = 200;
- int startwidth = (bm.getwidth ()-dstwidth)/2;
- int startheight = ((Bm.getheight ()-dstheight)/ 2);
- Rect src = new rect (Startwidth, startheight, Startwidth + dstwidth, startheight + dstheight);
- return Dividepart (BM, SRC);
- }
- /**
- * Cut Pictures
- * @param bmp Cut Pictures
- * @param src cut-off position
- * @return the picture after cut
- */
- Private Bitmap Dividepart (Bitmap bmp, Rect src)
- {
- int width = src.width ();
- int height = src.height ();
- Rect des = new Rect (0, 0, width, height);
- Bitmap croppedimage = bitmap.createbitmap (width, height, Bitmap.Config.RGB_565);
- Canvas canvas = new Canvas (croppedimage);
- Canvas.drawbitmap (BMP, Src, DES, null);
- return croppedimage;
- }
After processing the picture (the original picture or the picture above):
Android Image Processing series four-add a border to a picture (top)