how to resize GIF images
Hi
I used acme.jar to wirte gif images to a local disk . i worked fine , but iam facing problem in resizing these images , when i resize these images i get black background color in tat images .. can any body help me regarding this.....
thumbImage = scaleToSize(imageWidth, imageHeight, thumbImage);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image,0,0,thumbWidth,thumbHeight,null);
out = new BufferedOutputStream(new FileOutputStream(newFilePath));
GifEncoder gife = new GifEncoder(image,out);
gife.encode();
this is my code .....
Following is my code:
public final String dealFile(ActionMapping mapping,ActionForm form,
HttpServletRequest request) throws FileNotFoundException, IOException{
GroupForm gForm = (GroupForm) form;
ActionMessages errors = new ActionMessages();
// retrieve the file representation
FormFile file = gForm.getPictFile();
if (file == null) {
errors.add("FileNeeded", new ActionMessage("error.photo.filenull"));
saveErrors(request, errors);
return "failure";
}
// has the maximum length been exceeded?
Boolean maxLengthExceeded = (Boolean) request
.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
errors.add("maxLengthExceeded", new ActionMessage(
"error.photo.length"));
saveErrors(request, errors);
return "failure";
}
// retrieve the file name
String fileName = file.getFileName();
// judge file type
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
if (!(fileType.equalsIgnoreCase("jpg")
|| fileType.equalsIgnoreCase("jpeg")
|| fileType.equalsIgnoreCase("gif") || fileType
.equalsIgnoreCase("png"))) {
errors.add("NotPict", new ActionMessage("error.photo.wrongformat"));
saveErrors(request, errors);
return "failure";
}
Date date = new Date();
Random r = new Random(date.getTime());
// 生成新图片名
String newFileName = request.getSession().getId() + r.nextInt(10000)
+ "." + fileType;
String filePath = null;
String newFilePath = null;
// the directory to upload to
String uploadDir = servlet.getServletContext()
.getRealPath("/resources")
+ "/groupphoto/" + DateUtil.asShortString(date)+"/";
// prepare directory if not exists
File dirPath = new File(uploadDir);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
// retrieve the file data
InputStream stream = file.getInputStream();
// write the file to the file specified
OutputStream bos = new FileOutputStream(uploadDir + newFileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
buffer = null;
filePath = dirPath.getAbsolutePath() + Constants.FILE_SEP
+ newFileName;
File _file = new File(uploadDir + newFileName);
Image src = ImageIO.read(_file); // 构造Image对象
int width = src.getWidth(null); // 得到源图宽
int height = src.getHeight(null);// 得到源图高
// convert the picture to picture within 100x100 and save to a file with
// prefix
// 100x100_ for later listing purpose.
newFilePath = dirPath.getAbsolutePath() +Constants.FILE_SEP
+ "100x100_" + newFileName;
int newWidth = 0;
int newHeight = 0;
int bigger = width <= height ? height : width;
if (bigger > 100) {
if (width >= height) {
newWidth = 100;
newHeight = height * 100 / width;
} else {
newHeight = 100;
newWidth = width * 100 / height;
}
ImageUtil.convert(filePath, newFilePath, newWidth, newHeight);
}
// close the stream
stream.close();
_file = null;
src = null;
dirPath = null;
String url = request.getContextPath() + "/resources" + "/groupphoto/"
+ DateUtil.asShortString(date)+"/"+ "100x100_" + newFileName;
return url;
}