问题描述
- Java实现在图片中嵌入字符串
-
我想在图片中嵌入字符串,以下程序实现了在图片中嵌入图片,请问要怎样改???import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;public class Watermark extends JFrame implements ActionListener
{Image iImage, oImage;
BufferedImage bImage;int iw, ih; int[] pixels,//像素 cphpix; //加密图像数组 boolean loadflag = false, runflag = false; //图像处理执行标志 String imn, //图像文件名 imh, //加密图像标识"c_" wtr, title; //图像标题 ImageWatermark watermark; RAW reader; Common common; public Watermark() { setTitle("LSB图像水印"); this.setBackground(Color.lightGray); //菜单界面 setMenu(); watermark = new ImageWatermark(); reader = new RAW(); common = new Common(); //关闭窗口 closeWin(); setSize(530, 330); setVisible(true); } public void actionPerformed(ActionEvent evt) { Graphics graph = getGraphics(); MediaTracker tracker = new MediaTracker(this); if (evt.getSource() == openItem) { //打开要加水印的图片 JFileChooser chooser = new JFileChooser(); common.chooseFile(chooser, "./images/ch15", 0); int r = chooser.showOpenDialog(null); if(r == JFileChooser.APPROVE_OPTION) { if(runflag) { loadflag = false; runflag = false; } if(!loadflag) { String name = chooser.getSelectedFile().getAbsolutePath(); //取文件名 String filename = chooser.getSelectedFile().getName(); int len = filename.length(); //取扩展名 String exname = filename.substring(len-3, len); //取文件名imn imn = filename.substring(0, len-4); //取加密标识imh imh = filename.substring(0, 2); if(exname.equalsIgnoreCase("raw")) { pixels = reader.readRAW1D(name, iw, ih); ImageProducer ip = new MemoryImageSource(iw, ih, pixels, 0, iw); iImage = createImage(ip); } else { //装载图像 iImage = common.openImage(name, tracker); //取载入图像的宽和高 iw = iImage.getWidth(null); ih = iImage.getHeight(null); repaint(); } if(!imh.equals("j_")) title = "原图"; else title = "受攻击"; loadflag = true; } } }
//LSB图像水印嵌入
else if(evt.getSource() == wlsbimageItem)
{
if(loadflag)
{
setTitle(" LSB图像水印 ");//选择水印路径
JOptionPane.showMessageDialog(null, "请选择要嵌入的水印!");
JFileChooser iconfileChooser = new JFileChooser("F:");
int returnVal2 = iconfileChooser.showOpenDialog(iconfileChooser);//打开"打开文件"对话框
String waterpath = iconfileChooser.getSelectedFile().getAbsolutePath();//你选择的文件夹的路径pixels = common.grabber(common.openImage( waterpath, tracker), iw, ih);
//读入水印图像 int[] water = watermark.readWater(pixels, iw, ih);
pixels = common.grabber(iImage, iw, ih);
System.out.println("水印"+pixels);//嵌入水印
pixels = watermark.chaosEmbed(pixels, water, iw, ih);
showPix(graph, pixels, iw, ih, "原图", "含水印图");runflag = true;
//写入.raw含水印文件
reader.writeRAW("./images/ch15/water/w_" + imn +
".raw", pixels, iw, ih);JOptionPane.showMessageDialog(null, "在目录images/ch15/water, " + "保存含水印图像w_" + imn+".raw成功!"); } else JOptionPane.showMessageDialog(null,"请先打开一幅图!");
}
//LSB图像水印提取
else if(evt.getSource() == dlsbimageItem)
{//选择要提取水印图片的路径
JOptionPane.showMessageDialog(null, "请选择要提取水印的图片!");
JFileChooser iconfileChooser = new JFileChooser("./images/ch15/water");
int returnVal2 = iconfileChooser.showOpenDialog(iconfileChooser);//打开"打开文件"对话框
String picwaterpath = iconfileChooser.getSelectedFile().getAbsolutePath();//你选择的文件夹的路径
pixels = reader.readRAW1D(picwaterpath, iw, ih);
ImageProducer ip = new MemoryImageSource(iw, ih, pixels, 0, iw);
oImage = createImage(ip);pixels = common.grabber(oImage, iw, ih);
//提取水印图像 pixels = watermark.getWater(pixels, iw, ih); showPix(graph, pixels, iw, ih, "原图", "取出水印"); runflag = true;
}
else if (evt.getSource() == exitItem) System.exit(0); } public void paint(Graphics g) { if (iImage != null) { g.clearRect(0, 0, 530, 350); g.drawImage(iImage, 5, 50, null); g.drawString(title, 120, 320); } } public Image showPix(Graphics graph, int[] pixels, int w, int h, String istr, String ostr) {
//将数组中的象素产生一个图像
ImageProducer ip = new MemoryImageSource(w, h, pixels, 0, w);
Image oImage = createImage(ip);
common.draw(graph, iImage, istr, oImage, ostr);
runflag = true;
return oImage;
}public static void main(String[] args) { new Watermark(); } private void closeWin() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } //菜单界面 public void setMenu() { Menu fileMenu = new Menu("文件"); openItem = new MenuItem("打开"); openItem.addActionListener(this); fileMenu.add(openItem); exitItem = new MenuItem("退出"); exitItem.addActionListener(this); fileMenu.add(exitItem); Menu spwaterMenu = new Menu("空域水印"); wlsbimageItem = new MenuItem("LSB图像水印嵌入"); wlsbimageItem.addActionListener(this); spwaterMenu.add(wlsbimageItem); dlsbimageItem = new MenuItem("LSB图像水印提取"); dlsbimageItem.addActionListener(this); spwaterMenu.add(dlsbimageItem); MenuBar menuBar = new MenuBar(); menuBar.add(fileMenu); menuBar.add(spwaterMenu); setMenuBar(menuBar); } MenuItem openItem; MenuItem exitItem; MenuItem wlsbimageItem; //lsb嵌入图像水印 MenuItem dlsbimageItem; //lsb检测图像水印
}
import java.awt.Image;
import java.io.*;public class ImageWatermark {
Common common = new Common();
// LSB嵌入水印
public int[] chaosEmbed(int[] pix, int[] water, int iw, int ih) {
for (int i = 0; i < iw * ih; i++) {
int emb;// 含水印灰度
int gray;// 灰度值// 取(i,j)处的灰度
gray = pix[i] & 0xFF;if (gray % 2 == 1)
gray = gray - 1;// 最低位清零emb = gray + water[i];// 最低位嵌入水印
pix[i] = 255 << 24 | emb << 16 << 8 | emb;
}
return pix;
}// 读入水印图,并转为二值图像矩阵
public int[] readWater(int[] pix, int iw, int ih) {
int gray;// 灰度值for (int i = 0; i < iw * ih; i++) {
gray = pix[i] & 0xFF;// 取(i,j)处的灰度
if (gray > 128)
pix[i] = 0;// while
else
pix[i] = 1;// black
}
return pix;
}//提取水印图像
public int[] getWater(int[] pix, int iw, int ih) {
int g;
for (int i = 0; i < iw * ih; i++) {
g = pix[i] & 0xFF;
if (g % 2 == 0)
g = 255;
else
g = 0;
pix[i] = 255 << 24 | g << 16 | g << 8 | g;
}
return pix;
}// 读入32*32水印图,并转为二值图像矩阵
public int[] readImWater(int[] pix, int iw, int ih) {
int gray;// 灰度值
for (int i = 0; i < iw * ih; i++) {
gray = pix[i] & 0xFF;// 取[i,j]处的灰度
if (gray > 128)
pix[i] = 255;// white
else
pix[i] = 0;// black
}
return pix;
}}
解决方案
解决方案二:
登录验证码生成就是采用的这种方式图片中嵌字符串的。
解决方案三:
参考:http://blog.csdn.net/caoyuan10036/article/details/7278735