java图片工具(加3D效果字体和图片)

本文介绍了一种使用Java进行图像处理的方法,包括如何通过指定参数在图片上添加文字和Logo。具体步骤涉及读取图片文件、定义文字样式、添加文字内容及Logo,并最终保存为JPEG格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//GoodFont只是一个标记字体特性的bean

//加字体(3D效果)
 public static void createJpgByGoodFont(String inputPath,String outputPath,GoodFont[] goodfont){
  BufferedImage image;
  try {
   image = ImageIO.read(new FileInputStream(inputPath));
   
   if(goodfont==null)return;
  
   FileOutputStream jpgOut = new FileOutputStream(outputPath);
   Graphics2D g = image.createGraphics();
   
   for(GoodFont gf:goodfont){
    int style=gf.getStyle();
    int size=gf.getSize();
    String name=gf.getName();
    int leftX=gf.getLeft();
    int topY=gf.getTop();
    int width=gf.getWidth();
    int high=gf.getHigh();
    int textDirect=gf.getTextDirect();
    String text=gf.getText();
    String align=gf.getAlign();
    Color color=gf.getColor();
    Color fillColor=gf.getFillColor();
    Color drawColor=gf.getDrawColor();
    
    
    Font font = new Font(name, style, size);
    g.setFont(font);
    
    
  
    if(fillColor!=null){
     g.setColor(fillColor);
     g.fill3DRect(leftX, topY, width, high, true);
    }
    
    if(drawColor!=null){
     g.setColor(drawColor);
     for(int i=0;i<gf.getDrawBorder();i++)
     g.draw3DRect(leftX, topY, width, high+i, true);
    }
    
    
    //设置背景色,仅当背景无图片时使用
//    g.setBackground(Color.YELLOW);
//    g.setColor(Color.BLACK);
    g.setColor(color);
    if(align.equalsIgnoreCase("right")){
     leftX=leftX+width-size*text.length();
    }
    else if(align.equalsIgnoreCase("center")){
     leftX=leftX+(width-size*text.length())/2;
    }
    int j = size;
    System.out.println("text="+text+",leftX="+leftX+",topY="+topY+",size="+size+",color="+color+",border="+gf.getDrawBorder());
    if(textDirect==1)//横排的文字
     g.drawString(text,leftX, topY+j);
    else{//竖排的文字
     for(int i=0;i<text.length();i++)
     {
      g.drawString(text.substring(i,i+1),leftX,topY+j);
      j+=size;
     }
    }
   }
   g.dispose();
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(jpgOut);
   encoder.encode(image);
   jpgOut.flush();
   jpgOut.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }catch (Throwable e) {
   e.printStackTrace();
  }
 }

 


//加图片
 public static void createJpgByLogo(String inputPath,String outputPath,String logoPath,int x,int y){
  BufferedImage image;
  try {
   image = ImageIO.read(new FileInputStream(inputPath));
   
   if(logoPath==null)return;
  
   FileOutputStream jpgOut = new FileOutputStream(outputPath);
   Graphics2D g = image.createGraphics();
   g.drawImage(ImageIO.read(new FileInputStream(logoPath)), x, y, null);
   
   g.dispose();
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(jpgOut);
   encoder.encode(image);
   jpgOut.flush();
   jpgOut.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }catch (Throwable e) {
   e.printStackTrace();
  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值