要用到一个图片生成的代码,网上找了下,有一个C#版本的,不是自己想要的,不过他描述的逻辑是和想的。最后自己考虑用两张图片合并后得到一个规定大小的图片。
无论你现在上传什么样的图片,首先我们将底图固定(比如为110*110),然后你的图片可能是1000*800,或是200*300,都将第二张图片向110*110图片中写入,根据比例计算好位置,这样就保证了上传的图片最后不会被截图并且生成了一张适合前端显示的图片。
看代码吧,有用的人就拿去,效率上还有待改进:
<pre class="java" name="code">
/**
* 生成一张指定大小和路径和透明背景图片
* @param transparentWidth
* @param transparentHeight
* @param toPath
* @throws IOException
*/
private synchronized static void drawTransparent(int transparentWidth,
int transparentHeight, String toPath) throws IOException {
int width = transparentWidth;
int height = transparentHeight;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
image = g2d.getDeviceConfiguration().createCompatibleImage(width,
height, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
g2d.setStroke(new BasicStroke(1));
// 释放对象
g2d.dispose();
// 保存文件
ImageIO.write(image, "png", new File(toPath));
}
/**
* 生成缩略图-指定长或者宽的最大值来压缩图片
* @param transparentPath:指定的画布大小,是一个指定大小的透明背景图片
* @param srcImgPath:源图片路径
* @param outImgPath :输出的压缩图片的路径
* @param maxLength :长或者宽的最大值
* @throws IOException
*/
public synchronized static void createThumbnailImg(int transparentWidth,int transparentheight,String transparentPath,String srcImgPath, String outImgPath,
int maxLength) throws IOException {
String transPath=transparentPath +File.separator+transparentWidth+"_"+transparentheight+".png";
//如果透明背景图存在则不处理
if(!(new File(transPath).exists()))
{
drawTransparent(transparentWidth,transparentheight,transPath);
}
BufferedImage src = null;
InputStream is = null;
InputStream is2 = null;
//OutputStream os = null;
BufferedImage transparentImage = null;
try {
//读入底图
is = new FileInputStream(transPath);
//读入源图片
is2 = new FileInputStream(srcImgPath);
//缓存
transparentImage=javax.imageio.ImageIO.read(is);
src = javax.imageio.ImageIO.read(is2);
//源图片不空才处理
if (null != src)
{
// 得到源图宽
int old_w = src.getWidth();
// 得到源图长
int old_h = src.getHeight();
// 新图的宽
int new_w = 0;
// 新图的长
int new_h = 0;
// 根据图片尺寸压缩比得到新图的尺寸
//如果是宽比高大,则以宽为目标对象进行缩放
//即宽值固定,高度缩放的比例=maxLength/源图宽
//maxLength大,则为放大,maxLength小则为缩小
if (old_w > old_h) {
// 图片要缩放的比例
new_w = maxLength;
new_h = (int) Math.round(old_h
* ((float) maxLength / old_w));
} else {
new_w = (int) Math.round(old_w
* ((float) maxLength / old_h));
new_h = maxLength;
}
// 生成新图
BufferedImage newImg = null;
// 判断输入图片的类型
System.out.print("src.getType():" + src.getType());
switch (src.getType()) {
case 13:
// png,gif
newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_4BYTE_ABGR);
break;
default:
newImg = new BufferedImage(new_w, new_h,
BufferedImage.TYPE_INT_RGB);
break;
}
Graphics2D g2d = newImg.createGraphics();
g2d.getDeviceConfiguration().createCompatibleImage(new_w,
new_h, Transparency.TRANSLUCENT);
// 从原图上取颜色绘制新图
g2d.drawImage(src, 0, 0, old_w, old_h, null);
g2d.dispose();
// 根据图片尺寸压缩比得到新图的尺寸
newImg.getGraphics()
.drawImage(
src.getScaledInstance(new_w, new_h,
Image.SCALE_SMOOTH), 0, 0, null);
//========================到此处新图处理完成=============================
//========================现在开始做底图与新图的合并=====================
// 底图
BufferedImage image = transparentImage;
// 要添加的图片
BufferedImage image2 = newImg;
// 得到底图当画布
Graphics g = image.getGraphics();
// 开始绘制
// 绘入的是要添加的图,具体添加的位置是
// 底图宽取2分之1减去要增的图宽取2分之1处为X坐标
// 底图长取2分之1减去要增的图长取2分之1处为Y坐标
// ---------------
// | __________ |
// | | | |
// | | | |
// | |_________| |
// | |
// ---------------
// 在组件区域的左上角(image.getWidth()/2-image2.getWidth()/2,image.getHeight()/2-image2.getHeight()/2)以原始大小显示一个图像
g.drawImage(image2, image.getWidth() / 2 - image2.getWidth()/ 2,
image.getHeight() / 2 - image2.getHeight() / 2,
null);
// 输出
File file = new File(outImgPath);
if (!file.exists()) {
file.mkdirs();
}
//if (!file.getParentFile().exists()) {
// file.getParentFile().mkdir();
//}
// 输出到文件流
System.out
.println("outImgPath"
+ outImgPath.substring(outImgPath
.lastIndexOf(".") + 1));
ImageIO.write(image, "png", new File(outImgPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
is.close();
}
if (is2 != null) {
is2.close();
}
}
}