• 【Java小项目】图片浏览器


     

        主要功能:

             1.打开各个基本格式的图片(有文件过滤)

             2.图片的缩放(滚轮滚动)

             3.图片相关信息的显示

             4.打开目录的图片自动加载,可上一张下一张切换(单击左部上一张,右部下一张)

             5.缩放后的图片另存为

        遇到的问题:

    1.图片另存为的时候
    //错误,新建的图片是张黑图
    Image img=imageicon.getImage().getScaledInstance(w,h,type);
    BufferedImage bi=new BufferImage(w,h,typr);
    bi.getGraphics().drawImage(img, 0, 0, null);
    ImageIO.write(bi,"type",file);
    
    //正确
    ImageIcon ii=new ImageIcon(imageicon.getImage().getScaledInstance(w,h,type));
    BufferedImage bi=new BufferImage(w,h,typr);
    bi.getGraphics().drawImage(ii.getImage(), 0, 0, null);
    ImageIO.write(bi,"type",file);
    
    2.String.split()若要用"."来分割需要用两个\
    即
    String[] n=s.split("\.");
    

         主要思路:

                当选定某个图片时启动一个线程来获取该目录下的所有图片图片文件

    package com.try01;
    
    import java.io.File;
    
    /**
     * Created by ztc on 15-11-14.
     */
    public class getFileList extends Thread {
        File CurrentDirectory;
        public getFileList(File f){
            this.CurrentDirectory=f;
        }
    
        public void run(){
            File[] cd=CurrentDirectory.listFiles(tools.myFilenameFilter());
            for(int i=0;i<cd.length;i++){
               // System.out.println(cd[i]);
                tools.addFile(cd[i]);
            }
        }
    }
    
    源码 gitosc地址http://git.oschina.net/A_yes/PicViewer

    package com.try01;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.filechooser.FileFilter;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.nio.Buffer;
    
    /**
     * Created by ztc on 15-10-31.
     */
    public class MyFrame extends JFrame implements ActionListener,MouseListener,MouseWheelListener{
        JMenuBar jmb;
        JMenu jm1,jm2,jm3;
        JMenuItem[] jmi=new JMenuItem[7];
        JLabel jl,jl1;
        JPanel jp;
        JScrollPane jsp;
        ImageIcon imgIco,showii;
        File f;
        JFileChooser jfc;
        float width;
        float height;
        void init(){
            //创建对象
            //文件选择器
            jfc=new JFileChooser("./");
            //菜单栏
            jmb=new JMenuBar();
            jm1=new JMenu("文件");
            jm2=new JMenu("编辑");
            jm3=new JMenu("关于");
            jmi[0]=new JMenuItem("打开");
            jmi[0].addActionListener(this);
            jmi[1]=new JMenuItem("另存为");
            jmi[1].addActionListener(this);
            jmi[5]=new JMenuItem("上一张");
            jmi[5].addActionListener(this);
            jmi[6]=new JMenuItem("下一张");
            jmi[6].addActionListener(this);
            jmi[2]=new JMenuItem("放大");
            jmi[2].addActionListener(this);
            jmi[3]=new JMenuItem("缩小");
            jmi[3].addActionListener(this);
            jmi[4]=new JMenuItem("原图");
            jmi[4].addActionListener(this);
            jm1.add(jmi[0]);
            jm1.add(jmi[1]);
            jm1.add(jmi[5]);
            jm1.add(jmi[6]);
            jm2.add(jmi[2]);
            jm2.add(jmi[3]);
            jm2.add(jmi[4]);
            jmb.add(jm1);
            jmb.add(jm2);
            jmb.add(jm3);
    
            //显示区域
            jl=new JLabel("请选择图片。。。",JLabel.CENTER);
            jl.setForeground(Color.gray);
            jl.addMouseWheelListener(this);
            jl.addMouseListener(this);
            jsp=new JScrollPane(jl);
    
            //底部信息
            jl1=new JLabel("图片信息");
            //设置窗口
    
    
            this.setJMenuBar(jmb);
            this.add(jsp);
            this.add(jl1,"South");
    
            this.setSize(700,500);
            this.setLocation(300,200);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        public void ShowImg(){
            if(imgIco.getIconWidth()>695||imgIco.getIconHeight()>465){
                width=695;
                height=675*(float)imgIco.getIconHeight()/imgIco.getIconWidth();
                //System.out.println((float)ii.getIconHeight()/ii.getIconWidth());
            }else {
                width=imgIco.getIconWidth();
                height=imgIco.getIconHeight();
            }
            showii= new ImageIcon(
                    imgIco.getImage().getScaledInstance((int)width,(int)height,0));
            this.jl.setText("");
            this.jl.setIcon(showii);
            ShowMsg();
        }
        public void Zoom(int flag){
            if((width<5000&&height<4000)&&(width>5&&height>4)) {
                if (flag == 0) {
                    //放大
                    width *= 1.3;height *= 1.3;
                } else if (flag == 1) {
                    //缩小
                    width *= 0.7;height *= 0.7;
                } else if (flag == 2) {
                    //原图
                    width = imgIco.getIconWidth();height = imgIco.getIconHeight();
                }
                showii = new ImageIcon(imgIco.getImage().getScaledInstance((int) width, (int) height, 0));
                this.jl.setIcon(showii);
                ShowMsg();
            }
            else if(width>5000||height>4000){
                width*=0.8;
                height*=0.8;
                JOptionPane.showMessageDialog(this, "再放大会失真!");
            }else if(width<5||height<3){
                width*=1.2;
                height*=1.2;
                JOptionPane.showMessageDialog(this, "再缩小就看不见了!");
            }
        }
        public void Save(){
            Image img=showii.getImage();
            try {
                JOptionPane.showMessageDialog(this,"以不同拓展名命名
    就以不同格式保存!(默认为gif)");
                //System.out.println(width+"  "+height+":"+img.getWidth(this)+"  "+img.getHeight(this));
                //获取类型
                BufferedImage choosed=ImageIO.read(f);
                BufferedImage bi=new BufferedImage((int)width,(int)height,choosed.getType());
                bi.getGraphics().drawImage(img, 0, 0, null);
                int result=jfc.showSaveDialog(this);
                File save=jfc.getSelectedFile();
                if(result==0&&save!=null) {
                    // . 要加转义符
                    String[] n=save.getName().split("\.");
                    //System.out.println(n[0]);
                    ImageIO.write(bi,(n.length==1?"gif":n[1]),save);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void ShowMsg(){
            //设置底部信息
            String[] n=f.getName().split("\.");
            float per=(float)showii.getIconWidth()/imgIco.getIconWidth();
            String msg="原图:"+imgIco.getIconWidth()+"x"+imgIco.getIconHeight()+"像素。"
                    +"显示:"+showii.getIconWidth()+"x"+showii.getIconHeight()+"像素。"
                    +"缩放度:"+(int)(per*100)+"%"
                    +"格式:"+(n.length==1?"未知":n[1]);
            this.jl1.setText(msg);
            this.setTitle(f.toString());
            this.jl1.setForeground(Color.gray);
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==jmi[0]||e.getSource()==jl){
                //设置上次打开位置
                jfc.setCurrentDirectory(jfc.getCurrentDirectory());
                //设置过滤
                jfc.setFileFilter(tools.myFilter());
                int result=jfc.showOpenDialog(this);
                f=jfc.getSelectedFile();
                if(result==0&&f!=null) {
                    tools.addFile(f);
                    new getFileList(jfc.getCurrentDirectory()).start();
                    imgIco = new ImageIcon(f.getPath());
                    ShowImg();
                }
            }else if(f!=null&&e.getSource()==jmi[2]){
                //放大
                Zoom(0);
            }else if(f!=null&&e.getSource()==jmi[3]){
                //缩小
                Zoom(1);
            }else if(f!=null&&e.getSource()==jmi[4]){
                //原图
                Zoom(2);
            }else if(f!=null&&e.getSource()==jmi[5]){
                //上一张
                f=tools.getLast(f);
                imgIco=new ImageIcon(f.getPath());
                ShowImg();
            }else if(f!=null&&e.getSource()==jmi[6]){
                //下一张
                f=tools.getNext(f);
                imgIco=new ImageIcon(f.getPath());
                ShowImg();
            }else if(f!=null&&e.getSource()==jmi[1]){
                //保存
                Save();
            }
        }
    
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            //System.out.println(e.getWheelRotation()+"  "+e.getScrollAmount());
            if(f!=null) {
                if (e.getWheelRotation() == 1) {
                    Zoom(0);
                } else if (e.getWheelRotation() == -1) {
                    Zoom(1);
                }
            }
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
            if(f!=null) {
                if (e.getClickCount() == 1 && e.getX() < 200) {
                    f = tools.getLast(f);
                    imgIco = new ImageIcon(f.getPath());
                    ShowImg();
                } else if (e.getClickCount() == 1 && e.getX() > 500) {
                    f = tools.getNext(f);
                    imgIco = new ImageIcon(f.getPath());
                    ShowImg();
                }
            }
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
    
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
    
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
    
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
    
        }
    }
    
    tools类
    package com.try01;
    
    import javax.swing.filechooser.FileFilter;
    import java.io.File;
    import java.io.FilenameFilter;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by ztc on 15-11-13.
     */
    public class tools {
        public static List<File> fileBuffer=new ArrayList<File>();
        public static void addFile(File f){
            if(!fileBuffer.contains(f))
                fileBuffer.add(f);
        }
        public static File getNext(File f){
            if(fileBuffer.indexOf(f)+1<fileBuffer.size())
                return fileBuffer.get(fileBuffer.indexOf(f)+1);
            else
                return fileBuffer.get(0);
        }
        public static File getLast(File f){
            if(fileBuffer.indexOf(f)-1>=0)
                return fileBuffer.get(fileBuffer.indexOf(f)-1);
            else
                return fileBuffer.get(fileBuffer.size()-1);
        }
        public static FileFilter myFilter(){
            FileFilter ff=new FileFilter() {
                @Override
                public boolean accept(File f) {
                    String name=f.getName();
                    if ( name.endsWith(".jpg")||name.endsWith(".jpeg")
                            || name.endsWith(".gif") || name.endsWith(".png")
                            || name.endsWith(".bmp")||!name.contains("."))
                        return true;
                    return false;
                }
    
                @Override
                public String getDescription() {
                    return "*.jpg|*.jpeg|*.gif|*.bmp";
                }
            };
            return ff;
        }
        public static FilenameFilter myFilenameFilter(){
            FilenameFilter ff=new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    if ( name.endsWith(".jpg")||name.endsWith(".jpeg")
                            || name.endsWith(".gif") || name.endsWith(".png")
                            || name.endsWith(".bmp"))
                        return true;
                    return false;
                }
            };
            return ff;
        }
    }
    


  • 相关阅读:
    mysql查询字段取前3位,后3位,中间3位,去除前3位,去除后3位
    10月份四季度
    JavaScript箭头函数的立即执行函数实现三元表达式执行多条语句
    JavaScript判断是否是同一天
    项目经理:是兄弟就一起加班吧
    技术人员转型项目经理的角色转换
    项目经理入职后,如何快速管理项目
    如何解决项目成员之间的冲突?
    提高各方面沟通效率,是项目经理该去做的事
    项目计划太复杂?试试思维导图
  • 原文地址:https://www.cnblogs.com/A-yes/p/9894223.html
Copyright © 2020-2023  润新知