java 转换tif图片为jpg,解决转换后颜色异常问题
说明
转换代码
import cn.hutool.core.img.Img;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ImageUtils {
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(ImageUtils.class);
public static boolean checkImageType(byte[] b) {
try {
if((b[0] & 0xFF) == 0x49 && (b[1] & 0xFF)==0x49 && (b[2] & 0xFF)==0x2A){
return true;
}else {
return false;
}
}catch(Exception e) {
e.printStackTrace();
return false;
}
}
public static ByteArrayOutputStream tiffToJpg(InputStream in) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
BufferedImage bufferegImage= ImageIO.read(in);
Img.from(bufferegImage).write(out);
}catch(IOException e) {
e.printStackTrace();
}finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
return out;
}
}
参考文章: