缓冲图像生成空图像

时间:2019-01-13 05:13:37

标签: java image bufferedimage java-io

我正在尝试从系统读取文件,然后将其转换为PNG并重命名。我不确定为什么我的以下代码会生成空图像:

public boolean processImage(File imageToProcess, String newName, String temp_location, String boxLocation) throws MagickException {
    File renamedImage = renameImage(newName, temp_location);
    boolean isProcessed = convertToPNG(imageToProcess, renamedImage);
    String filePath = renamedImage.getPath();
    return isProcessed;
}

private File renameImage(String newName, String temp_location) {
    File file = new File(temp_location + newName + "." + "png");
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}

private boolean convertToPNG(File imageToConvert, File newImage) throws MagickException {
    boolean isProcessed = false;
    try {
        BufferedImage img = ImageIO.read(imageToConvert);
        isProcessed = ImageIO.write(img, "png", newImage);
    } catch (IOException ioExec) {
        log.error("File not found: " + imageToConvert == null ? "Image is null" : imageToConvert);
    } catch (IllegalArgumentException illegalArgExec) {
        log.error("Image cannot be converted to PNG: " + imageToConvert == null ? "Image is null" : imageToConvert);
    }
    return isProcessed;
}

我已经检查并验证了ImageIO.read试图读取的图像在驱动器中是否存在。

0 个答案:

没有答案