android图像处理系列之四--给图片添加边框(上)

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法。

基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下角,右边,右上角,上边,其中左右上下只需要一个有效长度,就像重写水平进度条一样,只需要一个有效的长度,然后平铺,就达到了最后想要的效果,不错,左右上下边采用的也是这样的思路。也可以将八张图片组合在一起,然后读取整张图片,用代码裁剪,下面会给出相应的代码。下面的代码主要是给出第一种方法,后一种给出代码,有兴趣的可以自己试试。注意图片不要放到drawable目录下面,因为屏幕分辨率会影响图片的大小,所以最好是放在assets目录里面。下面代码为了简便所以没有那样做。后面一篇还会讲到另一种添加边框图片的方法。

下面贴图片:

原图片:


处理后:


代码(res参数为上面所说的八个边框组合图片资源):

/**  * 图片与边框组合  * @param bm 原图片  * @param res 边框资源  * @return  */ private Bitmap combinateFrame(Bitmap bm, int[] res) {  Bitmap bmp = decodeBitmap(res[0]);  // 边框的宽高  final int smallW = bmp.getWidth();  final int smallH = bmp.getHeight();    // 原图片的宽高  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);    // 组合后图片的宽高  int newW = (wCount + 2) * smallW;  int newH = (hCount + 2) * smallH;    // 重新定义大小  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);    // 绘原图  canvas.drawBitmap(bm, (newW - bigW - 2 * smallW) / 2 + smallW, (newH - bigH - 2 * smallH) / 2 + smallH, null);  // 绘边框  // 绘四个角  int startW = newW - smallW;  int startH = newH - smallH;  Bitmap leftTopBm = decodeBitmap(res[0]); // 左上角  Bitmap leftBottomBm = decodeBitmap(res[2]); // 左下角  Bitmap rightBottomBm = decodeBitmap(res[4]); // 右下角  Bitmap rightTopBm = decodeBitmap(res[6]); // 右上角    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;    // 绘左右边框  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;    // 绘上下边框  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; }


如果边框是在一张图片里面,下面给出从一张图片取中间200X200的区域。如何类似边框过多,可以将裁剪的信息写入到指定的文件,裁剪时可先将边框图片信息读取出来,然后再裁剪出边框。如果处理的原图片太大,可能会出内存溢出。可先将图片缩小到一定尺寸再处理,具体缩小方法,参见android图像处理系列之二--图片旋转、缩放、反转的图片缩放。

 /**  * 截取图片的中间的200X200的区域  * @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); }  /**  * 剪切图片  * @param bmp 被剪切的图片  * @param src 剪切的位置  * @return 剪切后的图片  */ 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; }


处理后图片(原图片还是上面的图片):


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/sdfsdfytre/article/details/84184879