Java中动态调整缓冲图像大小

6

我尝试使用AffineTransform和Scalr.resize来调整缓冲图像的大小。

以下是它们各自的代码。

使用Scalr.resize:

    BufferedImage buff = robot.createScreenCapture(new Rectangle(bufx,bufy,bufwidth,bufheight)); // x-coord, y-coord, width, height

    BufferedImage scrCapt = Scalr.resize(buff, Method.BALANCED, scrwidth, scrheight);

使用 AffineTransform:

BufferedImage buff = robot.createScreenCapture(new Rectangle(bufx,bufy,bufwidth,bufheight)); // x-coord, y-coord, width, height

BufferedImage scrCapt = new BufferedImage(bufwidth,bufheight,BufferedImage.TYPE_INT_ARGB);
AffineTransform atscr = new AffineTransform();


atscr.scale(aspectRatioWidth,aspectRatioHeight);
AffineTransformOp scaleOp = new AffineTransformOp(atscr, AffineTransformOp.TYPE_BILINEAR);
scrCapt = scaleOp.filter(buff, scrCapt);

变量已在类的开头声明:

static int bufx = 0;
static int bufy = 0;
static int bufwidth = 1;
static int bufheight = 1;
static int scrwidth = 0;
static int scrheight = 0;
static float aspectRatioWidth = 0;
static float aspectRatioHeight = 0;

我正在另一个方法中动态获取所有变量的值:

aspectRatioWidth = bufwidth/scrwidth;
aspectRatioHeight = bufheight/scrheight;

然而,当我运行此代码时,无论是在函数AffineTransform还是Scalr.resize中都会出现错误:
Scalr.resize:
Exception in thread "Thread-2" java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:331)
at org.imgscalr.Scalr.createOptimalImage(Scalr.java:2006)
at org.imgscalr.Scalr.scaleImage(Scalr.java:2133)
at org.imgscalr.Scalr.resize(Scalr.java:1667)
at org.imgscalr.Scalr.resize(Scalr.java:1415)

AffineTransform:

Exception in thread "Thread-2" java.awt.image.ImagingOpException: Unable to invert transform AffineTransform[[0.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
at java.awt.image.AffineTransformOp.validateTransform(AffineTransformOp.java:558)
at java.awt.image.AffineTransformOp.<init>(AffineTransformOp.java:151)

我该怎么做? 我知道这是因为我在不同的方法中更改了变量并在另一个方法中访问它们。 但是这两个方法不能合并。 有没有办法让这个工作?

编辑:

我更改了调整大小的方法 现在这是我所做的

public static BufferedImage resizeImage(BufferedImage image, double scalewidth, double scaleheight){

    BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(),BufferedImage.SCALE_FAST);
    Graphics2D g = img.createGraphics();
    g.scale(scalewidth, scaleheight);
    g.drawImage(image, null, 0, 0);
    g.dispose();
    return image;
}

编辑(2):

为了更清楚地了解正在发生的情况:

这是一种返回scrwidth和scrheight的方法。

public static void showOnScreen( int screen, JFrame framenew )
   {
    GraphicsEnvironment ge = GraphicsEnvironment
      .getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();

  for (int i = 0; i < gs.length; i++) {
      screenwidth.add(gs[i].getDisplayMode().getWidth());
      screenheight.add(gs[i].getDisplayMode().getHeight());
}

scrwidth = screenwidth.get(screenwidth.size()-1);
scrheight = screenheight.get(screenheight.size()-1);




  System.out.print(ge);
  System.out.print(gs);
  if( screen > -1 && screen < gs.length )
  {gs[screen].setFullScreenWindow( framenew );}
  else if( gs.length > 0 )
  {gs[0].setFullScreenWindow( framenew );}
  else
  {throw new RuntimeException( "No Screens Found" );}}

这是一个返回bufwidth和bufheight的ActionListener:

  btnNewButton.addActionListener(new ActionListener() {      
  public void actionPerformed(ActionEvent e)
  {
      //Execute when button is pressed
      System.out.println("You clicked the button");

      int ind = c.getSelectedIndex();
        bufx = capx.get(ind);
        bufy = capy.get(ind);
        bufwidth = capwidth.get(ind);
        bufheight = capheight.get(ind);
        frame.setVisible(false);
        framenew.setVisible(true);
        showOnScreen(1,framenew);

        aspectRatioWidth = (double) bufwidth/scrwidth;
        aspectRatioHeight = (double) bufheight/scrheight;   

            System.out.print("aspectRatioWidth:  ");
            System.out.println(aspectRatioWidth);

            System.out.print("aspectRatioHeight:  ");
            System.out.println(aspectRatioHeight);          
  }
  });      

而 aspectRatios 在 run 中使用:

public void run() {
System.out.print("aspectRatioWidth:  ");
System.out.println(aspectRatioWidth);

System.out.print("aspectRatioHeight:  ");
System.out.println(aspectRatioHeight);

while(true){
    BufferedImage buff = robot.createScreenCapture(new Rectangle(bufx,bufy,bufwidth,bufheight)); // x-coord, y-coord, width, height

    BufferedImage resizedbuff = resizeImage(buff, aspectRatioWidth, aspectRatioHeight);}

那么,你做了一些更改(你是否已经进行了我推荐的更改?)——你的更改有什么作用?你的代码现在工作了吗?你还能看到异常吗? - Hovercraft Full Of Eels
我将数字转换为double类型。 当我在方法内执行除法并返回bufwidth、bufheight和所有变量时,它会给出正确的值。但是,在run()内打印aspectRatioWidth和aspectRatioHeight时,它返回零。我理解这是因为在实现run()时,所有变量的值尚未递增。如何确保只有在值递增后才运行run()? - newbie2015
1个回答

5
您正在进行整数除法,这总是返回一个整数,因此您的屏幕尺寸可能大于图像尺寸,所以结果为0:
aspectRatioWidth = bufwidth/scrwidth;
aspectRatioHeight = bufheight/scrheight;

解决方案:将数字转换为双精度浮点数,然后进行双精度除法。
aspectRatioWidth = (double) bufwidth/scrwidth;
aspectRatioHeight = (double) bufheight/scrheight;

编辑

不确定你最终想要做什么——在GUI中发布计算机屏幕的图像吗?如果是这样,也许可以尝试类似以下方式...

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.util.List;

import javax.swing.*;

public class ChangeVars extends JPanel {
   private static final int PREF_W = 400;
   private static final int PREF_H = PREF_W;
   private static final int DELAY = 20;
   public BufferedImage displayImage;
   private MyWorker myWorker;

   public ChangeVars() {
      try {
         myWorker = new MyWorker(DELAY);
         myWorker.execute();
      } catch (AWTException e) {
         e.printStackTrace();
      }
   }

   @Override
   // to initialize the panel to something
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (displayImage != null) {
         g.drawImage(displayImage, 0, 0, null);
      }
   }

   public void stopWorker() {
      if (myWorker != null && !myWorker.isDone()) {
         myWorker.setRunning(false);
         myWorker.cancel(true);
      }
   }

   private class MyWorker extends SwingWorker<Void, BufferedImage> {

      private volatile boolean running = true;
      private Robot robot;
      private int delay;

      public MyWorker(int delay) throws AWTException {
         this.delay = delay;
         robot = new Robot();
      }

      @Override
      protected Void doInBackground() throws Exception {
         while (running) {
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle screenRect = new Rectangle(0, 0, d.width, d.height);
            BufferedImage img = robot.createScreenCapture(screenRect);
            publish(img);
            Thread.sleep(delay);
         }
         return null;
      }

      @Override
      protected void process(List<BufferedImage> chunks) {
         for (BufferedImage image : chunks) {
            Dimension sz = getSize();
            double scaleX = (double) sz.width / image.getWidth();
            double scaleY = (double) sz.height / image.getHeight();
            AffineTransform transform = AffineTransform.getScaleInstance(
                  scaleX, scaleY);
            AffineTransformOp transformOp = new AffineTransformOp(transform,
                  AffineTransformOp.TYPE_BILINEAR);
            displayImage = new BufferedImage(sz.width, sz.height,
                  BufferedImage.TYPE_INT_ARGB);
            displayImage = transformOp.filter(image, displayImage);
            repaint();
         }
      }

      public void setRunning(boolean running) {
         this.running = running;
      }

      public boolean getRunning() {
         return running;
      }

   }

   private static void createAndShowGui() {
      final ChangeVars changeVars = new ChangeVars();

      JFrame frame = new JFrame("ChangeVars");
      frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
      frame.addWindowListener(new WindowAdapter() {

         @Override
         public void windowClosing(WindowEvent e) {
            if (changeVars != null) {
               changeVars.stopWorker();
            }
            System.exit(0);
         }

      });
      frame.getContentPane().add(changeVars);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

虽然更简单的方法是让paintComponent完成缩放:

@Override
protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  if (displayImage != null) {
     int width = getWidth();
     int height = getHeight();
     g.drawImage(displayImage, 0, 0, width, height, null);
  }
}

// ....

  @Override
  protected void process(List<BufferedImage> chunks) {
     for (BufferedImage image : chunks) {
        displayImage = image;
        repaint();
     }
  }

我正在使用不同的方法来实现图像缩放,它工作得很好,但现在我意识到有一个不同的问题。当我在run()函数中访问它们时,这两个变量在另一个函数中被更改了。因此,当aspectRatioWidth和aspectRatioHeight始终返回零时,因为bufwidth和bufheight已经初始化为零。有没有办法确保在运行run()之前实现其他函数? - newbie2015
@newbie2015:所以现在你正在对我们改变需求。将来请尽量避免这样做,因为这可能让我们感到沮丧,并使现有的答案无效。至于你的问题,如果你现在遇到了一个新的问题,那么编辑你的问题并尽可能地澄清你的问题。根据你上面发布的内容的简介,我不确定我能完全理解问题在哪里。例如,我无法知道变量之间的关系如何。 - Hovercraft Full Of Eels
@newbie2015:考虑创建并发布一个“最小完整可验证示例程序”(Minimal, Complete, and Verifiable Example Program),其中你将代码压缩到最小的能够编译和运行的程度,没有外部依赖(例如需要链接到数据库或图像),没有与你的问题无关的额外代码,但仍然能够演示你的问题。另外,你当前的代码似乎过度使用了静态字段,这通常应该避免。 - Hovercraft Full Of Eels
@newbie2015:这段代码的目的是什么?为什么要创建所有这些图片?为什么使用一个未受控制的 while (true) 轮询循环? - Hovercraft Full Of Eels
让我们在聊天中继续这个讨论。点击此处进入聊天室 - newbie2015
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接