can anyone help me how to save my resize image in a folder? i am using bufferedimage and jfilechooser. please guys help me.
任何人都可以帮我如何将我的调整大小图像保存在文件夹中?我正在使用bufferedimage和jfilechooser。请大家帮帮我
private void cmd_attachActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser ch = new JFileChooser();
ch.showOpenDialog(null);
File f=ch.getSelectedFile();
filename =f.getAbsolutePath();
path.setText(filename);
try{
File image = new File(filename);
BufferedImage bufferedimage=ImageIO.read(image);
BufferedImage thumbnail=Thumbnails.of(bufferedimage)
.size(150,188)
.asBufferedImage();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(thumbnail, "jpeg", os);
InputStream is=new ByteArrayInputStream(os.toByteArray());
// FileInputStream fis=new FileInputStream(image);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
try{
for (int readNum; (readNum=is.read(buff ))!=-1;){
bos.write(buff,0,readNum);
System.out.println("Read" +readNum+ "bytes,");
}
}catch(IOException ex){
java.util.logging.Logger.getLogger(Employee_info.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
person_name=bos.toByteArray();
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Select an image!");
}
finally{
try{
pst.close();
rs.close();
}catch(Exception e){
}
}
Update_table();
}
1 个解决方案
#1
If you already have BufferedImage
you can use Graphics2D
如果您已经拥有BufferedImage,则可以使用Graphics2D
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
or AffineTransformation
to scale images
或AffineTransformation来缩放图像
/**
* scale image
*
* @param image to scale
* @param type type of image
* @param dWidth width of destination image
* @param dHeight height of destination image
* @param fWidth x-factor for transformation / scaling
* @param fHeight y-factor for transformation / scaling
* @return scaled image
*/
public static BufferedImage scale(BufferedImage image, int type, int dWidth, int dHeight, double fWidth, double fHeight) {
BufferedImage dbi = null;
if(image != null) {
dbi = new BufferedImage(dWidth, dHeight, type);
Graphics2D g = dbi.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance(fWidth, fHeight);
g.drawRenderedImage(image , at);
}
return dbi;
}
#1
If you already have BufferedImage
you can use Graphics2D
如果您已经拥有BufferedImage,则可以使用Graphics2D
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
or AffineTransformation
to scale images
或AffineTransformation来缩放图像
/**
* scale image
*
* @param image to scale
* @param type type of image
* @param dWidth width of destination image
* @param dHeight height of destination image
* @param fWidth x-factor for transformation / scaling
* @param fHeight y-factor for transformation / scaling
* @return scaled image
*/
public static BufferedImage scale(BufferedImage image, int type, int dWidth, int dHeight, double fWidth, double fHeight) {
BufferedImage dbi = null;
if(image != null) {
dbi = new BufferedImage(dWidth, dHeight, type);
Graphics2D g = dbi.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance(fWidth, fHeight);
g.drawRenderedImage(image , at);
}
return dbi;
}