Home >Backend Development >PHP Tutorial >java operating system clipboard content data

java operating system clipboard content data

WBOY
WBOYOriginal
2016-07-25 09:08:14949browse
Customers have various unique needs and now require that the content copied during the use of the platform be saved and displayed for users' reference.
  1. package cn.net.ssd.common.format;
  2. import java.awt.Image;
  3. import java.awt.Toolkit;
  4. import java.awt.datatransfer.Clipboard;
  5. import java.awt.datatransfer. DataFlavor;
  6. import java.awt.datatransfer.StringSelection;
  7. import java.awt.datatransfer.Transferable;
  8. import java.awt.datatransfer.UnsupportedFlavorException;
  9. import java.io.IOException;
  10. public class ClipboradOperate {
  11. /** @Description:
  12. * @author zhk
  13. * @createtime 2012-7-12 11:33:06 AM
  14. * @param args
  15. */
  16. public static void main(String[] args) {
  17. Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();//Get the system clipboard
  18. try {
  19. ImageViewer im=new ImageViewer(getImageFromClipboard( ));
  20. }catch (Exception e) {
  21. // TODO Auto-generated catch block
  22. e.printStackTrace();
  23. }
  24. }
  25. /**
  26. * Get the text content from the specified clipboard
  27. * The local clipboard uses Clipborad cp = new Clipboard("clip1"); to construct
  28. * The system clipboard uses Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard( );
  29. * Clipboard content getContents(null); Return Transferable
  30. */
  31. protected static String getClipboardText() throws Exception{
  32. Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); // Get the system clipboard
  33. // Get the contents of the clipboard
  34. Transferable clipT = clip.getContents(null);
  35. if (clipT != null) {
  36. // Check whether the content is text type
  37. if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
  38. return (String)clipT.getTransferData(DataFlavor.stringFlavor);
  39. }
  40. return null;
  41. }
  42. //Go to cut Board write text data
  43. protected static void setClipboardText(Clipboard clip, String writeMe) {
  44. Transferable tText = new StringSelection(writeMe);
  45. clip.setContents(tText, null);
  46. }
  47. // Read images from the clipboard
  48. public static Image getImageFromClipboard() throws Exception{
  49. Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
  50. Transferable cc = sysc.getContents(null);
  51. if (cc == null)
  52. return null;
  53. else if (cc.isDataFlavorSupported(DataFlavor.imageFlavor))
  54. return (Image)cc.getTransferData(DataFlavor.imageFlavor);
  55. return null;
  56. }
  57. // Write image to clipboard
  58. protected static void setClipboardImage2(final Image image) {
  59. Transferable trans = new Transferable(){
  60. public DataFlavor[] getTransferDataFlavors() {
  61. return new DataFlavor[] { DataFlavor.imageFlavor };
  62. }
  63. public boolean isDataFlavorSupported(DataFlavor flavor) {
  64. return DataFlavor.imageFlavor.equals (flavor);
  65. }
  66. public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
  67. if(isDataFlavorSupported(flavor))
  68. return image;
  69. throw new UnsupportedFlavorException(flavor);
  70. }
  71. };
  72. Toolkit.getDefaultTool kit( ).getSystemClipboard().setContents(trans, null);
  73. }
  74. }
Copy code
  1. package cn.net.ssd.common.format;
  2. /*
  3. * 查看图形的窗口
  4. */
  5. import java.awt.Frame;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.awt.Toolkit;
  10. import java.awt.event.WindowAdapter;
  11. import java.awt.event.WindowEvent;
  12. /**
  13. * Window used to display graphics
  14. */
  15. public class ImageViewer extends Frame {
  16. private Image image;
  17. /**
  18. * Show an image
  19. * @param viewMe
  20. */
  21. public ImageViewer(Image viewMe) {
  22. image = viewMe;
  23. MediaTracker mediaTracker = new MediaTracker(this);
  24. mediaTracker.addImage(image, 0);
  25. try {
  26. mediaTracker.waitForID(0);
  27. } catch (InterruptedException ie) {
  28. ie.printStackTrace();
  29. System.exit(1);
  30. }
  31. addWindowListener(new WindowAdapter() {
  32. public void windowClosing(WindowEvent e) {
  33. System.exit(0);
  34. }
  35. });
  36. //窗口适应图像大小
  37. setSize(image.getWidth(null), image.getHeight(null));
  38. //窗口标题
  39. setTitle("Viewing Image from Clipboard");
  40. setVisible(true);
  41. }
  42. public void paint(Graphics graphics) {
  43. graphics.drawImage(image, 0, 0, null);
  44. }
  45. /**
  46. * Used to read image files and generate Image objects
  47. */
  48. public static Image getImageFromFile(String fileName) {
  49. Toolkit toolkit = Toolkit.getDefaultToolkit();
  50. Image image = toolkit.getImage(fileName);
  51. return image;
  52. }
  53. }
复制代码
java operating system clipboard content data


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:ewerwerNext article:ewerwer