java - 缩放文件夹中的图像

标签 java

我正在尝试使用文件选择器调整图片大小。看起来一切都是文件,但添加文件夹后我无法打开它。

public void metodAddpath(String fullPath)  {

try {
                       File sourceFile = new File(fullPath);               
                     BufferedImage bufferedimage = ImageIO.read(sourceFile);
                     ByteArrayOutputStream os = new ByteArrayOutputStream();
                     ImageIO.write(bufferedimage, "jpg", os);
                      InputStream is = new ByteArrayInputStream(os.toByteArray());
        FileOutputStream fileOutputStream = new FileOutputStream(
                        sourceFile);

        int bufferSize;
        byte[] bufffer = new byte[512];

        while ((bufferSize = is.read(bufffer)) > 0) {
            fileOutputStream.write(bufffer, 0, bufferSize);
        }
        is.close();
        fileOutputStream.close();             

                    //scaleImage(bufferedimage, 220, 220);  

} catch(Exception e) {
    e.printStackTrace();
}

} 当我按下按钮将图像保存到文件夹中后。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    Database base = new  Database();
   metodAddpath(jTextField1.getText());

    base.addPictureResource(jTextField1.getText());
}

但是当我尝试将其添加到文件夹中时,出现错误。

最佳答案

我只是想站出来说出来,这些都不是......

try {
    File sourceFile = new File(fullPath);               
    BufferedImage bufferedimage = ImageIO.read(sourceFile);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(bufferedimage, "jpg", os);
    InputStream is = new ByteArrayInputStream(os.toByteArray());
    FileOutputStream fileOutputStream = new FileOutputStream(
    sourceFile);

    int bufferSize;
    byte[] bufffer = new byte[512];

    while ((bufferSize = is.read(bufffer)) > 0) {
        fileOutputStream.write(bufffer, 0, bufferSize);
    }
    is.close();
    fileOutputStream.close();             

    //scaleImage(bufferedimage, 220, 220);  

} catch(Exception e) {
    e.printStackTrace();
}

有道理。

您正在读取图像,将其写入ByteArrayOutputStream,通过InputStream进行管道传输,然后使用该输入流通过a将内容写入另一个文件FileOutputStream ...为什么?!

类似...

File sourceFile = new File(fullPath);               
try {
    BufferedImage bufferedimage = ImageIO.read(sourceFile);

    //scaleImage(bufferedimage, 220, 220);  

    // Beware, this is overwriting the existing file
    try (FileOutputStream fileOutputStream = new FileOutputStream(sourceFile)) {
        ImageIO.write(bufferedimage, "jpg", fileOutputStream);
    }
} catch(Exception e) {
    e.printStackTrace();
}

会做同样的工作,更容易阅读并且可能更高效......

我怀疑这会回答你的问题,但它可能会减少一些困惑

关于java - 缩放文件夹中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53548804/

相关文章:

java - Maximo - 使用 java 代码获取当前工作流程位置

java - 如何在 JFreeChart 中显示过滤后的数据

java - 正则表达式的 Java 输入验证问题

java - Android:无法从静态上下文中引用非静态方法

java - 迭代列表并根据日期分组

java - 我如何在java中读取特定数据

java - 在 Spring Boot 应用程序上使用 Flyway 时如何在 H2 中加载初始数据?

java - 如何高效地查看来自url的图片

c# - C# 中的 Java System.currentTimeMillis() 等效项

java - 从 Java 中的二维数组矩阵获取行和列