如何在没有拉伸或扭曲的情况下裁剪和缩放图像?

时间:2013-09-26 13:54:17

标签: java swing awt imageicon

我正在尝试缩放拍摄的图像:

robot.createScreenCapture(new Rectangle(x,y,area.getWidth(),area.getHeight()

但是在裁剪之后,当我将它放入jLabel(高度:165px和宽度:214)时,它会被拉伸和扭曲。临时文件夹中存储的图像很好。这是我的代码:

  public void cropImage(){

        try {
        //Execute when button is pressed  
                //Point pt = resizer.getLocation();  
               // this.setVisible(false);
                Robot robot= new Robot();

               Point pt = this.resizer.getLocationOnScreen();

               // int x = ((int)this.getLocation().getX()+1)+(int)pt.getX()+1;
               // int y = ((int)this.getLocation().getY()+1)+(int)pt.getY()+1;

                int x = (int)pt.getX()+1;
                int y = (int)pt.getY()+1;
                System.out.println("----------------------------");
                System.out.println(" X : "+x +" Y : "+ y);
                System.out.println("----------------------------");
                System.out.println(" Width : "+area.getWidth() +" Height : "+ getHeight());

                BufferedImage img = robot.createScreenCapture(new Rectangle(x,y,getWidth(),area.getHeight()));
                String saveFilePath = Common.getTempPath()+Common.getSeparator()+"temp.jpg";
                File save_path=new File(saveFilePath); 
                ImageIO.write(img, "JPG", save_path);

                emf.createInsideElements(saveFilePath);
                emf.repaint();
                emf.pack();
                this.dispose();

        } catch (AWTException ex) {
             Logger.getLogger(ResizableComponent.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
             Logger.getLogger(ResizableComponent.class.getName()).log(Level.SEVERE, null, ex);
        } 


  }

0 个答案:

没有答案