Java 图片设置圆角(设置边框,旁白)

/**
     * 图片设置圆角
     * @param srcImage
     * @param radius
     * @param border
     * @param padding
     * @return
     * @throws IOException
     */
    public static BufferedImage setRadius(BufferedImage srcImage, int radius, int border, int padding) throws IOException{
        int width = srcImage.getWidth();
        int height = srcImage.getHeight();
        int canvasWidth = width + padding * 2;
        int canvasHeight = height + padding * 2;

        BufferedImage image = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = image.createGraphics();
        gs.setComposite(AlphaComposite.Src);
        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setColor(Color.WHITE);
        gs.fill(new RoundRectangle2D.Float(0, 0, canvasWidth, canvasHeight, radius, radius));
        gs.setComposite(AlphaComposite.SrcAtop);
        gs.drawImage(setClip(srcImage, radius), padding, padding, null);
        if(border !=0){
            gs.setColor(Color.GRAY);
            gs.setStroke(new BasicStroke(border));
            gs.drawRoundRect(padding, padding, canvasWidth - 2 * padding, canvasHeight - 2 * padding, radius, radius);
        }
        gs.dispose();
        return image;
    }

    /**
     * 图片设置圆角
     * @param srcImage
     * @return
     * @throws IOException
     */
    public static BufferedImage setRadius(BufferedImage srcImage) throws IOException{
        int radius = (srcImage.getWidth() + srcImage.getHeight()) / 6;
        return setRadius(srcImage, radius, 2, 5);
    }

    /**
     * 图片切圆角
     * @param srcImage
     * @param radius
     * @return
     */
    public static BufferedImage setClip(BufferedImage srcImage, int radius){
        int width = srcImage.getWidth();
        int height = srcImage.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = image.createGraphics();

        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, radius, radius));
        gs.drawImage(srcImage, 0, 0, null);
        gs.dispose();
        return image;
    }
时间: 2024-10-18 03:39:11

Java 图片设置圆角(设置边框,旁白)的相关文章

iOS之用xib给控件设置圆角、边框效果

xib中为各种控件设置圆角 通过代码的方式设置 @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *myView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.myView.layer.masksToBounds = YES; self.myView.layer.cornerRa

UIButton设置圆角和边框及边框颜色

[box.actionButton.layer setMasksToBounds:YES]; [box.actionButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径 //边框宽度 [box.actionButton.layer setBorderWidth:1.0]; //边框颜色 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGColorRef colorref = CG

xib设置lable设置圆角和边框,颜色

//设置圆角 layer.cornerRadius ,注意该 key 对应 Value 的 type 应该设置为 String layer.masksToBounds ,注意该 key 对应 Value 的 type 应该设置为 Boolean , 当右侧出现对号时为YES *注意*:经过测试,UILabel 必须设置设置 masksToBounds 这一键值对,才会出现圆角效果:UIButton.UIView.UIImageView 只需设置 layer.cornerRadius 这一键值对就

Bootstrap3基础 img-rounded 给图片加圆角的边框

礼悟:    公恒学思合行悟,尊师重道存感恩.叶见寻根三返一,江河湖海同一体.          虚怀若谷良心主,愿行无悔给最苦.读书锻炼养身心,诚劝且行且珍惜.              ide:visual studio 2017             browser:Firefox / Chrome   bootstrap:3.3.7 代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8&qu

Java 图片切圆角,消除锯齿

public static BufferedImage setBorderRadius(BufferedImage srcImage, int radius){ int width = srcImage.getWidth(); int height = srcImage.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d =

给图片设置圆角和圆形边框,添加点击事件

//1.创建图片 _userImage = [[UIImageView alloc]init]; //2.设置图片圆角 _userImage.layer.cornerRadius = 33; _userImage.layer.masksToBounds = YES; //3.设置圆形边框 CALayer *layer = [_userImage layer]; layer.borderColor = [[UIColor whiteColor] CGColor]; layer.borderWidt

iOS设置圆角及圆形图片

方方正正的样式往往会显得很生硬,而圆角的样式会让人产生别样的亲切感,现在越来越多地用到圆角,诸如用户头像之类的图片也大都用圆形来显示,本文就介绍如何设置按钮.文本框的圆角以及制作圆形的图片. 先来看看效果图: 如效果图所示,我们制作了一个圆形的头像,一个完全半圆的圆角按钮,一个小圆角的按钮,以及一个带边框且边框为圆角的label. 大概思路如下: View都有一个layer的属性,我们正是通过layer的一些设置来达到圆角的目的,因此诸如UIImageView.UIButton.UILabel等

一起学android之设置资源图片为圆角图片 (28)

效果图: 参看以下代码: public class MainActivity extends Activity { private ImageView imageView1; private ImageView imageView2; Bitmap mBitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.

iOS图片圆角设置的正确姿势

最近从上一家公司辞职,一直处于待业状态,顺便再家里带带闺女,今天有时间,所以写点东西,希望刚开始接触iOS开发的小白能够有所收获. 在iOS开发中圆角图片很常见,但是我们一般在设置图片圆角的时候几乎都是这样设置的: UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)]; image.layer.cornerRadius = 25.0; image.layer.masksToBoun