bir BufferedImage boyutlandırmak.Dinamik ben ikisi için Kodlarımı AffineTransform kullanarak tamponlu görüntüyü yeniden boyutlandırma yanı sıra Burada <p>Scalr.resize</p> vardır çalıştı java
kullanarak 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 kullanarak:
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;
Ben değerlerini alıyorum:
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);
değişkenler sınıf içinde başlangıçta ilan edilmiştir Farklı bir yöntemde dinamik olarak tüm değişkenler için:
Bu kodu çalıştırdığınızdaaspectRatioWidth = bufwidth/scrwidth;
aspectRatioHeight = bufheight/scrheight;
Ancak şu hata mesajı hem işlevleri Scalr.resize yanı sıra AffineTransform:
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)
Bunun için nasıl giderim? Bunun gerçekleştiğini anlıyorum çünkü değişkeni farklı bir yöntemle değiştiriyorum ve başka bir tanesine erişiyorum. Ancak bu iki yöntem birleştirilemez. Bu çalışmayı yapabileceğim herhangi bir yol var mı?
DÜZENLEME: neler olduğunu daha net bir fikir için
: Ben burada yeniden boyutlandırma yöntemi değişti
Şimdi
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;
}
DÜZENLEME (2) yaptığı şey tam olarak:
Bu bir yöntemdir. scrwidth döner ve
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");}}
scrheight Ve bu bufwidth ve bufheight döndüren ActionListener geçerli:
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);
}
});
Ve aspectRatios vadede iç kullanılır: Sen int bölümü yapıyoruz
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);}
Yani ... bazı değişiklikler yaptık (Yaptığınız var değişiklik önerdim?) - değişiklikleriniz ne yaptı? Kodunuz şimdi çalışıyor mu? Hala bir istisna görüyor musunuz? –
yes Sayıları ikiye dönüştürdüm. Ben ben vadede() içindeki aspectRatioWidth ve aspectRatioHeight çıktısını sıfır dönüyor Ancak Bunun nedeni oluyor anlıyorum bufwidth, bufheight ve tüm değişkenleri dönen hangi yöntemle iç bölme işlemlerini yaparken Doğru değerini veriyor run() uygulandığında, tüm değişkenlerin değerleri henüz artırılmamıştır Değer() değerlerin arttıktan sonra gerçekleştiğinden nasıl emin olabilirim? – newbie2015