我想不失透明度来调整图像(JPG,PNG,GIF)。
我想保存调整后的版本的磁盘调整大小后。
我用Google搜索了很多,但我只发现失去了透明度,并填补了通透的空间黑色的解决方案...
我要寻找一个片断或没有工作图书馆:)
我想不失透明度来调整图像(JPG,PNG,GIF)。
我想保存调整后的版本的磁盘调整大小后。
我用Google搜索了很多,但我只发现失去了透明度,并填补了通透的空间黑色的解决方案...
我要寻找一个片断或没有工作图书馆:)
这是我如何解决我的问题:
public static BufferedImage convertRGBAToIndexed(BufferedImage src) {
BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
Graphics g = dest.getGraphics();
g.setColor(new Color(231, 20, 189));
g.fillRect(0, 0, dest.getWidth(), dest.getHeight());
dest = makeTransparent(dest, 0, 0);
dest.createGraphics().drawImage(src, 0, 0, null);
return dest;
}
public static BufferedImage makeTransparent(BufferedImage image, int x, int y) {
ColorModel cm = image.getColorModel();
if (!(cm instanceof IndexColorModel))
return image; // sorry...
IndexColorModel icm = (IndexColorModel) cm;
WritableRaster raster = image.getRaster();
int pixel = raster.getSample(x, y, 0);
int size = icm.getMapSize();
byte[] reds = new byte[size];
byte[] greens = new byte[size];
byte[] blues = new byte[size];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues, pixel);
return new BufferedImage(icm2, raster, image.isAlphaPremultiplied(), null);
}
public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {
int original_width = imgSize.width;
int original_height = imgSize.height;
int bound_width = boundary.width;
int bound_height = boundary.height;
int new_width = 0;
int new_height = 0;
if (original_width > original_height) {
new_width = bound_width;
new_height = (new_width*original_height)/original_width;
} else {
new_height = bound_height;
new_width = (new_height*original_width)/original_height;
}
return new Dimension(new_width, new_height);
}
public static void resizeImage(File original_image, File resized_image, int IMG_SIZE) {
try {
BufferedImage originalImage = ImageIO.read(original_image);
String extension = Files.getFileExtension(original_image.getName());
int type = extension.equals("gif") || (originalImage.getType() == 0) ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
Dimension new_dim = getScaledDimension(new Dimension(originalImage.getWidth(), originalImage.getHeight()), new Dimension(IMG_SIZE,IMG_SIZE));
BufferedImage resizedImage = new BufferedImage((int) new_dim.getWidth(), (int) new_dim.getHeight(), type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, (int) new_dim.getWidth(), (int) new_dim.getHeight(), null);
g.dispose();
if (!extension.equals("gif")) {
ImageIO.write(resizedImage, extension, resized_image);
} else {
// Gif Transparence workarround
ImageIO.write(convertRGBAToIndexed(resizedImage), "gif", resized_image);
}
} catch (IOException e) {
Utils.log("resizeImage", e.getMessage());
}
}
resarch后,我发现,你可以调整缓存图像只有当你把它作为BufferedImage的,没有它转换成的ImageIcon
我解决这样的proplem:
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
try {
initComponents();
BufferedImage c = ImageIO.read(new File("60_Personnel.png"));
JLabel lblMains = new JLabel( new ImageIcon(c));
this.add(lblMains); //add real size
this.add(new JLabel(new ImageIcon((resize(c, 150))))); //add converting image
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static BufferedImage resize(Object img, int percent) {
BufferedImage buff = (BufferedImage) img;
return resize(buff, buff.getWidth() * percent / 100, buff.getWidth() * percent / 100);
}
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return dimg;
}
}