如何在幻灯片中处理多分辨率图像?

时间:2015-08-31 08:01:41

标签: java image swing user-interface slideshow

我使用此代码进行幻灯片放映,并且它的工作率为100%,但问题是图像分辨率如果分辨率很高没问题但是如果它很低则图像显示的形状非常糟糕。

,代码是:

public class EasyView_V1 {
     public static Timer tm = null;

     public static File[] f = new File("C:/Ma7moud/my projects/Rami/NetBeans/Web applications/TheChatServerApp_3/web/images/brugges2006/big").listFiles();

     public static void main(String[] args) {
         JPanel panel5 = new JPanel();
         JLabel pic = new JLabel();

         pic.setSize(new Dimension(610, 695));

         //Call The Function SetImageSize
         try {
             SetImageSize(f.length - 1, f, pic);
         } catch (Exception e) {
             System.out.println(e.toString());
             JOptionPane.showMessageDialog(null, "Images directory not found", "Error", JOptionPane.ERROR_MESSAGE);
             JOptionPane.showMessageDialog(null, "Plz check the images directory and try again", "Attention", JOptionPane.INFORMATION_MESSAGE);
             System.exit(1);
         }

         try {
             //set a timer
             tm = new Timer(1000, (ActionEvent e) -> {
             SetImageSize(x, f, pic);
             x += 1;
             try {
                 if (x >= f.length) {
                     x = 0;
                 }
             } catch (Exception ex) {
                 System.out.println(ex.toString());
                 System.exit(1);
             }
         });
       } catch (Exception e) {
            System.out.println(e.toString());
       }

    panel5.add(pic);
    tm.start();
    JFrame f = new JFrame ()
    f.add(panel5);
    f.pack();
    f.setVisible(true);
}

以下是图像绘制的代码:

public static void SetImageSize(int i, File[] f, JLabel pic) {
    try {
        ImageIcon icon = new ImageIcon(f[i].toString());
        Image img = icon.getImage();
        @SuppressWarnings("UnusedAssignment")
        Image newImg = null;

        newImg = img.getScaledInstance(pic.getWidth(), pic.getHeight(), Image.SCALE_SMOOTH);

        ImageIcon newImc = new ImageIcon(newImg);
        pic.setIcon(newImc);
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,它是关于将每个图像的纵横比和图像的位置固定到JPanel中,所以解决方案如下:

我改变设置的图像大小:

public static void SetImageSize(int i, File[] f, JLabel pic) {
        try {
            pic.setSize(new Dimension(610, 695));

            ImageIcon icon = new ImageIcon(f[i].toString());
            Image img = icon.getImage();

            ImageIcon finalIimage = new ImageIcon(ImgResize(img, img.getWidth(pic), img.getHeight(pic), pic));

            xy = ImgPosition(finalIimage.getIconWidth(), finalIimage.getIconHeight(), pic);

            pic.setSize(new Dimension(pic.getWidth(), img.getHeight(pic)));
            pic.setIcon(finalIimage);
        } catch (Exception ex) {
            System.out.println(ex.toString() + "set image size");
        }
    }

并且我还添加了这两种方法:

private static Image ImgResize(Image srcImg, double w, double h, JLabel pic) {
        @SuppressWarnings("UnusedAssignment")

        BufferedImage resizedImg = null;

        int W = pic.getWidth(), H = pic.getHeight();
        double AR = w / h, nh = 0, nw = 0;

        try {
            if (w > h) {
                nw = W;
                nh = nw / AR;
            } else {
                nh = H;
                nw = nh * AR;
            }

            resizedImg = new BufferedImage((int) nw, (int) nh, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = resizedImg.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(srcImg, 0, 0, (int) nw, (int) nh, null);
            g2.dispose();

            return resizedImg;
        } catch (Exception e) {
            System.out.println(e.toString() + " resize");
        }
        return resizedImg;
    }

    private static int[] ImgPosition(int w, int h, JLabel pic) {
        int W = pic.getWidth(), H = pic.getHeight();
        @SuppressWarnings({"UnusedAssignment", "LocalVariableHidesMemberVariable"})
        int x = 0, y = 0;

        int[] a = new int[2];

        try {
            if (w > h) {
                x = 5;
                y = ((H / 2) - (h / 2));
            } else {
                y = 0;
                x = ((W / 2) - (w / 2)) - 30;
            }

        } catch (Exception e) {
            System.out.println(e.toString() + " position");
        }
        a[0] = x;
        a[1] = y;

        return a;
    }
}

我将它添加到主方法:

pic.addComponentListener(new ComponentListener() {

            @Override
            public void componentResized(ComponentEvent e) {
                //==================== Variables Panel ====================
                int panelWidthAndHight = (Integer) ((Math.round(window.getHeight() / 2)) - 2);

                int textWidth = (Integer) ((Math.round(window.getHeight() / 2)) - 75);
                int textHeight = 23;

                int location1 = (Integer) ((Math.round(window.getHeight() / 2)));
                int location2 = (Integer) (Math.round(window.getHeight()));

                int picWidth = (Integer) (Math.round(window.getWidth() - (window.getHeight()) - 2));
                int picHeight = (Integer) (Math.round(window.getHeight() - 3));
                //====================      End    ====================

                //==================== First Panel ====================
                panel.setSize(new Dimension(panelWidthAndHight, panelWidthAndHight));
                panel.setLocation(1, 1);

                amountText1.setSize(new Dimension(textWidth, textHeight));
                LittreText1.setSize(new Dimension(textWidth, textHeight));
                //====================      End    ====================

                //==================== Second Panel ====================
                panel2.setSize(new Dimension(panelWidthAndHight, panelWidthAndHight));
                panel2.setLocation(location1, 1);

                amountText2.setSize(new Dimension(textWidth, textHeight));
                LittreText2.setSize(new Dimension(textWidth, textHeight));
                //====================      End    ====================

                //==================== Third Panel ====================
                panel3.setSize(new Dimension(panelWidthAndHight, panelWidthAndHight - 40));
                panel3.setLocation(1, location1);

                amountText3.setSize(new Dimension(textWidth, textHeight));
                LittreText3.setSize(new Dimension(textWidth, textHeight));
                //====================      End    ====================

                //==================== Third Panel ====================
                panel4.setSize(new Dimension(panelWidthAndHight, panelWidthAndHight - 40));
                panel4.setLocation(location1, location1);

                amountText4.setSize(new Dimension(textWidth, textHeight));
                LittreText4.setSize(new Dimension(textWidth, textHeight));
                //====================      End    ====================

                panel5.setSize(new Dimension(picWidth, picHeight));

                pic.setLocation(xy[0], xy[1]);

                window.invalidate();
            }

            @Override
            public void componentMoved(ComponentEvent e) {
                //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void componentShown(ComponentEvent e) {
                //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void componentHidden(ComponentEvent e) {
                //To change body of generated methods, choose Tools | Templates.
            }
        });

就是这样......

我希望这个答案可以帮助别人。