java swing实现简单图片显示(测试生成图片快捷方式)
生活随笔
收集整理的這篇文章主要介紹了
java swing实现简单图片显示(测试生成图片快捷方式)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原鏈接:https://yq.aliyun.com/articles/58303
package com.ts.x;import java.awt.Image; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.URL; import java.util.Hashtable;import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel;import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix;public class SwingTestImg extends JFrame{private static final long serialVersionUID = 1L;private JLabel label;private Icon icon;private Image image;public static void main(String[] args) {new SwingTestImg();}public SwingTestImg(){try{setTitle("測試圖片簡單顯示");setSize(300,300);setDefaultCloseOperation(DISPOSE_ON_CLOSE);label = new JLabel();add(label);setVisible(true);//A:網路URL圖片 // icon = new ImageIcon(new URL("http://tp1.sinaimg.cn/3223061260/180/5659068018/1"));//B:項目目錄下圖片 // InputStream is = SwingTestImg.class.getResourceAsStream("twodimensioncode.gif"); // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // byte [] buff = new byte[100]; // int readCount = 0; // while((readCount = is.read(buff,0,100)) > 0){ // baos.write(buff,0,readCount); // } // byte [] inbyte = baos.toByteArray(); // icon = new ImageIcon(inbyte); // //C:本地磁盤圖片,圖片太大,會導致空白顯示 // image = new ImageIcon("D:/1.png").getImage();//D:代碼生成的BufferedImage二維碼圖片Hashtable hints = new Hashtable();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");BitMatrix matrix = new MultiFormatWriter().encode("http://www.vcspark.com/", BarcodeFormat.QR_CODE, 300, 300,hints);int width = matrix.getWidth();int height = matrix.getHeight();BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);for(int x = 0;x < width; x++){for(int y = 0;y < height; y++){image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);}}icon = new ImageIcon(image);}catch(Exception e){System.out.println("初始化失敗"+e.getMessage());e.printStackTrace();}label.setIcon(icon); // label.setIcon(new ImageIcon(image));} }總結
以上是生活随笔為你收集整理的java swing实现简单图片显示(测试生成图片快捷方式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 接口测试工具
- 下一篇: [转]微信小程序开发需要注意的29个坑