⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagefilechooser.java

📁 Java版本的屏幕截图 工具,可以自己放到系统托盘使用
💻 JAVA
字号:
/*
 * Copyright (c) 2002-2008 TeamDev Ltd. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * The complete licence text can be found at
 * http://www.teamdev.com/winpack/license.jsf
 */
package teamdev.jxcapture.samples.demo;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.ResourceBundle;
import java.net.URL;

/**
 * @author Ikryanov Vladimir
 */
public class ImageFileChooser {

    private Component owner;

    public ImageFileChooser() {
        this(null);
    }

    public ImageFileChooser(Component owner) {
        this.owner = owner;
    }

    /**
     * Shows the file open dialog.
     *
     * @return the selected file or <code>null</code> if cancel actions has occured
     */
    public File openImageFile() {
        return showDialog(false, null);
    }

    /**
     * Shows the file save dialog.
     *
     * @return the selected file or <code>null</code> if cancel actions has occured
     */
    public File saveImageFile() {
        return saveImageFile(null);
    }

    /**
     * Shows the file save dialog.
     *
     * @return the selected file or <code>null</code> if cancel actions has occured
     */
    public File saveImageFile(String fileName) {
        File imageFile = showDialog(true, fileName);
        if (imageFile != null && imageFile.exists()) {
            ResourceBundle resource = ApplicationSettings.getResourceBundle();
            String title = resource.getString("FileChooser.Confirm.FileExistsDialog.Title");
            String message = resource.getString("FileChooser.Confirm.FileExistsDialog.Message");
            message = message.replaceAll("%s", imageFile.getName());
            int option = JOptionPane.showConfirmDialog(owner, message, title, JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.NO_OPTION) {
                return saveImageFile(fileName);
            }
        }
        return imageFile;
    }

    private File showDialog(boolean save, String defaultFileName) {
        JFileChooser fileChooser = new JFileChooser();
        // removes the default file filter
        fileChooser.setAcceptAllFileFilterUsed(false);
        // sets the file filters to the file chooser dialog
        FileFilter[] filters = getFilters();
        for (int i = 0; i < filters.length; i++) {
            FileFilter filter = filters[i];
            fileChooser.addChoosableFileFilter(filter);
        }
        if (!save) {
            fileChooser.addChoosableFileFilter(new AllImageFilesFilter());
        }
        // sets the file chooser preview
        fileChooser.setAccessory(new ImagePreview(fileChooser));
        // shows the file chooser dialog
        fileChooser.setDialogTitle(save ? "Save As" : "Open");
        if (save) {
            String name = defaultFileName == null ? getFileName() : defaultFileName;
            fileChooser.setSelectedFile(new File(name));
        }
        JFrame window = new JFrame();
        URL imageURL = getClass().getResource("resources/images/JxCapture.png");
        window.setIconImage(new ImageIcon(imageURL).getImage());
        Component parent = owner != null ? owner : window;
        int state = save ? fileChooser.showSaveDialog(parent) : fileChooser.showOpenDialog(parent);
        if (state == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            if (selectedFile != null) {
                // checks the extension of the selected file
                String fileName = selectedFile.getName();
                int index = fileName.lastIndexOf(".");
                if (index == -1) {
                    FileFilter fileFilter = fileChooser.getFileFilter();
                    ImageFileFilter filter = (ImageFileFilter) fileFilter;
                    String extension = filter.getExtension();
                    String absolutePath = selectedFile.getAbsolutePath();

                    return new File(absolutePath + "." + extension);
                }
                return selectedFile;
            }
        }
        return null;
    }

    private String getFileName() {
        ApplicationSettings settings = ApplicationSettings.getInstance();
        String fileName = settings.getTemplateFileName();
        int index = settings.getTemplateNumber();
        return fileName.replaceAll("#", String.valueOf(index));
    }

    /**
     * Gets all supported file filters.
     *
     * @return the array of <code>FileFilter</code> objects
     */
    public FileFilter[] getFilters() {
        ApplicationSettings settings = ApplicationSettings.getInstance();
        String[] descriptions = settings.getFormatDescriptions();
        String[] formats = settings.getImageFormats();
        ImageFileFilter[] filters = new ImageFileFilter[formats.length];
        for (int i = 0; i < formats.length; i++) {
            String[] extensions = settings.getFormatExtensions(formats[i]);
            if (extensions.length > 0) {
                filters[i] = new ImageFileFilter(extensions, descriptions[i]);
            }
        }
        return filters;
    }

    private class ImageFileFilter extends FileFilter {
        private String[] extensions;
        private String description;

        public ImageFileFilter(String[] extensions, String description) {
            this.extensions = extensions;
            this.description = description;
        }

        public boolean accept(File file) {
            if (file.isFile()) {
                String name = file.getName();
                for (int i = 0; i < extensions.length; i++) {
                    if (name.endsWith("." + extensions[i])) return true;
                }
                return false;
            }
            return file.isDirectory();
        }

        public String getDescription() {
            return description;
        }

        public String getExtension() {
            return extensions[0];
        }

        public String toString() {
            return description;
        }
    }

    private class AllImageFilesFilter extends FileFilter {

        public boolean accept(File file) {
            if (file.isDirectory()) return true;
            String[] formats = ApplicationSettings.getInstance().getImageFormats();
            for (int i = 0; i < formats.length; i++) {
                String[] extensions = ApplicationSettings.getInstance().getFormatExtensions(formats[i]);
                if (extensions.length > 0) {
                    for (int j = 0; j < extensions.length; j++) {
                        String extension = extensions[j];
                        if (file.getName().endsWith("." + extension)) return true;
                    }
                }
            }
            return false;
        }

        public String getDescription() {
            return "All Formats";
        }
    }

    public class ImagePreview extends JComponent implements PropertyChangeListener {
        ImageIcon thumbnail = null;
        File file = null;

        public ImagePreview(JFileChooser fc) {
            setPreferredSize(new Dimension(100, 50));
            fc.addPropertyChangeListener(this);
        }

        public void loadImage() {
            if (file == null) {
                thumbnail = null;
                return;
            }

            ImageIcon tmpIcon = new ImageIcon(file.getPath());
            if (tmpIcon.getIconWidth() > 90) {
                thumbnail = new ImageIcon(tmpIcon.getImage(). getScaledInstance(90, -1, Image.SCALE_DEFAULT));
            } else {
                thumbnail = tmpIcon;
            }
        }

        public void propertyChange(PropertyChangeEvent e) {
            boolean update = false;
            String prop = e.getPropertyName();

            //If the directory changed, don't show an image.
            if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
                file = null;
                update = true;

                //If a file became selected, find out which one.
            } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
                file = (File) e.getNewValue();
                update = true;
            }

            //Update the preview accordingly.
            if (update) {
                thumbnail = null;
                if (isShowing()) {
                    loadImage();
                    repaint();
                }
            }
        }

        protected void paintComponent(Graphics g) {
            if (thumbnail == null) {
                loadImage();
            }
            if (thumbnail != null) {
                int x = getWidth() / 2 - thumbnail.getIconWidth() / 2;
                int y = getHeight() / 2 - thumbnail.getIconHeight() / 2;

                if (y < 0) y = 0;
                if (x < 5) x = 5;

                thumbnail.paintIcon(this, g, x, y);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -