fix pink bug

This commit is contained in:
Looly 2021-09-10 19:07:08 +08:00
parent a0f407865e
commit ba4fabb88c
5 changed files with 60 additions and 12 deletions

View File

@ -3,10 +3,12 @@
-------------------------------------------------------------------------------------------------------------
# 5.7.13 (2021-09-09)
# 5.7.13 (2021-09-10)
### 🐣新特性
### 🐞Bug修复
* 【core 】 修复FuncKey函数无效问题
* 【core 】 修复ImgUtil.copyImage读取网络URL后宽高报错问题issue#1821@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -289,7 +289,7 @@ public class Img implements Serializable {
*
* @param width 缩放后的宽度
* @param height 缩放后的高度
* @param fixedColor 比例不对时补充的颜色不补充为<code>null</code>
* @param fixedColor 比例不对时补充的颜色不补充为{@code null}
* @return this
*/
public Img scale(int width, int height, Color fixedColor) {

View File

@ -22,10 +22,15 @@ import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
@ -1194,9 +1199,10 @@ public class ImgUtil {
if (imageType != bufferedImage.getType()) {
bufferedImage = copyImage(image, imageType);
}
} else {
bufferedImage = copyImage(image, imageType);
return bufferedImage;
}
bufferedImage = copyImage(image, imageType);
return bufferedImage;
}
@ -1247,7 +1253,12 @@ public class ImgUtil {
* @since 4.5.17
*/
public static BufferedImage copyImage(Image img, int imageType, Color backgroundColor) {
final BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), imageType);
// ensures that all the pixels loaded
// issue#1821@Github
img = new ImageIcon(img).getImage();
final BufferedImage bimage = new BufferedImage(
img.getWidth(null), img.getHeight(null), imageType);
final Graphics2D bGr = GraphicsUtil.createGraphics(bimage, backgroundColor);
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
@ -1255,6 +1266,22 @@ public class ImgUtil {
return bimage;
}
/**
* 创建与当前设备颜色模式兼容的 {@link BufferedImage}
*
* @param width 宽度
* @param height 高度
* @param transparency 透明模式 {@link java.awt.Transparency}
* @return {@link BufferedImage}
* @since 5.7.13
*/
public static BufferedImage createCompatibleImage(int width, int height, int transparency) throws HeadlessException {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
return gc.createCompatibleImage(width, height, transparency);
}
/**
* 将Base64编码的图像信息转为 {@link BufferedImage}
*
@ -2160,7 +2187,7 @@ public class ImgUtil {
* 可以使用灰度 (gray)
*
* @param colorSpace 颜色模式如灰度等
* @param image 被转换的图片
* @param image 被转换的图片
* @return 转换后的图片
* @since 5.7.8
*/
@ -2197,11 +2224,11 @@ public class ImgUtil {
* 图片滤镜借助 {@link ImageFilter}实现实现不同的图片滤镜
*
* @param filter 滤镜实现
* @param image 图片
* @param image 图片
* @return 滤镜后的图片
* @since 5.7.8
*/
public static Image filter(ImageFilter filter, Image image){
public static Image filter(ImageFilter filter, Image image) {
return Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(), filter));
}

View File

@ -14,7 +14,7 @@ import java.util.function.Function;
public class FuncKeyMap<K, V> extends CustomKeyMap<K, V> {
private static final long serialVersionUID = 1L;
private Function<Object, K> keyFunc;
private final Function<Object, K> keyFunc;
// ------------------------------------------------------------------------- Constructor start
@ -26,11 +26,12 @@ public class FuncKeyMap<K, V> extends CustomKeyMap<K, V> {
*/
public FuncKeyMap(Map<K, V> m, Function<Object, K> keyFunc) {
super(m);
this.keyFunc = keyFunc;
}
// ------------------------------------------------------------------------- Constructor end
/**
* 将Key转为驼峰风格如果key为字符串的话
* 根据函数自定义键
*
* @param key KEY
* @return 驼峰Key

View File

@ -1,12 +1,16 @@
package cn.hutool.core.img;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import org.junit.Ignore;
import org.junit.Test;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
public class ImgTest {
@ -50,7 +54,7 @@ public class ImgTest {
@Test
@Ignore
public void pressImgTest(){
public void pressImgTest() {
Img.from(FileUtil.file("d:/test/图片1.JPG"))
.pressImage(ImgUtil.read("d:/test/617180969474805871.jpg"), new Rectangle(0, 0, 800, 800), 1f)
.write(FileUtil.file("d:/test/pressImg_result.jpg"));
@ -58,9 +62,23 @@ public class ImgTest {
@Test
@Ignore
public void strokeTest(){
public void strokeTest() {
Img.from(FileUtil.file("d:/test/公章3.png"))
.stroke(null, 2f)
.write(FileUtil.file("d:/test/stroke_result.png"));
}
/**
* issue#I49FIU
*/
@Test
@Ignore
public void scaleTest() {
String downloadFile = "d:/test/1435859438434136064.JPG";
File file = FileUtil.file(downloadFile);
File fileScale = FileUtil.file(downloadFile + ".scale." + FileTypeUtil.getType(file));
Image img = ImgUtil.getImage(URLUtil.getURL(file));
ImgUtil.scale(img, fileScale, 0.8f);
}
}