📄 senderuseragent.java
字号:
/*
* Created on 2005-3-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.rainbow.mms.gateway;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.AreaAveragingScaleFilter;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import com.gif4j.GifDecoder;
import com.gif4j.GifFrame;
import com.gif4j.*;
//import com.gif4j.i;
import com.jhlabs.ie.Pixmap;
import com.rainbow.mms.common.UserMobileAgent;
import com.rainbow.util.tools.HibernateUtil;
import com.sun.jimi.core.decoder.gif.GIFDecoder;
/**
* 根据手机型号完成图片格式的适配转换,如果适配库中没有该用户的手机型号信息,
* 那么图片尺寸就按照128*128计算,图片格式就按照支持GIF,最佳为JPEG格式算。
* 支持的图片尺寸和格式转换的格式有:jpg, png, gif, bmp, tif, wbmp
* @author Rainbow MMS Group Leader —— TrWorks
*/
class SenderUserAgent {
public static Logger log = Logger.getLogger(SenderUserAgent.class);
/**
* 根据手机型号完成图片格式的适配转换
* @param in 输入的图片流
* @param inType 输入的图片格式
* @param mobile 要适配的手机
* @return 转换结果
*/
public static boolean doUserAgent(InputStream in, String inType, String mobile,
StringBuffer newPicType, ByteArrayOutputStream out){
String srcPicType = null;
boolean beConvert = false;
if (in == null ||
inType.substring(0, 3).compareToIgnoreCase("ima") != 0 ||
mobile.compareTo("") == 0){
return beConvert;
}
// 将输入的inType从MIME类型标示,转义成srcPicType的文件类型标示
// 图片支持的格式有:jpg, png, gif, bmp, tif, wbmp
if (0 == inType.compareToIgnoreCase("image/jpg") ||
0 == inType.compareToIgnoreCase("image/jpeg") ||
0 == inType.compareToIgnoreCase("image/pjpeg")){
srcPicType = "jpeg";
}
else if (0 == inType.compareToIgnoreCase("image/png")){
srcPicType = "png";
}
else if (0 == inType.compareToIgnoreCase("image/gif")){
srcPicType = "gif";
}
else if (0 == inType.compareToIgnoreCase("image/bmp")){
srcPicType = "bmp";
}
else if (0 == inType.compareToIgnoreCase("image/tif")){
srcPicType = "tif";
}
else if (0 == inType.compareToIgnoreCase("image/tiff")){
srcPicType = "tiff";
}
else if (0 == inType.compareToIgnoreCase("image/vnd.wap.wbmp")){
srcPicType = "wbmp";
}
else{
return false;
}
// 默认的尺寸和最佳静态图片格式
int picWidth = 128;
int picHeight = 128;
int picSupportGIF = 1;
String picType = "jpeg";
// 从数据库中检索该手机号码的手机型号的适配信息
try {
Session sess = HibernateUtil.currentSession();
List list = sess.createCriteria(UserMobileAgent.class).
add(Restrictions.eq("mobile", mobile)).list();
UserMobileAgent userAgent = null;
if (list.get(0) != null){
userAgent = (UserMobileAgent)list.get(0);
}
if (userAgent != null) {
picType = userAgent.getAgent().getPicType();
picWidth = userAgent.getAgent().getPicWidth();
picHeight = userAgent.getAgent().getPicHeight();
picSupportGIF = userAgent.getAgent().getSupportGif();
}
} catch (Exception e) {
;
}
BufferedImage srcbufImage = null;
GifImage gifImage = null;
try {
// 不是GIF
if (srcPicType.compareToIgnoreCase("gif") != 0){
srcbufImage = ImageIO.read(in);
BufferedImage dstbufImage = srcbufImage;
int height = srcbufImage.getHeight();
int width = srcbufImage.getWidth();
boolean needZoom = false;
// 当图像的实际尺寸超过手机型号的允许尺寸时就需要缩放,并且按照长度或高度最小的值取.
if (picHeight < height){
needZoom = true;
beConvert = true;
height = picHeight;
}
if (picWidth < width){
needZoom = true;
beConvert = true;
width = picWidth;
}
if (srcbufImage != null){
log.info("因手机型号原因,特将静态图像缩小, orgWidth=" + srcbufImage.getWidth() +
",orgHeight=" + srcbufImage.getHeight() + "; dstWidth=" +
width +";dstHeight=" + height);
dstbufImage = zoom(srcbufImage, width, height);
}
// 输入的静态图不是该手机型号最佳的静态图片格式,那么就转换成最佳格式
if ((picType.compareToIgnoreCase(srcPicType) != 0)){
beConvert = true;
log.info("因手机型号原因,特将静态图像类型转换, srcType=" + srcPicType +", dstType=" + picType);
// 转换图像格式
if (false == ImageIO.write(dstbufImage, picType, out)){
log.error("图像转换失败");
return false;
}
// 设定新的图片MIME类型
if (picType.compareToIgnoreCase("jpeg") == 0 ||
picType.compareToIgnoreCase("jpg") == 0){
newPicType.append("image/jpeg");
}
else if (picType.compareToIgnoreCase("png") == 0){
newPicType.append("image/png");
}
else if (picType.compareToIgnoreCase("bmp") == 0){
newPicType.append("image/bmp");
}
else if (picType.compareToIgnoreCase("gif") == 0){
newPicType.append("image/gif");
}
else if (picType.compareToIgnoreCase("tif") == 0){
newPicType.append("image/tif");
}
else if (picType.compareToIgnoreCase("tiff") == 0){
newPicType.append("image/tiff");
}
else if (picType.compareToIgnoreCase("wbmp") == 0){
newPicType.append("image/vnd.wap.wbmp");
}
else{
return false;
}
}
else {
// 不需要格式转换,但是如果大小变化了
newPicType.append(inType);
if (false == ImageIO.write(dstbufImage, srcPicType, out)) {
log.error("图像转换失败");
return false;
}
}
}
else{ // GIF
newPicType.append("image/gif");
gifImage = GifDecoder.decode(in);
int height = gifImage.getCurrentLogicHeight();
int width = gifImage.getCurrentLogicWidth();
int frameNum = gifImage.getNumberOfFrames();
boolean needZoom = false;
// 当图像的实际尺寸超过手机型号的允许尺寸时就需要缩放,并且按照长度或高度最小的值取.
if (picHeight < height){
needZoom = true;
beConvert = true;
height = picHeight;
}
if (picWidth < width){
needZoom = true;
beConvert = true;
width = picWidth;
}
if (needZoom == true){
GifImage resultGif = new GifImage();
for (int i = 0; i < frameNum; i++){
BufferedImage frameImage = gifImage.getFrame(i).getAsBufferedImage();
frameImage = zoom(frameImage, width, height);
resultGif.addGifFrame(new GifFrame(frameImage));
}
gifImage = resultGif;
}
// 如果终端不支持GIF,那么需要将GIF的第一桢转化为该终端支持的最佳静态格式图片
if (picSupportGIF == 0){
BufferedImage firstImage = gifImage.getFrame(0).getAsBufferedImage();
// 转换图像格式
if (false == ImageIO.write(firstImage, picType, out)){
log.error("图像转换失败");
return false;
}
}
else{
GifEncoder.encode(gifImage, out);
}
}
}
catch (IOException e1) {
e1.printStackTrace();
log.error("图像转换失败");
return false;
}
return beConvert;
}
/**
* 缩放图像
* @param srcBufImg 源图像
* @param width 目的宽
* @param height 目的高
* @return 目的图像
*/
private static BufferedImage zoom(BufferedImage srcBufImg, int width, int height) {
ImageProducer imp = srcBufImg.getSource();
Pixmap pixmap1 = new Pixmap(imp);
ImageFilter imagefilter = new AreaAveragingScaleFilter(width, height);
FilteredImageSource fitImgSrc = new FilteredImageSource(pixmap1
.getSource(), imagefilter);
Pixmap pixmap2 = new Pixmap(fitImgSrc);
Toolkit kit = Toolkit.getDefaultToolkit();
Image dstImg = kit.createImage(pixmap2.getSource());
BufferedImage bufferImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
bufferImage.getGraphics().drawImage(dstImg, 0, 0, width, height, null);
return bufferImage;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -