繁体   English   中英

JAVA-上传PNG图片时获取黑色背景

[英]JAVA - Get black background when uploading PNG image

使用用户上传的Google的Thumbnailator保存png文件时出现问题。 它失去了透明度。 这是我的编码:

@RequestMapping(method = RequestMethod.POST, path = "/upload")
@ResponseBody
public String upload(@RequestPart("file") MultipartFile picture) {
    String originalFileName = picture.getOriginalFilename();
    String suffix = 
         originalFileName.substring(originalFileName.lastIndexOf("."));
    String pictureName = UUID.randomUUID().toString() + suffix;
    String fileSavePath = gunsProperties.getFileUploadPath();
    Thumbnails.of(picture.getInputStream()).outputFormat("png")
        .scale(1f).outputQuality(0.15f)
        .toFile(new File(fileSavePath + pictureName));

    return pictureName;
}

它带有黑色背景。 还有其他方法可以将其保存为透明的png吗? 谢谢!

是什么直接将其写入文件而不是使用缩略图? 由于您的缩放比例始终为1.0,因此您可以直接将其写入磁盘:

picture.transferTo(new File(fileSamePath + pictureName));

要么

FileOutputStream fos = new FileOutputStream(new File(fileSamePath + pictureName));
fos.write(picture.getBytes());
fos.close();

或者尝试指定图像格式:

Thumbnails.of(picture.getInputStream()).imageType(BufferedImage.TYPE_INT_ARGB).outputFormat("png").scale(1f).outputQuality(0.15f).toFile(new File(fileSavePath + pictureName));

这将使其具有透明性的24位而不是8位(如果有)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM