主题 : 问了多次没反应,我只好自己把水印的bug修复了需要的留邮箱
级别: 贡士
UID: 5287
积分:723 加为好友
威望: 0 精华: 0
主题:95 回复:171
注册时间:2010-04-05
在线时长:0
1#   发表于:2011-11-28 08:27:32  IP:114.247.*.*
修改的类如下~~for 311版


欢迎你
级别: 贡士
UID: 5287
积分:723 加为好友
威望: 0 精华: 0
主题:95 回复:171
注册时间:2010-04-05
在线时长:0
2#   发表于:2011-11-28 08:27:57  IP:114.247.*.*
package com.jeecms.cms.action.directive;

import static com.jeecms.common.web.freemarker.DirectiveUtils.OUT_BEAN;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.jeecms.cms.entity.main.CmsSite;
import com.jeecms.cms.entity.main.Content;
import com.jeecms.cms.manager.main.ContentMng;
import com.jeecms.cms.web.FrontUtils;
import com.jeecms.common.web.freemarker.DirectiveUtils;
import com.jeecms.common.web.freemarker.ParamsRequiredException;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 内容对象标签
 * 
 * @author liufang
 * 
 */
public class ContentDirective implements TemplateDirectiveModel {
/**
 * 输入参数,栏目ID。
 */
public static final String PARAM_ID = "id";
/**
 * 输入参数,下一篇。
 */
public static final String PRAMA_NEXT = "next";
/**
 * 输入参数,栏目ID。
 */
public static final String PARAM_CHANNEL_ID = "channelId";

@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {



System.out.println("execute===="+params);


Integer id = getId(params);
Boolean next = DirectiveUtils.getBool(PRAMA_NEXT, params);
Content content;
if (next == null) {
content = contentMng.findById(id);
} else {
CmsSite site = FrontUtils.getSite(env);
Integer channelId = DirectiveUtils.getInt(PARAM_CHANNEL_ID, params);
content = contentMng.getSide(id, site.getId(), channelId, next);
}

Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
params);
paramWrap.put(OUT_BEAN, DEFAULT_WRAPPER.wrap(content));



Map<String, TemplateModel> origMap = DirectiveUtils
.addParamsToVariable(env, paramWrap);



System.out.println("execute==origMap=="+origMap);
System.out.println("execute==paramWrap=="+paramWrap);

body.render(env.getOut());
DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
}

private Integer getId(Map<String, TemplateModel> params)
throws TemplateException {
Integer id = DirectiveUtils.getInt(PARAM_ID, params);
if (id != null) {
return id;
} else {
throw new ParamsRequiredException(PARAM_ID);
}
}

@Autowired
private ContentMng contentMng;
}
欢迎你
级别: 贡士
UID: 5287
积分:723 加为好友
威望: 0 精华: 0
主题:95 回复:171
注册时间:2010-04-05
在线时长:0
3#   发表于:2011-11-28 08:28:11  IP:114.247.*.*
package com.jeecms.common.image;

import java.io.InputStream;
import java.util.Locale;

/**
 * 图片辅助类
 * 
 * @author liufang
 * 
 */
public abstract class ImageUtils {
/**
 * 图片的后缀
 */
public static final String[] IMAGE_EXT = new String[] { "jpg", "jpeg",
"gif", "png", "bmp" };

/**
 * 是否是图片
 * 
 * @param ext
 * @return "jpg", "jpeg", "gif", "png", "bmp" 为文件后缀名者为图片
 */
public static boolean isValidImageExt(String ext) {
ext = ext.toLowerCase(Locale.ENGLISH);
for (String s : IMAGE_EXT) {
if (s.equalsIgnoreCase(ext)) {
return true;
}
}
return false;
}

/**
 * Checks if the underlying input stream contains an image.
 * 
 * @param in
 *            input stream of an image
 * @return <code>true</code> if the underlying input stream contains an
 *         image, else <code>false</code>
 */
public static boolean isImage(final InputStream in) {
ImageInfo ii = new ImageInfo();
ii.setInput(in);
return ii.check();
}

/**
 * 获得水印位置
 * 
 * @param width
 *            原图宽度
 * @param height
 *            原图高度
 * @param p
 *            水印位置 1-5,其他值为随机。1:左上;2:右上;3:左下;4:右下;5:中央。
 * @param offsetx
 *            水平偏移。
 * @param offsety
 *            垂直偏移。
 * @return 水印位置 Rick.........
 */
public static Position markPosition(int width, int height, int p,
int offsetx, int offsety) {

if (p < 1 || p > 5) {
p = (int) (Math.random() * 5) + 1;
}
int x, y;
switch (p) {
// 左上
case 1:
x = 0;
y = 0;
break;
// 右上
case 2:
x = width - offsetx;
y = 0;
break;
// 左下
case 3:
x = 0;
y = height - offsety;
break;
// 右下
case 4:
x = width - offsetx;
y = height - offsety;
break;
// 中央
case 5:
x = ((width - offsetx) / 2);
y = (height / 2);
break;
default:
throw new RuntimeException("never reach ...");
}

System.out.println("Rick.width:" + width);
System.out.println("Rick.height:" + height);


System.out.println("Rick.offsetx:" + offsetx);
System.out.println("Rick.offsety:" + offsety);

System.out.println("Rick.x:" + x);
System.out.println("Rick.y:" + y);

return new Position(x, y);
}

/**
 * 水印位置
 * 
 * 包含左边偏移量,右边偏移量。
 * 
 * @author liufang
 * 
 */
public static class Position {
private int x;

private int y;

public Position(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}
}

}
欢迎你
级别: 贡士
UID: 5287
积分:723 加为好友
威望: 0 精华: 0
主题:95 回复:171
注册时间:2010-04-05
在线时长:0
4#   发表于:2011-11-28 08:28:35  IP:114.247.*.*
package com.jeecms.common.image;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;

import com.jeecms.common.image.ImageUtils.Position;

/**
 * 图片缩小类。使用方型区域颜色平均算法
 * 
 * @author liufang
 * 
 */
public class AverageImageScale {
/**
 * 缩小图片
 * 
 * @param srcFile
 *            原图片
 * @param destFile
 *            目标图片
 * @param boxWidth
 *            缩略图最大宽度
 * @param boxHeight
 *            缩略图最大高度
 * @throws IOException
 */
public static void resizeFix(File srcFile, File destFile, int boxWidth,
int boxHeight) throws IOException {
BufferedImage srcImgBuff = ImageIO.read(srcFile);
int width = srcImgBuff.getWidth();
int height = srcImgBuff.getHeight();
if (width <= boxWidth && height <= boxHeight) {
FileUtils.copyFile(srcFile, destFile);
return;
}
int zoomWidth;
int zoomHeight;
if ((float) width / height > (float) boxWidth / boxHeight) {
zoomWidth = boxWidth;
zoomHeight = Math.round((float) boxWidth * height / width);
} else {
zoomWidth = Math.round((float) boxHeight * width / height);
zoomHeight = boxHeight;
}
BufferedImage imgBuff = scaleImage(srcImgBuff, width, height,
zoomWidth, zoomHeight);
writeFile(imgBuff, destFile);
}

/**
 * 裁剪并压缩
 * 
 * @param srcFile
 *            原文件
 * @param destFile
 *            目标文件
 * @param boxWidth
 *            缩略图最大宽度
 * @param boxHeight
 *            缩略图最大高度
 * @param cutTop
 *            裁剪TOP
 * @param cutLeft
 *            裁剪LEFT
 * @param cutWidth
 *            裁剪宽度
 * @param catHeight
 *            裁剪高度
 * @throws IOException
 */
public static void resizeFix(File srcFile, File destFile, int boxWidth,
int boxHeight, int cutTop, int cutLeft, int cutWidth, int catHeight)
throws IOException {
BufferedImage srcImgBuff = ImageIO.read(srcFile);
srcImgBuff = srcImgBuff.getSubimage(cutTop, cutLeft, cutWidth,
catHeight);
int width = srcImgBuff.getWidth();
int height = srcImgBuff.getHeight();
if (width <= boxWidth && height <= boxHeight) {
writeFile(srcImgBuff, destFile);
return;
}
int zoomWidth;
int zoomHeight;
if ((float) width / height > (float) boxWidth / boxHeight) {
zoomWidth = boxWidth;
zoomHeight = Math.round((float) boxWidth * height / width);
} else {
zoomWidth = Math.round((float) boxHeight * width / height);
zoomHeight = boxHeight;
}
BufferedImage imgBuff = scaleImage(srcImgBuff, width, height,
zoomWidth, zoomHeight);
writeFile(imgBuff, destFile);
}

public static void writeFile(BufferedImage imgBuf, File destFile)
throws IOException {
File parent = destFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
ImageIO.write(imgBuf, "jpeg", destFile);
}

/**
 * 添加文字水印
 * 
 * @param srcFile
 *            源图片文件。需要加水印的图片文件。
 * @param destFile
 *            目标图片。加水印后保存的文件。如果和源图片文件一致,则覆盖源图片文件。
 * @param minWidth
 *            需要加水印的最小宽度,如果源图片宽度小于该宽度,则不加水印。
 * @param minHeight
 *            需要加水印的最小高度,如果源图片高度小于该高度,则不加水印。
 * @param pos
 *            加水印的位置。
 * @param offsetX
 *            加水印的位置的偏移量x。
 * @param offsetY
 *            加水印的位置的偏移量y。
 * @param text
 *            水印文字
 * @param color
 *            水印颜色
 * @param size
 *            水印字体大小
 * @param alpha
 *            透明度
 * @throws IOException
 */
public static void imageMark(File srcFile, File destFile, int minWidth,
int minHeight, int pos, int offsetX, int offsetY, String text,
Color color, int size, int alpha) throws IOException {

BufferedImage imgBuff = ImageIO.read(srcFile);
int width = imgBuff.getWidth();
int height = imgBuff.getHeight();
if (width <= minWidth || height <= minHeight) {
imgBuff = null;
if (!srcFile.equals(destFile)) {
FileUtils.copyFile(srcFile, destFile);
}
} else {
imageMark(imgBuff, width, height, pos, offsetX, offsetY, text,
color, size, alpha);
writeFile(imgBuff, destFile);
imgBuff = null;
}
}

/**
 * 添加图片水印
 * 
 * @param srcFile
 *            源图片文件。需要加水印的图片文件。
 * @param destFile
 *            目标图片。加水印后保存的文件。如果和源图片文件一致,则覆盖源图片文件。
 * @param minWidth
 *            需要加水印的最小宽度,如果源图片宽度小于该宽度,则不加水印。
 * @param minHeight
 *            需要加水印的最小高度,如果源图片高度小于该高度,则不加水印。
 * @param pos
 *            加水印的位置。
 * @param offsetX
 *            加水印的位置的偏移量x。
 * @param offsetY
 *            加水印的位置的偏移量y。
 * @param markFile
 *            水印图片
 * @throws IOException
 */
public static void imageMark(File srcFile, File destFile, int minWidth,
int minHeight, int pos, int offsetX, int offsetY, File markFile)
throws IOException {

BufferedImage imgBuff = ImageIO.read(srcFile);
int width = imgBuff.getWidth();
int height = imgBuff.getHeight();
if (width <= minWidth || height <= minHeight) {
imgBuff = null;
if (!srcFile.equals(destFile)) {
FileUtils.copyFile(srcFile, destFile);
}
} else {

int[] pic = getPicWH(markFile.getAbsolutePath());

System.out.println("markFile.getAbsolutePath()=="
+ markFile.getAbsolutePath());
System.out.println("width==" + width);
System.out.println("height==" + height);
System.out.println("pos==" + pos);

System.out.println("offsetX==" + offsetX);
System.out.println("offsetY==" + offsetY);



System.out.println("pic[0]==" + pic[0]);
System.out.println("pic[1]==" + pic[1]);

imageMark(imgBuff, width, height, pos,  pic[0],pic[1], markFile);






// imageMark(imgBuff, width, height, pos, offsetX, offsetY, markFile);














writeFile(imgBuff, destFile);
imgBuff = null;
}

}

/**
 * 添加文字水印
 * 
 * @param imgBuff
 *            原图片
 * @param width
 *            原图宽度
 * @param height
 *            原图高度
 * @param pos
 *            位置。1:左上;2:右上;3左下;4右下;5:中央;0或其他:随机。
 * @param offsetX
 *            水平偏移量。
 * @param offsetY
 *            垂直偏移量。
 * @param text
 *            水印内容
 * @param color
 *            水印颜色
 * @param size
 *            文字大小
 * @param alpha
 *            透明度。0-100。越小越透明。
 * @throws IOException
 */
private static void imageMark(BufferedImage imgBuff, int width, int height,
int pos, int offsetX, int offsetY, String text, Color color,
int size, int alpha) throws IOException {

Position p = ImageUtils.markPosition(width, height, pos, offsetX,
offsetY);
Graphics2D g = imgBuff.createGraphics();
g.setColor(color);
g.setFont(new Font(null, Font.PLAIN, size));
AlphaComposite a = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
(float) alpha / 100);
g.setComposite(a);
g.drawString(text, p.getX(), p.getY());

g.dispose();
}

private static void imageMark(BufferedImage imgBuff, int width, int height,
int pos, int offsetX, int offsetY, File markFile)
throws IOException {
Position p = ImageUtils.markPosition(width, height, pos, offsetX,
offsetY);
Graphics2D g = imgBuff.createGraphics();
AlphaComposite a = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP);
g.setComposite(a);
g.drawImage(ImageIO.read(markFile), p.getX(), p.getY(), null);
g.dispose();
}

private static BufferedImage scaleImage(BufferedImage srcImgBuff,
int width, int height, int zoomWidth, int zoomHeight) {
int[] colorArray = srcImgBuff.getRGB(0, 0, width, height, null, 0,
width);
BufferedImage outBuff = new BufferedImage(zoomWidth, zoomHeight,
BufferedImage.TYPE_INT_RGB);
// 宽缩小的倍数
float wScale = (float) width / zoomWidth;
int wScaleInt = (int) (wScale + 0.5);
// 高缩小的倍数
float hScale = (float) height / zoomHeight;
int hScaleInt = (int) (hScale + 0.5);
int area = wScaleInt * hScaleInt;
int x0, x1, y0, y1;
int color;
long red, green, blue;
int x, y, i, j;
for (y = 0; y < zoomHeight; y++) {
// 得到原图高的Y坐标
y0 = (int) (y * hScale);
y1 = y0 + hScaleInt;
for (x = 0; x < zoomWidth; x++) {
x0 = (int) (x * wScale);
x1 = x0 + wScaleInt;
red = green = blue = 0;
for (i = x0; i < x1; i++) {
for (j = y0; j < y1; j++) {
color = colorArray[width * j + i];
red += getRedValue(color);
green += getGreenValue(color);
blue += getBlueValue(color);
}
}
outBuff.setRGB(x, y, comRGB((int) (red / area),
(int) (green / area), (int) (blue / area)));
}
}
return outBuff;
}

private static int getRedValue(int rgbValue) {
return (rgbValue & 0x00ff0000) >> 16;
}

private static int getGreenValue(int rgbValue) {
return (rgbValue & 0x0000ff00) >> 8;
}

private static int getBlueValue(int rgbValue) {
return rgbValue & 0x000000ff;
}

private static int comRGB(int redValue, int greenValue, int blueValue) {
return (redValue << 16) + (greenValue << 8) + blueValue;
}

// public static void imageMark(File srcFile, File destFile, int minWidth,
// int minHeight, int pos, int offsetX, int offsetY, File markFile)

public static int[] getPicWH(String path) {

int reValue[] = new int[2];
File picFile = new File(path); // 图片文件路径
BufferedImage image = null;
try {
image = ImageIO.read(picFile);
} catch (IOException e) {
e.printStackTrace();
}
int width = image.getWidth();// 图片宽度
int height = image.getHeight();// 图片高度

reValue[0] = width;
reValue[1] = height;
return reValue;

}

public static void main(String[] args) throws Exception {
long time = System.currentTimeMillis();
File srcFile = new File("c:/a.jpg");

File destFile = new File("c:/aaa.jpg");

File markFile = new File("c:/b.png");

int[] pic = getPicWH("c:/b.png");

AverageImageScale.imageMark(srcFile, destFile, 100, 100, 4, pic[0],
pic[1], markFile);
time = System.currentTimeMillis() - time;
System.out.println("resize2 img in " + time + "ms");
}
}
欢迎你
级别: 贡士
UID: 5287
积分:723 加为好友
威望: 0 精华: 0
主题:95 回复:171
注册时间:2010-04-05
在线时长:0
5#   发表于:2011-11-28 08:29:00  IP:114.247.*.*
package com.jeecms.common.image;

import java.io.InputStream;
import java.util.Locale;

/**
 * 图片辅助类
 * 
 * @author liufang
 * 
 */
public abstract class ImageUtils {
/**
 * 图片的后缀
 */
public static final String[] IMAGE_EXT = new String[] { "jpg", "jpeg",
"gif", "png", "bmp" };

/**
 * 是否是图片
 * 
 * @param ext
 * @return "jpg", "jpeg", "gif", "png", "bmp" 为文件后缀名者为图片
 */
public static boolean isValidImageExt(String ext) {
ext = ext.toLowerCase(Locale.ENGLISH);
for (String s : IMAGE_EXT) {
if (s.equalsIgnoreCase(ext)) {
return true;
}
}
return false;
}

/**
 * Checks if the underlying input stream contains an image.
 * 
 * @param in
 *            input stream of an image
 * @return <code>true</code> if the underlying input stream contains an
 *         image, else <code>false</code>
 */
public static boolean isImage(final InputStream in) {
ImageInfo ii = new ImageInfo();
ii.setInput(in);
return ii.check();
}

/**
 * 获得水印位置
 * 
 * @param width
 *            原图宽度
 * @param height
 *            原图高度
 * @param p
 *            水印位置 1-5,其他值为随机。1:左上;2:右上;3:左下;4:右下;5:中央。
 * @param offsetx
 *            水平偏移。
 * @param offsety
 *            垂直偏移。
 * @return 水印位置 Rick.........
 */
public static Position markPosition(int width, int height, int p,
int offsetx, int offsety) {

if (p < 1 || p > 5) {
p = (int) (Math.random() * 5) + 1;
}
int x, y;
switch (p) {
// 左上
case 1:
x = 0;
y = 0;
break;
// 右上
case 2:
x = width - offsetx;
y = 0;
break;
// 左下
case 3:
x = 0;
y = height - offsety;
break;
// 右下
case 4:
x = width - offsetx;
y = height - offsety;
break;
// 中央
case 5:
x = ((width - offsetx) / 2);
y = (height / 2);
break;
default:
throw new RuntimeException("never reach ...");
}

System.out.println("Rick.width:" + width);
System.out.println("Rick.height:" + height);


System.out.println("Rick.offsetx:" + offsetx);
System.out.println("Rick.offsety:" + offsety);

System.out.println("Rick.x:" + x);
System.out.println("Rick.y:" + y);

return new Position(x, y);
}

/**
 * 水印位置
 * 
 * 包含左边偏移量,右边偏移量。
 * 
 * @author liufang
 * 
 */
public static class Position {
private int x;

private int y;

public Position(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}
}

}
欢迎你
级别: 贡士
UID: 5287
积分:723 加为好友
威望: 0 精华: 0
主题:95 回复:171
注册时间:2010-04-05
在线时长:0
6#   发表于:2011-11-28 08:29:58  IP:114.247.*.*
基本写错了~~~~
欢迎你
1 共1页