资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java图片结合代码 java中图片

java图片加水印代码 最好有实例!!!先谢了!!

文字水印

为靖边等地区用户提供了全套网页设计制作服务,及靖边网站建设行业解决方案。主营业务为成都做网站、成都网站制作、靖边网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

import java.awt.*;

import java.awt.image.*;

import java.io.*;

import javax.swing.*;

import com.sun.image.codec.jpeg.*;

public class WaterSet {

/**

* 给图片添加水印

*

* @param filePath

* 需要添加水印的图片的路径

* @param markContent

* 水印的文字

* @param markContentColor

* 水印文字的颜色

* @param qualNum

* 图片质量

* @return

*/

public boolean createMark(String filePath, String markContent,

Color markContentColor, float qualNum) {

ImageIcon imgIcon = new ImageIcon(filePath);

Image theImg = imgIcon.getImage();

int width = theImg.getWidth(null);

int height = theImg.getHeight(null);

BufferedImage bimage = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

Graphics2D g = bimage.createGraphics();

g.setColor(markContentColor);

g.setBackground(Color.white);

g.drawImage(theImg, 0, 0, null);

g.drawString(markContent, width / 5, height / 5); // 添加水印的文字和设置水印文字出现的内容

g.dispose();

try {

FileOutputStream out = new FileOutputStream(filePath);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);

param.setQuality(qualNum, true);

encoder.encode(bimage, param);

out.close();

} catch (Exception e) {

return false;

}

return true;

}

}

图片水印

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

public final class ImageUtils {

public ImageUtils() {

}

/*

* public final static String getPressImgPath() { return ApplicationContext

* .getRealPath("/template/data/util/shuiyin.gif"); }

*/

/**

* 把图片印刷到图片上

*

* @param pressImg --

* 水印文件

* @param targetImg --

* 目标文件

* @param x

* --x坐标

* @param y

* --y坐标

*/

public final static void pressImage(String pressImg, String targetImg,

int x, int y) {

try {

//目标文件

File _file = new File(targetImg);

Image src = ImageIO.read(_file);

int wideth = src.getWidth(null);

int height = src.getHeight(null);

BufferedImage image = new BufferedImage(wideth, height,

BufferedImage.TYPE_INT_RGB);

Graphics g = image.createGraphics();

g.drawImage(src, 0, 0, wideth, height, null);

//水印文件

File _filebiao = new File(pressImg);

Image src_biao = ImageIO.read(_filebiao);

int wideth_biao = src_biao.getWidth(null);

int height_biao = src_biao.getHeight(null);

g.drawImage(src_biao, (wideth - wideth_biao) / 2,

(height - height_biao) / 2, wideth_biao, height_biao, null);

//水印文件结束

g.dispose();

FileOutputStream out = new FileOutputStream(targetImg);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(image);

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 打印文字水印图片

*

* @param pressText

* --文字

* @param targetImg --

* 目标图片

* @param fontName --

* 字体名

* @param fontStyle --

* 字体样式

* @param color --

* 字体颜色

* @param fontSize --

* 字体大小

* @param x --

* 偏移量

* @param y

*/

public static void pressText(String pressText, String targetImg,

String fontName, int fontStyle, int color, int fontSize, int x,

int y) {

try {

File _file = new File(targetImg);

Image src = ImageIO.read(_file);

int wideth = src.getWidth(null);

int height = src.getHeight(null);

BufferedImage image = new BufferedImage(wideth, height,

BufferedImage.TYPE_INT_RGB);

Graphics g = image.createGraphics();

g.drawImage(src, 0, 0, wideth, height, null);

// String s="";

g.setColor(Color.RED);

g.setFont(new Font(fontName, fontStyle, fontSize));

g.drawString(pressText, wideth - fontSize - x, height - fontSize

/ 2 - y);

g.dispose();

FileOutputStream out = new FileOutputStream(targetImg);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(image);

out.close();

} catch (Exception e) {

System.out.println(e);

}

}

public static void main(String[] args) {

pressImage("F:/logo.png", "F:/123.jpg", 0, 0);

}

}

JAVA项目开发中程序、图片怎么组合在一起的

这是以段绘制水印的程序 还有透明效果 水印上去的就是一张图片 你应该看的懂

/**

* @see 绘制水印

* @param base 根目录

* @param file 图片文件

* @param image 图片源

* @param width 图片宽度

* @param height 图片高度

* */

private boolean printWater(String base, File file, BufferedImage image, int width, int height){

String fileName = file.getName();

String extend = fileName.substring(fileName.lastIndexOf(".") + 1);

//如果图片不是gif类型,并且图片小于1M就加水印

if(!"gif".toLowerCase().equals(extend) file.length() = 1024 * 1024){

try {

String waterImgPath = UtilityPropKit.getProp("waterImgPath");

BufferedImage water = ImageIO.read(new File(base + waterImgPath)); //水印

// 水印长度

int waWidth = water.getWidth();

// 水印高度

int waHeight = water.getHeight();

if(height waHeight * 2 width waWidth * 2){

//根据源图片尺寸,产生目标图片

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

//获取Graphics2D

Graphics2D graph = img.createGraphics();

//绘制源图

graph.drawImage(image, 0, 0, width, height, null);

//在已经绘制的图片中加入透明度通道

graph.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));

//绘制水印

graph.drawImage(water, 0, height-waHeight, waWidth, waHeight, null);

//释放对象

graph.dispose();

//保存文件

ImageIO.write(img, extend, file);

}

return true;

} catch (Exception e) {

e.printStackTrace();

}

}

return false;

}

怎么用java代码模拟一张图片

用java代码模拟一张图片可以这样操作:1.创建BufferedImage类

2.根据BufferedImage类得到一个Graphics2D对象

3.根据Graphics2D对象进行逻辑操作

4.处理绘图

5.将绘制好的图片写入到图片

有大神说一下Java这种组合框显示图片具体的代码,谢谢了

效果图如下

swing和awt写法差别不大,我使用的是swing的组件

参考代码和注释如下

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

//本窗口继承自JFrame 实现了 ActionListener接口

public class JCBFrame extends JFrame implements ItemListener{

JComboBoxString jcb;

JLabel jlimg;

// 文字

String[] ss =    {"樱桃","香蕉","西瓜"};

//文字对应图片位置

String[] icons = {"img/1.jpg","img/2.jpg","img/3.jpg"};

int init = 2;//初始选择的下标--西瓜

public JCBFrame() {

jcb = new JComboBoxString(ss);

jcb.setSelectedIndex(init);//初始选择的项目

jcb.addItemListener(this);//设置条目事件响应 ,比如选中某个条目的处理

add(jcb,BorderLayout.NORTH);

jlimg = new JLabel();

jlimg.setHorizontalAlignment(JLabel.CENTER);

add(jlimg);

showImg(init);//初始显示的图片

//窗口属性的设置

setTitle("测试窗口");//标题

setDefaultCloseOperation(EXIT_ON_CLOSE);

setSize(260, 300);//大小

setLocationRelativeTo(null);//居中

}

//显示图片

private void showImg(int index) {

jlimg.setIcon( new ImageIcon(this.getClass().getResource(icons[index])));

}

//main方法

public static void main(String[] args) {

new JCBFrame().setVisible(true);//实例化并可见

}

@Override

public void itemStateChanged(ItemEvent e) {

if(e.getStateChange()==ItemEvent.SELECTED) {//当选择某个条目时

showImg(jcb.getSelectedIndex());//显示的图片改变

}

}

}


分享文章:java图片结合代码 java中图片
地址分享:http://cdkjz.cn/article/ddeseig.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220