在Java应用程序中实现copy图像功能:
创新互联建站专注于企业全网营销推广、网站重做改版、赫章网站定制设计、自适应品牌网站建设、H5页面制作、商城网站制作、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为赫章等各大城市提供网站开发制作服务。
用Java开发图形应用程序的朋友一定遇到过如何在程序中实现复制图像的功能。在jdk1.4以前,java本身就支持将程序中文字串复制给其它的非java应用程序使用,而将程序中的图像复制到非java应用程序简直难上加难。只到jdk1.4出来,这个问题才得以解决。要做复制功能,一般是继承TransferHandler类,实现Transferable接口, 这样你的复制内容才能传到系统clipboard,为此我们来写一个ImageSelection类: /** * Copyright: Copyright (c) 2002 * @author Turbo Chen * @version 1.00 */ import java.awt.*; import java.awt.image.*; import java.awt.datatransfer.*; import javax.swing.*; public class ImageSelection extends TransferHandler implements Transferable { private static final DataFlavor flavors[] = {DataFlavor.imageFlavor}; private Image image; public int getSourceActions(JComponent c) { return TransferHandler.COPY; } public boolean canImport(JComponent comp, DataFlavor flavor[]) { for (int i=0, n=flavor.length; i if (flavor[i].equals(flavors[0])) { return true; } } return false; } //将图像复制到Image对象. public Transferable createTransferable(JComponent comp) { // Clear clip image = null; Icon icon = null; Rectangle rect = comp.getBounds(); BufferedImage bufImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); Graphics g = bufImage.getGraphics(); comp.paint(g); if (bufImage != null ) { image = bufImage; return this; } return null; } // Transferable public Object getTransferData(DataFlavor flavor) { if (isDataFlavorSupported(flavor)) { return image; } return null; } public DataFlavor[] getTransferDataFlavors() { return flavors; } public boolean isDataFlavorSupported(DataFlavor flavor) { return flavor.equals(flavors[0]); } } 利用这个类,就可以轻松的将JComponent图像复制到系统clipboard了.怎么使用呢,再来看看下面的代码: final Clipboard clipboard = kit.getSystemClipboard(); Icon icon = new ImageIcon("myphoto.jpg"); final JLabel label = new JLabel(icon); label.setTransferHandler(new ImageSelection()); JButton copy = new JButton("Label Copy"); copy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TransferHandler handler = label.getTransferHandler(); handler.exportToClipboard(label, clipboard, TransferHandler.COPY); } }); 在你的程序中,要有一个JFrame, 加上一个JLabel,一个JButton,将上面的代码加进入,你就可以在你的程序中看到图像如何复制到系统clipboard了.
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
copy("D:\\test.jpg", "D:\\ttt.jpg");
}
public static void copy(String src, String dest) throws IOException {
byte[] buffer = new byte[1024];
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dest);
int c;
while((c = in.read(buffer)) = 0) {
out.write(buffer, 0, c);
}
} finally {
if (in != null) {
try {
in.close();
} catch (Exception err) {
// Ignore the exception.
}
}
if (out != null) {
try {
out.close();
} catch (Exception err) {
//Ignore the exception.
}
}
}
}
}
这是我们公司基类里的一个方法希望对你有帮助。。/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
// System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace(); } }