Re: java.awt.image.RescaleOp.filter() causes VM crash: EXCEPTION_ACCESS_VIOLATION
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 28 Jun 2008 17:33:47 -0700
Bob Wobbler wrote:
I'm trying to scale a BufferedImage to fit it into a JLabel. First I
calculate the correct scale factor, the create the operation and a new
BufferedImage to copy the scaled image to.
But once my code reaches the RescaleOp's filter Operation the virtual
machine crashes. I couldn't really find any posts on this problem,
still I hope someone here might have solved this problem before.
Btw the method BufferedImage.getScaledInstance works, the only problem
is that it's result is an Image, but I need a BufferedImage because I
have to do some further processing with the data, but can't read any
pixels from the Image object this operation returns. So an alternative
solution would be to convert the Image to a BufferedImage - only
problem is that I don't know how to do that either.
Here's the code:
// read image
BufferedImage originalImage = ImageIO.read(imageFile);
// calculate scale factors for width and heigth
float scaleWidthFactor = (float) PANEL_WIDTH / (float)
originalImage.getWidth();
float scaleHeightFactor = (float) PANEL_HEIGHT / (float)
originalImage.getHeight();
// choose correct scale factor to fit image into the label
float scaleFactor = (scaleWidthFactor < scaleHeightFactor) ?
scaleWidthFactor : scaleHeightFactor;
// create rescale operation
BufferedImageOp rescaleOp = new RescaleOp(scaleFactor, 0, null);
// create new buffered image with correct dimensions for scaling
operation
BufferedImage scaledImage =
rescaleOp.createCompatibleDestImage(originalImage,
originalImage.getColorModel());
// scale image - CAUSES VM TO CRASH
rescaleOp.filter(originalImage, scaledImage);
// .. some more code (working)
centerLabel.setIcon(new ImageIcon(scaledImage));
And here's the error message I get:
#
# An unexpected error has been detected by Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d15d65d,
pid=23884, tid=20788
#
# Java VM: Java HotSpot(TM) Client VM (1.6.0_03-b05 mixed mode)
# Problematic frame:
# C [awt.dll+0xad65d]
#
# An error report file with more information is saved as
hs_err_pid23884.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
#
Thanks for your time & any hints you can give.
Robert
I'm getting the same error on Windows and none on Fedora 9.
But that really isn't what you need. RescaleOp doesn't do what you expect it to do, it is to modify the colors of the image. I know the name fooled me too. What you need is an AffineTransformOp. Use a scale instance of the AffineTransform to scale your image.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class test5 extends JPanel {
BufferedImage small;
public test5() {
try {
BufferedImage big = ImageIO.read(new File("kittens.jpg"));
AffineTransform at =
AffineTransform.getScaleInstance(0.4,0.4);
AffineTransformOp op =
new AffineTransformOp(at,(RenderingHints)null);
small = op.filter(big,null);
setPreferredSize(new Dimension(
small.getWidth(),small.getHeight()));
} catch (Exception e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
g.drawImage(small,0,0,null);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test5 t5 = new test5();
f.add(t5,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}
--
Knute Johnson
email s/nospam/knute2008/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
.
- Follow-Ups:
- References:
- Prev by Date: Re: Weird window close behavior
- Next by Date: Re: teste
- Previous by thread: java.awt.image.RescaleOp.filter() causes VM crash: EXCEPTION_ACCESS_VIOLATION
- Next by thread: Re: java.awt.image.RescaleOp.filter() causes VM crash: EXCEPTION_ACCESS_VIOLATION
- Index(es):
Relevant Pages
|