是啊.就是在图片的二进制中的加入数据,但是怎么加呢..Java.io的stream提供了什么方法没有啊?
解决方案 »
- 求帮助!!
- OPENNMS 二次开发问题
- No WebApplicationContext found: no ContextLoaderListener registered?
- 明天去谈合同 大家贴下薪水 我在武汉 准备转正要3000
- hibernate新手问个简单问题。。。急。。。在线等
- 为什么Struts的页面用Dreamwear打开时,只显示代码试图,而不显示设计试图?
- 讨论:Java后台与前台的通讯,用什么去实现好哪?
- 菜鸟级问题,大家帮忙
- 如何用I/O流实现任意格式文件的本地复制??
- web项目中,编译好的class文件在哪存放?Eclipse4.4
- java.io.StreamCorruptedException: invalid stream header报错
- 登陆时报错....郁闷
下面是程序:
import java.io.*;
import java.util.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;public class ImageEnsure {
public ImageEnsure() {
}
private char mapTable[]={
'a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','q','r',
's','t','u','v','w','x',
'y','z','0','1','2','3',
'4','5','6','7','8','9'}; public String getEnsure(int width, int height, OutputStream os) {
if(width<=0)width=60;
if(height<=0)height=20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB); // 获取图形上下文
Graphics g = image.getGraphics(); // 设定背景色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height); //画边框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1); // 取随机产生的认证码
String strEnsure = ""; // 4代表4位验证码
for(int i=0; i<4; ++i) {
strEnsure += mapTable[(int)(mapTable.length*Math.random())];
}
// 将认证码显示到图象中
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str = strEnsure.substring(0,1);
g.drawString(str,8,17);
str = strEnsure.substring(1,2);
g.drawString(str,20,15);
str = strEnsure.substring(2,3);
g.drawString(str,35,18);
str = strEnsure.substring(3,4);
g.drawString(str,45,15);
// 随机产生100个干扰点
Random rand = new Random();
for (int i=0;i<100;i++)
{
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x,y,1,1);
} // 释放图形上下文
g.dispose();
try {
// 输出图象到页面
ImageIO.write(image, "JPEG", os);
} catch (IOException e) {
return "";
}
return strEnsure;
}
public static void main(String []args) {
try{
BufferedOutputStream os = new BufferedOutputStream(
new FileOutputStream("e:\\test.jpg"));
new ImageEnsure().getEnsure(100, 200, os);
os.flush();
os.close();
} catch(Exception e) {}
}}
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
import java.text.AttributedString;
import java.awt.font.TextAttribute;
import java.text.AttributedCharacterIterator;
public class WaterMark {
/**
* 给图片添加水印
* @param filePath 需要添加水印的图片的路径
* @param Content 水印的文字
* @param ContentColor 水印文字的颜色
* @param qualNum 图片质量
* @param fontType 字体
* @param fontsize 字体大小
* @return
* @author zhongweihai [email protected]
*/
public boolean createMark(String filePath,String Content,Color ContentColor,float qualNum,
String fontType,int fontSize)
{
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(ContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null );
AttributedString ats = new AttributedString(Content);
Font f = new Font(fontType,Font.BOLD, fontSize); ats.addAttribute(TextAttribute.FONT, f, 0,Content.length() );
AttributedCharacterIterator iter = ats.getIterator(); g.drawString(iter,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;
} public static void main(String[] args)
{
WaterMark wm = new WaterMark();
wm.createMark("c:\\abc.jpg","图片上加的文字水印",Color.red,70f,"黑体",23);
}
}