mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -62,8 +62,13 @@ public class QrCodeUtil {
|
||||
* @return 图片 Base64 编码字符串
|
||||
*/
|
||||
public static String generateAsBase64DataUri(final String content, final QrConfig qrConfig, final String imageType) {
|
||||
final BufferedImage img = generate(content, qrConfig);
|
||||
return ImgUtil.toBase64DataUri(img, imageType);
|
||||
BufferedImage img = null;
|
||||
try{
|
||||
img = generate(content, qrConfig);
|
||||
return ImgUtil.toBase64DataUri(img, imageType);
|
||||
} finally {
|
||||
ImgUtil.flush(img);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,8 +110,13 @@ public class QrCodeUtil {
|
||||
* @return 目标文件
|
||||
*/
|
||||
public static File generate(final String content, final int width, final int height, final File targetFile) {
|
||||
final BufferedImage image = generate(content, width, height);
|
||||
ImgUtil.write(image, targetFile);
|
||||
BufferedImage image = null;
|
||||
try{
|
||||
image = generate(content, width, height);
|
||||
ImgUtil.write(image, targetFile);
|
||||
} finally {
|
||||
ImgUtil.flush(image);
|
||||
}
|
||||
return targetFile;
|
||||
}
|
||||
|
||||
@@ -120,8 +130,13 @@ public class QrCodeUtil {
|
||||
* @since 4.1.2
|
||||
*/
|
||||
public static File generate(final String content, final QrConfig config, final File targetFile) {
|
||||
final BufferedImage image = generate(content, config);
|
||||
ImgUtil.write(image, targetFile);
|
||||
BufferedImage image = null;
|
||||
try{
|
||||
image = generate(content, config);
|
||||
ImgUtil.write(image, targetFile);
|
||||
} finally {
|
||||
ImgUtil.flush(image);
|
||||
}
|
||||
return targetFile;
|
||||
}
|
||||
|
||||
@@ -135,8 +150,13 @@ public class QrCodeUtil {
|
||||
* @param out 目标流
|
||||
*/
|
||||
public static void generate(final String content, final int width, final int height, final String imageType, final OutputStream out) {
|
||||
final BufferedImage image = generate(content, width, height);
|
||||
ImgUtil.write(image, imageType, out);
|
||||
BufferedImage img = null;
|
||||
try{
|
||||
img = generate(content, width, height);
|
||||
ImgUtil.write(img, imageType, out);
|
||||
} finally {
|
||||
ImgUtil.flush(img);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,8 +169,13 @@ public class QrCodeUtil {
|
||||
* @since 4.1.2
|
||||
*/
|
||||
public static void generate(final String content, final QrConfig config, final String imageType, final OutputStream out) {
|
||||
final BufferedImage image = generate(content, config);
|
||||
ImgUtil.write(image, imageType, out);
|
||||
BufferedImage image = null;
|
||||
try{
|
||||
image = generate(content, config);
|
||||
ImgUtil.write(image, imageType, out);
|
||||
} finally {
|
||||
ImgUtil.flush(image);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,7 +226,13 @@ public class QrCodeUtil {
|
||||
* @return 解码文本
|
||||
*/
|
||||
public static String decode(final InputStream qrCodeInputstream) {
|
||||
return decode(ImgUtil.read(qrCodeInputstream));
|
||||
BufferedImage image = null;
|
||||
try{
|
||||
image = ImgUtil.read(qrCodeInputstream);
|
||||
return decode(image);
|
||||
} finally {
|
||||
ImgUtil.flush(image);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,7 +242,13 @@ public class QrCodeUtil {
|
||||
* @return 解码文本
|
||||
*/
|
||||
public static String decode(final File qrCodeFile) {
|
||||
return decode(ImgUtil.read(qrCodeFile));
|
||||
BufferedImage image = null;
|
||||
try{
|
||||
image = ImgUtil.read(qrCodeFile);
|
||||
return decode(image);
|
||||
} finally {
|
||||
ImgUtil.flush(image);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -32,10 +32,7 @@ import java.awt.geom.RoundRectangle2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.CropImageFilter;
|
||||
import java.awt.image.ImageFilter;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
|
||||
@@ -45,7 +42,7 @@ import java.nio.file.Path;
|
||||
* @author looly
|
||||
* @since 4.1.5
|
||||
*/
|
||||
public class Img implements Serializable {
|
||||
public class Img implements Flushable, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final BufferedImage srcImage;
|
||||
@@ -810,6 +807,12 @@ public class Img implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
ImgUtil.flush(this.srcImage);
|
||||
ImgUtil.flush(this.targetImage);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------- Private method start
|
||||
|
||||
/**
|
||||
|
@@ -20,6 +20,7 @@ import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.io.resource.Resource;
|
||||
import org.dromara.hutool.core.io.stream.FastByteArrayOutputStream;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.lang.tuple.Pair;
|
||||
import org.dromara.hutool.core.math.NumberUtil;
|
||||
import org.dromara.hutool.core.net.url.UrlUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
@@ -1550,12 +1551,12 @@ public class ImgUtil {
|
||||
*
|
||||
* @param image {@link Image}
|
||||
* @param imageType 图片类型(图片扩展名)
|
||||
* @param destImageStream 写出到的目标流
|
||||
* @param targetImageStream 写出到的目标流
|
||||
* @throws IORuntimeException IO异常
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public static void write(final Image image, final String imageType, final ImageOutputStream destImageStream) throws IORuntimeException {
|
||||
write(image, imageType, destImageStream, 1);
|
||||
public static void write(final Image image, final String imageType, final ImageOutputStream targetImageStream) throws IORuntimeException {
|
||||
write(image, imageType, targetImageStream, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1760,6 +1761,39 @@ public class ImgUtil {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 {@link ImageInputStream} 获取对应类型的宽和高
|
||||
*
|
||||
* @param imageStream {@link ImageInputStream}
|
||||
* @param type 图片类型
|
||||
* @return 宽和高
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static Pair<Integer, Integer> getWidthAndHeight(final InputStream imageStream, final String type) {
|
||||
return getWidthAndHeight(getImageInputStream(imageStream), type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 {@link ImageInputStream} 获取对应类型的宽和高
|
||||
*
|
||||
* @param imageStream {@link ImageInputStream}
|
||||
* @param type 图片类型
|
||||
* @return 宽和高
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static Pair<Integer, Integer> getWidthAndHeight(final ImageInputStream imageStream, final String type) {
|
||||
final ImageReader reader = getReader(type);
|
||||
if (null != reader) {
|
||||
reader.setInput(imageStream, true);
|
||||
try {
|
||||
return Pair.of(reader.getWidth(0), reader.getHeight(0));
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ----- getImage and getPoint
|
||||
@@ -2008,4 +2042,16 @@ public class ImgUtil {
|
||||
new FilteredImageSource(image.getSource(), filter));
|
||||
}
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 刷新和释放{@link Image} 资源
|
||||
*
|
||||
* @param image {@link Image}
|
||||
* @since 5.8.27
|
||||
*/
|
||||
public static void flush(final Image image) {
|
||||
if (null != image) {
|
||||
image.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user