从HashMap获取旋转图像的问题

时间:2015-01-21 14:11:13

标签: java image rotation hashmap

我有以下问题:

  1. 我不知道为什么我的功能在显示已旋转的图像时,只显示了与给出照片一样多的jpanel中的一个。 让我举一个例子:我将4个图像加载到hashmap中。然后我将它们放入我的旋转功能并尝试显示它们。我最终得到了一张旋转图像的4个标签。

  2. 当我尝试使用与之前使用的相同的深度旋转两次时(我使用滑块获取度数)它不再旋转但是如果我触摸滑块并移动它,则该功能正常旋转

  3. 我不知道如何将Image类型转换为File对象,因此我将其转换为BufferedImage,然后转换为File。我浏览了互联网,但找不到任何有用的东西。

  4. 我的主要代码:

    JPanel panel_2e = new JPanel();
            panel_2e.setOpaque(true);
            panel_2e.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.GRAY, Color.DARK_GRAY), "Rotate options"));
            panel_2e.setLayout(new BoxLayout(panel_2e,BoxLayout.Y_AXIS));
            JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 360, 180);
    
            slider.setMinorTickSpacing(10);
            slider.setMajorTickSpacing(90);
            slider.setPaintTicks(true);
            slider.setPaintLabels(true);
            slider.setLabelTable(slider.createStandardLabels(45));
    
            JPanel radio_buttons_rotate=new JPanel();
            radio_buttons_rotate.setLayout(new BoxLayout(radio_buttons_rotate, BoxLayout.Y_AXIS));
    
            JCheckBox cut_frame = new JCheckBox("Cut edges");
            cut_frame.setSelected(true);
            cut_frame.addItemListener(new ItemListener()  {
                    public void itemStateChanged(ItemEvent e) {
                        if(e.getStateChange() == ItemEvent.SELECTED)
                        {check = true;
                            System.out.println("Cut-edge mode selected");}
                        else
                        {check = false;
                            System.out.println("Cut-edge mode deselected");}
                    }
            });
            cut_frame.setBounds(78, 41, 60, 23);
            radio_buttons_rotate.add(cut_frame);
    
            JRadioButton black_rdbutton = new JRadioButton("Black background");
            black_rdbutton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                           black = true;
                    }
            });
    
    
            black_rdbutton.setBounds(78, 41, 60, 23);
            radio_buttons_rotate.add(black_rdbutton);
    
    
            JRadioButton white_rdbutton = new JRadioButton("White background");
            white_rdbutton.setSelected(true);
            white_rdbutton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                          black = false;
                    }
            });
    
    
            white_rdbutton.setBounds(78, 41, 60, 23);
            radio_buttons_rotate.add(white_rdbutton);
    
            ButtonGroup group_frame_rotate = new ButtonGroup();
            group_frame_rotate.add(black_rdbutton);
            group_frame_rotate.add(white_rdbutton);
    
            panel_2e.add(radio_buttons_rotate);
    
            JLabel Rotate  = new JLabel();
            Rotate.setText(deg+"\u00b0");
            panel_2e.add(slider);
            JButton btnRotate = new JButton("Rotate");
            JPanel RotatePanel=new JPanel(); 
            RotatePanel.add(Rotate);
            RotatePanel.add(btnRotate);
            RotatePanel.setLayout(new FlowLayout());
            btnRotate.setBounds(28, 158, 89, 23);
            btnRotate.setBackground(Color.DARK_GRAY);
            btnRotate.setForeground(Color.WHITE);
            panel_2e.add(RotatePanel);
    
    
    
    
            panel_2.add(panel_2e);
            slider.addChangeListener(new ChangeListener() {
    
                 public void stateChanged(ChangeEvent ce) {
                     deg=((JSlider) ce.getSource()).getValue(); 
    
                     Rotate.setText(deg+"\u00b0");                 }
    
            });
            btnRotate.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    if(AddDirectory.all_chosen.isEmpty())
                    {Object[] options = {"OK"};
                    int n = JOptionPane.showOptionDialog(frame,
                            "Please choose at least 1 image to rotate.","Empty work list",
                               JOptionPane.PLAIN_MESSAGE,
                               JOptionPane.QUESTION_MESSAGE,
                               null,
                               options,
                               options[0]);}
                    else
                    {RotateFunction rot = new RotateFunction(AddDirectory.all_chosen);
                    rot.main();
                    BufferedImage kk = null;
                            Display disp= new Display(); // invoke Display class
    
                            for(File i : all_chosen_images.values()){
                                    try{
                                    kk = ImageIO.read(new File(i.getAbsolutePath()));
                                    disp.createFrame(center, i, kk); //create new frame with image 
                                    }
                                    catch (IOException es){
                                            es.printStackTrace();
                                    }
    
                            }
                            }
                }
            });
    

    这是我的旋转功能:

    public class RotateFunction{
    
        HashMap<JButton,File> map;
        Image spiral;
    
    
        RotateFunction(HashMap<JButton,File> source_of_image)
        {
                map = AddDirectory.all_chosen;
        }
    
    
         public void main(){
                int counter = 0;
    
                if (spiral == null){
                        for(Map.Entry<JButton, File> entry: map.entrySet())   //////Moving through hashMap
                        {
                    try {
                        spiral = getImage(entry.getValue().getAbsolutePath());
                        counter++;
                        System.out.println("Path of image " + counter + " : " +entry.getValue().getAbsolutePath());  
                        if (PhotoEdit.check == true){
                            rotateImage(PhotoEdit.deg, null);
                            System.out.println("Rotating image "+counter+" by "+PhotoEdit.deg+" degrees (edge cut)");
                        }
                        else{
                            rotateImage1(PhotoEdit.deg, null);
                            System.out.println("Rotating image "+counter+" by "+PhotoEdit.deg+" degrees (bigger frame)");   
                        }
                       BufferedImage tmp = toBufferedImage(spiral);
                       File f;
                       f = new File( "image.png" );  
                       try
                       {  
    
                        ImageIO.write( tmp, "PNG", f );  
    
                       }  
                       catch ( IOException x )
                       {  
                           // Complain if there was any problem writing  
                           // the output file.  
                           x.printStackTrace();  
                       }        
                        map.put(entry.getKey(), f);
                    }
                    catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
    
                        } 
                        PhotoEdit.all_chosen_images.clear();
                        PhotoEdit.all_chosen_images.putAll(map);  //Putting HashMap with rotated images into global HashMap
                        System.out.println("Images have been successfully rotated");
                }
        }
    
        public Image getImage(String path){
                Image tempImage = null;
                try
                {
                        tempImage = Toolkit.getDefaultToolkit().getImage(path);
                }
                catch (Exception e)
                {
                        System.out.println("An error occured - " + e.getMessage());
                }
                return tempImage;
        }
    
        ////////////////////////////////////////////////////////////////////
    
        ///////////////////IMAGE ROTATION /////////////////////////////////
    
        public void rotateImage(double degrees, ImageObserver o){
                ImageIcon icon = new ImageIcon(this.spiral);
                BufferedImage blankCanvas = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2 = (Graphics2D)blankCanvas.getGraphics();
                g2.rotate(Math.toRadians(degrees), icon.getIconWidth()/2, icon.getIconHeight()/2);
                g2.drawImage(this.spiral, 0, 0, o);
                this.spiral = blankCanvas;
        }
    
        public void rotateImage1(double degrees, ImageObserver o){
            double sin = Math.abs(Math.sin(Math.toRadians(degrees)));
            double cos = Math.abs(Math.cos(Math.toRadians(degrees)));
            ImageIcon icon = new ImageIcon(this.spiral);
            int w = icon.getIconWidth();
            int h = icon.getIconHeight();
            int neww = (int)Math.floor(w*cos+h*sin);
            int newh = (int)Math.floor(h*cos+w*sin);
            BufferedImage blankCanvas = new BufferedImage(neww, newh, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = (Graphics2D)blankCanvas.getGraphics();
            g2.translate((neww-w)/2, (newh-h)/2);
            g2.rotate(Math.toRadians(degrees), icon.getIconWidth()/2, icon.getIconHeight()/2);
            g2.drawImage(this.spiral, 0, 0, o);
            this.spiral = blankCanvas;
    }
    //////////////////////////////////////////////////////////////////////////
    
        public static BufferedImage toBufferedImage(Image img)
        {
            if (img instanceof BufferedImage)
            {
                return (BufferedImage) img;
            }
    
            // Create a buffered image with transparency
            BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    
            // Draw the image on to the buffered image
            Graphics2D bGr = bimage.createGraphics();
            bGr.drawImage(img, 0, 0, null);
            bGr.dispose();
    
            // Return the buffered image
            return bimage;
        }
    

    }

    在显示功能中,我有以下参数 - createFrame(JDekstopPane where_to_put_the_image,File img,BufferedImage image)

    HashMap - all_chosen_images是存储所有图像的全局HashMap

    真的希望得到你的帮助。 亚历山大

0 个答案:

没有答案