Topic: 关于jpeg图片处理的问题

  Print this page

1.关于jpeg图片处理的问题 Copy to clipboard
Posted by: fat32
Posted on: 2003-11-07 13:16

小弟在作一个图片处理的程序,目的是将图片按比例缩小,下面是我在网上找的一个程序,在我的windows上运行正常:
package didibaba.util;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

// com.sun.image.codec.jpeg package is included in sun and ibm sdk 1.3
import com.sun.image.codec.jpeg.*;

/**
* Use this class to size images.
*
* @author Dan Becker
*/
public class ImageSizer {

public static final MediaTracker tracker = new MediaTracker(new Component() {
});

/**
* Adjusts the size of the image to the given coordinates.
* If width or height is -1, the image aspect ration is maintained.
*/
public static Image setSize(Image image, int width, int height) {
return setSize(image, width, height, java.awt.Image.SCALE_DEFAULT);
}

/**
* Adjusts the size of the image to the given coordinates.
* If width or height is -1, the image aspect ration is maintained.
* <p>
* Hints are one of SCALE_DEFAULT, SCALE_FAST, SCALE_SMOOTH,
* SCALE_REPLICATE, SCALE_AREA_AVERAGING as defined in java.awt.Image.
*/
public static Image setSize(Image image, int width, int height, int hints) {
return image.getScaledInstance(width, height, hints);
}

/**
* Checks the given image for valid width and height.
*
*/
public static void checkImage(Image image) {
waitForImage(image);
int imageWidth = image.getWidth(null);
if (imageWidth < 1)
throw new IllegalArgumentException(
"image width " + imageWidth + " is out of range");
int imageHeight = image.getHeight(null);
if (imageHeight < 1)
throw new IllegalArgumentException(
"image height " + imageHeight + " is out of range");
// System.out.println( "Image size=" + imageWidth + "x" + imageHeight );
}

/**
* Waits for given image to load. Use before querying image height/width/colors.
*/
public static void waitForImage(Image image) {
try {
tracker.addImage(image, 0);
tracker.waitForID(0);
// loadStatus = tracker.statusID( 0, false );
tracker.removeImage(image, 0);
} catch (InterruptedException e) {
e.printStackTrace();
}
} // waitForImage

/**
* Encodes the given image at the given quality to the output stream.
*/
public static void encodeJPEG(
OutputStream outputStream,
Image outputImage,
float outputQuality)
throws java.io.IOException {
int outputWidth = outputImage.getWidth(null);
if (outputWidth < 1)
throw new IllegalArgumentException(
"output image width " + outputWidth + " is out of range");
int outputHeight = outputImage.getHeight(null);
if (outputHeight < 1)
throw new IllegalArgumentException(
"output image height " + outputHeight + " is out of range");

// Get a buffered image from the image.
BufferedImage bi =
new BufferedImage(outputWidth, outputHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
biContext.drawImage(outputImage, 0, 0, null);
// Note that additional drawing such as watermarks or logos can be placed here.

// com.sun.image.codec.jpeg package is included in sun and ibm sdk 1.3
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
// The default quality is 0.75.
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(bi);
jep.setQuality(outputQuality, true);
encoder.encode(bi, jep);
// encoder.encode( bi );
outputStream.flush();
} // encodeImage

public static void resize(String inputFile,String outputFile,int outputWidth,int outputHeight,float outputQuality) {
//Get input image.
Image inputImage = Toolkit.getDefaultToolkit().getImage(inputFile);
checkImage(inputImage);

//Create output image.
Image outputImage =
ImageSizer.setSize(inputImage, outputWidth, outputHeight);
checkImage(outputImage);
try {
FileOutputStream fos = new FileOutputStream(outputFile);
encodeJPEG(fos, outputImage, outputQuality);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}
下面是我写的一个调用这个程序的servlet:
package didibaba.community.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import didibaba.util.ImageSizer;
/**
* @version 1.0
* @author
*/
public class TestImageConvert extends HttpServlet implements Servlet {

/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
performTask(req, resp);
}

/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
performTask(req, resp);
}

private void performTask(HttpServletRequest req, HttpServletResponse resp) {

try {
resp.setContentType("text/html;charset=gb2312");
PrintWriter out = resp.getWriter();

System.out.println("[jute.debug] :<<<<<<<<<<<<<<<<<<<<<<<<");
ImageSizer.resize(
"/tmp/DSC00503.JPG",
"/tmp/DSC00503.snap.JPG",
640,
480,
0.75f);
System.out.println("[jute.debug] : >>>>>>>>>>>>>>>>>>>>>>>>");

out.println("<h1>转换成功!");
out.close();
} catch (Exception e) {
e.printStackTrace();
}

}
}

这些程序在windows上运行正常,但拿到linux或者aix上就报错了,错误信息如下:
Error 500
An error has occured while processing request: http://10.108.200.211/error.jsp
Message: sun/awt/motif/MToolkit
StackTrace: javax.servlet.ServletException: sun/awt/motif/MToolkit at com.ibm.servlet.engine.webapp.StrictServletInstance.doService(ServletManager.java:848) at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(StrictLifecycleServlet.java:167) at com.ibm.servlet.engine.webapp.IdleServletState.service(StrictLifecycleServlet.java:297) at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(StrictLifecycleServlet.java:110) at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java:472) at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletManager.java:1012) at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(ServletManager.java:913) at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:721) at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:374) at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:118) at com.ibm.servlet.engine.srt.WebAppInvoker.doForward(WebAppInvoker.java:134) at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:239) at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:67) at com.ibm.servlet.engine.invocation.CacheableInvocationContext.invoke(CacheableInvocationContext.java:106) at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:154) at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(OSEListener.java:315) at com.ibm.servlet.engine.http11.HttpConnection.handleRequest(HttpConnection.java:60) at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:323) at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:252) at com.ibm.ws.util.CachedThread.run(ThreadPool.java:138)

哪位DX知道是怎么回事啊,帮帮小弟吧,我已经快被逼疯了!
我用的是IBM WebSphere 4.0.5

2.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: fat32
Posted on: 2003-11-07 14:16

我又找了一段程序,在windows上也可以正常运行,但在linux和aix上仍然报错,不过错误和上面不一样了。错误信息如下:
Error 500
An error has occured while processing request: http://10.1.1.250/error.jsp
Message: class sun.awt.image.codec.JPEGParam does not implement interface com.sun.image.codec.jpeg.JPEGEncodeParam
StackTrace: javax.servlet.ServletException: class sun.awt.image.codec.JPEGParam does not implement interface com.sun.image.codec.jpeg.JPEGEncodeParam at com.ibm.servlet.engine.webapp.StrictServletInstance.doService(ServletManager.java(Compiled Code)) at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(StrictLifecycleServlet.java(Compiled Code)) at com.ibm.servlet.engine.webapp.IdleServletState.service(StrictLifecycleServlet.java(Compiled Code)) at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java(Compiled Code)) at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java(Compiled Code)) at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletManager.java(Compiled Code)) at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java(Compiled Code)) at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java(Compiled Code)) at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java(Compiled Code)) at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java(Compiled Code)) at com.ibm.servlet.engine.srt.WebAppInvoker.doForward(WebAppInvoker.java(Compiled Code)) at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java(Compiled Code)) at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(CachedInvocation.java(Compiled Code)) at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java(Compiled Code)) at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(OSEListener.java(Compiled Code)) at com.ibm.servlet.engine.http11.HttpConnection.handleRequest(HttpConnection.java(Compiled Code)) at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java(Compiled Code)) at com.ibm.ws.http.HttpConnection.run(HttpConnection.java(Compiled Code)) at com.ibm.ws.util.CachedThread.run(ThreadPool.java(Compiled Code))
附件里是我的源代码,各位DX帮帮吧。

source.zip (6.75k)

3.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: jameszhang
Posted on: 2003-11-07 14:47

class sun.awt.image.codec.JPEGParam does not implement interface com.sun.image.codec.jpeg.JPEGEncodeParam at com.ibm.servlet.engine.webapp.StrictServletInstance.doService...
不知道是不是因为这个?

4.Re:关于jpeg图片处理的问题 [Re: jameszhang] Copy to clipboard
Posted by: fat32
Posted on: 2003-11-07 14:49

问题是怎么解决呢,我用的WebSpshere版本在windows上和在linux上是一样的。但为什么在windows上能正常运行,在linux上就不行了呢?

5.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: jameszhang
Posted on: 2003-11-07 15:11

如果不麻烦的话,你可以换一个应用服务器是一下,看是不是WebSpshere的问题,如果麻烦你就直接给IBM打电话询问?

6.Re:关于jpeg图片处理的问题 [Re: jameszhang] Copy to clipboard
Posted by: fat32
Posted on: 2003-11-07 15:16

我有好几台linux机器,还有两台rs6000的机器,都报同样的错误。

7.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: jameszhang
Posted on: 2003-11-07 15:32

fat32 wrote:
我有好几台linux机器,还有两台rs6000的机器,都报同样的错误。

我说的是换WEBLOGIC试一下,呵呵,没说明白

8.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: safe
Posted on: 2003-11-07 16:02

class sun.awt.image.codec.JPEGParam does not implement interface com.sun.image.codec.jpeg.JPEGEncodeParam at com.ibm.servlet.engine.webapp.StrictServletInstance.doService...

sun.*, com.sun.* package 只在 sun 的 jdk 有吧
如果用其他 vendor 的 jdk 就沒了
先確認一下使用的 jdk vendor

9.Re:关于jpeg图片处理的问题 [Re: jameszhang] Copy to clipboard
Posted by: fat32
Posted on: 2003-11-07 16:26

我没有WebLogic。不过即使在Weblogic上能成功又有什么用呢,我们的生产环境用的是WebSphere,不可能改用WebLogic的啊

10.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: jameszhang
Posted on: 2003-11-07 16:49

那只能给IBM去电话说他的com.ibm.servlet.engine.webapp.StrictServletInstance是否兼容com.sun.image.codec.jpeg.JPEGEncodeParam

11.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: fat32
Posted on: 2003-11-07 23:47

终于有点线索了,哈哈:


Click here to open a new window

12.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: jameszhang
Posted on: 2003-11-08 08:49

fat32 wrote:
终于有点线索了,哈哈:


恭喜呀!呵呵

13.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: scottlai
Posted on: 2003-11-08 09:03

應該是少了 GRAPHIC LIB.
好像是glib吧.
年代久遠. 忘了還有哪幾個lib了
問ZUA...

14.Re:关于jpeg图片处理的问题 [Re: safe] Copy to clipboard
Posted by: rainman
Posted on: 2003-11-08 09:15

Jute有缩小图片的代码啊。zua以前在linux上测试的时候发现当linux缺少某些库的时候会不行。

15.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: scottlai
Posted on: 2003-11-08 09:20

古早的對話...

************************************************************
****************** 阿熊 - 11/10/02 10:21:34 ******************
************************************************************

[10:31] 阿熊 says: glibc是什么东东
[10:32] Scott says: 就一個圖型用的LIB啊
[10:33] Scott says: AWT 有GUI
[10:33] Scott says: 當然要NATIVE SUPPORT啊
[10:33] Scott says: SWING也是
[10:44] 阿熊 says: 如果安装x-window,是不是将那两个包也自动安装
[10:44] Scott says: 看你怎麼裝了
[10:44] 阿熊 says: 说明白些
[10:45] Scott says: XWINDOW一定要GLIBC啊
[10:45] Scott says: 看你LINUX怎麼裝啊
[10:45] Scott says: GLIBC 不一定會被裝進去啊
[10:46] 阿熊 says: 那xwindow怎样运行
[10:46] Scott says: XFREE86 不寒GLIB
[10:46] 阿熊 says: XWINDOW一定要GLIBC啊
[10:46] Scott says: 對啊
[10:46] 阿熊 says: GLIBC 不一定會被裝進去啊
[10:47] Scott says: 就看你有沒有選INSTALL GLIB啊
[10:47] Scott says: 偶怎知裝的人會不會選??
[10:47] 阿熊 says: 我是说安装xwindow
[10:48] Scott says: 對啊
[10:48] Scott says: 你選裝XWINDOW
[10:48] Scott says: 沒裝GLIB
[10:48] 阿熊 says: 那安装完x-window一定有glibc了
[10:48] Scott says: 那XWINDOW就不能跑了啊
[10:48] 阿熊 says: 那安装x-window有没有自动安装xfree86
[10:49] Scott says: XWINDOW可以用RPM裝
[10:49] 阿熊 says: glibc明白了
[10:49] Scott says: 也可以抓BIN回來裝
[10:49] Scott says: 那看裝的人如何裝
[10:49] Scott says: 就算用RPM裝XWINDOW
[10:49] 阿熊 says: 也是一样的
[10:50] Scott says: 自己也要去CHECK GLIB有沒有裝啊
[10:50] 阿熊 says: 那x-window是不是一定需要xfee86
[10:50] Scott says: XWINDOW市規範
[10:50] Scott says: XFREE86市實作
[10:51] 阿熊 says: 明白
[10:51] Scott says: XWINDOW市一種標準
[10:51] 阿熊 says: 就象J2EE 1.3

16.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: fat32
Posted on: 2003-11-08 20:12

如果使用1.4以下的JDK,我上面找到的那篇文章推荐使用Xvfb(X Virtual Frame Buffer),这个东东不需要服务器有物理的显示卡(adapter)。如果使用1.4以上的JDK,就只需要在应用服务器的启动脚本里加上-Djava.awt.headless=true就可以了。

不过我还没试过行不行。

17.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: coloumn
Posted on: 2003-11-11 14:41

看看你的Linux或Unix上的X Window Server是否启动了。好像java对一些图像的处理就是用到了本地的Window服务。如果还不行,你再考虑其他办法吧!

18.Re:关于jpeg图片处理的问题 [Re: fat32] Copy to clipboard
Posted by: bjwz
Posted on: 2003-11-11 20:54

这和Linux有关系吗?应该不会吧


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923