• ImageIcon图像处理相关测试【一些特殊的处理方式】


    /*************以下源码通过测试******************************/

    package cn.jason.ios.images;

    import java.awt.FileDialog;
    import java.awt.Image;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.net.URL;

    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

    /**
    *
    * ImageIcon图像处理相关测试[一些特殊的处理方式]
    *
    */
    public class MyImageIcon extends JFrame{

    /**
    * 打开本地管理器对话框,选择图片文件
    * @return 返回图片绝对路径
    */
    private static String getLocalImageAbsolutePath(){
    JFrame frame=new JFrame();
    //创建对话框
    FileDialog dialog=new FileDialog(frame);
    //设置尺寸
    dialog.setBounds(300, 100, 500, 450);
    //打开对话框
    dialog.setVisible(true);
    //获取返回的绝对路径
    StringBuilder absolutePath=new StringBuilder();
    absolutePath.append(dialog.getDirectory()=="null"?"null":dialog.getDirectory());
    absolutePath.append(dialog.getFile()=="null"?"null":dialog.getFile());
    //过滤信息
    if(absolutePath.toString().contains("null")){
    return "NO-Path";
    }
    return absolutePath.toString();
    }
    /**
    * 终止程序
    */
    private static void exitPrograming(){
    System.exit(0);
    }
    /**
    * 获取图片二进制数据 :[1024*1024*10]:10M
    * @param imagePath 图片绝对地址
    * @return 返回二进制数据
    */
    private static byte[] getImageBytes(String imagePath){
    //二进制对象
    byte[] b=new byte[1024*1024*10];
    //创建图片文件
    File imageFile=new File(imagePath);
    //图片流对象
    try {
    FileInputStream fis=new FileInputStream(imageFile);
    //写入
    fis.read(b);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return b!=null?b:null;
    }
    /**
    * 通过图片二进制数据 获取ImageIcon对象
    * @param b 二进制数据
    * @return ImageIcon对象
    */
    private static ImageIcon getImageIcon(byte[] b){
    return new ImageIcon(b);
    }
    /**
    * 通过图片地址 获取ImageIcon对象
    * @param filename 图片地址
    * @return ImageIcon对象
    */
    private static ImageIcon getImageIcon(String filename){
    return new ImageIcon(filename);
    }
    /**
    * 通过图片地址对象 获取ImageIcon对象
    * @param location URL地址对象
    * @return ImageIcon对象
    */
    private static ImageIcon getImageIcon(URL location){
    return new ImageIcon(location);
    }
    /**
    * 通过图片对象 获取ImageIcon对象
    * @param image Image地址对象
    * @return ImageIcon对象
    */
    private static ImageIcon getImageIcon(Image image){
    return new ImageIcon(image);
    }
    /**
    * 调整ImageIcon的尺寸
    * @param icon Icon对象
    * @param width 调整后的宽度
    * @param height 调整后的高度
    * @return
    */
    private static ImageIcon imageSizeCorrect(ImageIcon icon,
    int width,
    int height){
    Image images=icon.getImage();
    images=images.getScaledInstance(width, height, Image.SCALE_DEFAULT );
    return new ImageIcon(images);
    }


    /**
    * 程序入口
    * @param params
    */
    public static void main(String[] params) {

    String path=getLocalImageAbsolutePath();
    System.out.println("打开地址:"+path);
    MyImageIcon testImages=new MyImageIcon();
    testImages.setBounds(200,100, 500, 500);
    JLabel jLabel=new JLabel();
    jLabel.setBounds(100,100, 300, 300);
    jLabel.setIcon(imageSizeCorrect(MyImageIcon.getImageIcon(MyImageIcon.getImageBytes(path)),300,300));
    testImages.add(jLabel);
    testImages.setVisible(true);

    //结束程序
    MyImageIcon.exitPrograming();
    }
    }

    研究技术需要静下心来,一点一点地深究.......
  • 相关阅读:
    操作系统8:文件系统
    操作系统7:内存管理
    操作系统6:死锁
    操作系统5:进程同步
    操作系统3:CPU调度
    操作系统2:进程
    操作系统1:操作系统结构
    计算机组成:CPU
    计算机组成:数制与运算
    计算机组成:输入输出系统
  • 原文地址:https://www.cnblogs.com/newwind/p/5650079.html
Copyright © 2020-2023  润新知