导航:首页 > 编程语言 > java图片缩略图

java图片缩略图

发布时间:2025-03-06 18:45:46

1. java上传图片 生成缩略图,如果上传的图片尺寸比较小就压缩处理

//将图按比例缩小。
public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
//这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
//则将下面的if else语句注释即可
if(sx>sy)
{
sx = sy;
targetW = (int)(sx * source.getWidth());
}else{
sy = sx;
targetH = (int)(sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { //handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics();
//smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}

public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)
throws Exception {
BufferedImage srcImage;
// String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());
String imgType = "JPEG";
if (fromFileStr.toLowerCase().endsWith(".png")) {
imgType = "PNG";
}
// System.out.println(ex);
File saveFile=new File(saveToFileStr);
File fromFile=new File(fromFileStr);
srcImage = ImageIO.read(fromFile);
if(width > 0 || hight > 0)
{
srcImage = resize(srcImage, width, hight);
}
ImageIO.write(srcImage, imgType, saveFile);

}

public static void main (String argv[]) {
try{
//参数1(from),参数2(to),参数3(宽),参数4(高)
saveImageAsJpg("C:\\Documents and Settings\\xugang\\桌面\\tmr-06.jpg",
"C:\\Documents and Settings\\xugang\\桌面\\2.jpg",
120,120);
} catch(Exception e){
e.printStackTrace();
}

}

2. java类 图片导入到excel 模糊 就是图片被盖上了一层红色

Java上传图片时,对某些图片进行缩放、裁剪或者生成缩略图时会蒙上一层红色,经过检查只要经过ImageIO.read()方法读取后再保存,该图片便已经变成红图。因此,可以推测直接原因在于ImageIO.read()方法加载图片的过程存在问题。
public static BufferedImage getImages(byte[] data) throws IOException {
ByteArrayInputStream input = new ByteArrayInputStream(data);
return ImageIO.read(input);
}

经过查阅得知ImageIO.read()方法读取图片时可能存在不正确处理图片ICC信息的问题,ICC为JPEG图片格式中的一种头部信息,导致渲染图片前景色时蒙上一层红色。解决方案:
不再使用ImageIO.read()方法加载图片,而使用JDK中提供的Image src=Toolkit.getDefaultToolkit().getImage

Image src=Toolkit.getDefaultToolkit().getImage(file.getPath());
BufferedImage image=BufferedImageBuilder.toBufferedImage(src);//Image to BufferedImage

或者Toolkit.getDefaultToolkit().createImage
Image imageTookit = Toolkit.getDefaultToolkit().createImage(bytes);
BufferedImage cutImage = BufferedImageBuilder.toBufferedImage(imageTookit);

BufferedImageBuilder源码
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null),
image.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}

阅读全文

与java图片缩略图相关的资料

热点内容
这是命令吗txt电子书下载 浏览:940
adb命令导出媒体库 浏览:826
华为云服务器多少钱 浏览:366
连看世界app怎么用 浏览:35
ipad解压专家怎么解压qq邮箱文件 浏览:252
php712安装 浏览:448
python远程桌面控制 浏览:215
操作系统scan算法 浏览:11
服务器板块有什么龙头 浏览:74
我的世界服务器成员怎么开创造 浏览:660
程序员郑州买房哪个区好 浏览:204
程序员发怒 浏览:823
安卓机看视频怎么没有小窗口 浏览:456
minecraft服务器怎么布置 浏览:306
怎么把安卓的东西转到已激活苹果 浏览:852
停止服务doss命令 浏览:878
u盘占内存但该文件夹为空 浏览:612
服务器怎么更换重生点 浏览:34
收费api调用平台源码 浏览:648
安卓怎么自检病毒 浏览:560