java swing从剪切板黏贴图片粘贴图片

本文介绍如何使用Java Swing从系统剪贴板中获取并粘贴图片至应用程序的Label控件上。通过详细代码示例展示了处理不同类型的图片数据,并将其转换为BufferedImage格式的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java swing从剪切板黏贴图片



 

 

代码:

public static BufferedImage getClipboardImage(Frame frame) {
		// java.lang.ClassCastException: sun.awt.datatransfer.TransferableProxy cannot be cast to sun.awt.datatransfer.ClipboardTransferable
		Transferable trans=frame.getToolkit().getSystemClipboard().getContents(null);
		BufferedImage image=null;
		
		try {
			if (null != trans && trans.isDataFlavorSupported(DataFlavor.imageFlavor)) {   
			Object obj22=trans.getTransferData(DataFlavor.imageFlavor);
			if(!ValueWidget.isNullOrEmpty(obj22)){
				if(obj22 instanceof BufferedImage){
                    image = (BufferedImage) obj22;
                } else if (obj22 instanceof sun.awt.image.MultiResolutionCachedImage) {//兼容mac os
                    sun.awt.image.MultiResolutionCachedImage cachedImage = (sun.awt.image.MultiResolutionCachedImage) obj22;
                    if (null == cachedImage) {
                        return null;
                    }
                    sun.awt.image.ToolkitImage toolkitImage = (sun.awt.image.ToolkitImage) cachedImage.getScaledInstance(cachedImage.getWidth(null), cachedImage.getHeight(null), Image.SCALE_SMOOTH);
                    if (null == toolkitImage) {
                        return null;
                    }
                    java.awt.image.FilteredImageSource filteredImageSource = (java.awt.image.FilteredImageSource) ReflectHWUtils.getObjectValue(toolkitImage, "source");
                    if (null == filteredImageSource) {
                        return null;
                    }
                    sun.awt.image.OffScreenImageSource imageSource = (sun.awt.image.OffScreenImageSource) ReflectHWUtils.getObjectValue(filteredImageSource, "src");
                    image = (BufferedImage) ReflectHWUtils.getObjectValue(imageSource, "image");
//					System.out.println(imageSource);
                }
			}
			}
		} catch (UnsupportedFlavorException e1) {
			e1.printStackTrace();
			GUIUtil23.errorDialog(e1);
		} catch (IOException e1) {
			e1.printStackTrace();
			GUIUtil23.errorDialog(e1);
		}
//		}
        catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

		
		
		return image;
	}

 调用:

/***
	 * 把系统剪切板中的图片黏贴到swing的Label控件中
	 */
	public void pasteClipboardImageAction(){
		BufferedImage bufferedimage=ComponentUtil.getClipboardImage(this.frame);

		if(ValueWidget.isNullOrEmpty(bufferedimage)){
			GUIUtil23.alert("系统剪切板中无图片,请先复制图片");
			return;
		}
		image=bufferedimage;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {//把粘贴过来的图片转为为二进制(字节数组)
			ImageIO.write(bufferedimage, read_qrcode_from_pic_format/*jpg*/, baos);
			QRbytes= baos.toByteArray();
			ComponentUtil.appendResult(resultArea, "粘贴的二维码大小:\t"+QRbytes.length, true);
		} catch (IOException e) {
			e.printStackTrace();
		}

		qrResultLabel.setIcon(new ImageIcon(image));
	}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值