日期:2014-05-18  浏览次数:20883 次

帮忙把下面的C#代码转换成Java,谢谢
C# code
public class ImageCropper extends IHttpHandler{
    public void ProcessRequest(HttpContext context)
    {
        String ImgPath = Convert.ToString(context.Request["p"]);
        int PointX = Convert.ToInt32(context.Request["x"]);
        int PointY = Convert.ToInt32(context.Request["y"]);
        int CutWidth = Convert.ToInt32(context.Request["w"]);
        int CutHeight = Convert.ToInt32(context.Request["h"]);
        int PicWidth = Convert.ToInt32(context.Request["pw"]);
        int PicHeight = Convert.ToInt32(context.Request["ph"]);

        context.Response.ContentType = "image/jpeg";
        ShowImage(HttpContext.Current.Server.MapPath(ImgPath), PointX, PointY, CutWidth, CutHeight, PicWidth, PicHeight).WriteTo(context.Response.OutputStream);
    }

    private MemoryStream ShowImage(String path, int PointX, int PointY, int CutWidth, int CutHeight, int PicWidth, int PicHeight)
    {
        Image image = Image.FromFile(path);
        BeanMap bm = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        Graphics graphics = Graphics.FromImage(bm);
        graphics.DrawImage(image, new Rectangle(0, 0, CutWidth, CutHeight), PointX * image.Width / PicWidth, PointY * image.Height / PicHeight, CutWidth * image.Width / PicWidth, CutHeight * image.Height / PicHeight, GraphicsUnit.Pixel);
        int a = path.LastIndexOf('.');
        int b = path.LastIndexOf('\\');
        string _newPic = path.Substring(0, b) + "\\new_" + DateTime.Now.ToFileTimeUtc().ToString("X") + ".jpg";
        bm.Save(_newPic, System.Drawing.Imaging.ImageFormat.Jpeg);
        MemoryStream ms = new MemoryStream();
        bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        image.Dispose();
        graphics.Dispose();
        bm.Dispose();
        return ms;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}


------解决方案--------------------
你应该去找现成的java代码,很多的,
java和.net是完全不同的处理方法,为什么要转呢
------解决方案--------------------
重新写来得比较快。
------解决方案--------------------
.net 和java处理起来会有些不同的,你随便搜一下“java 缩略图”,一堆现成的啊。

Java code

import java.io.*;import java.awt.*;
import java.awt.image.*;import com.sun.image.codec.jpeg.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c)2007-6-13</p>
 * <p>Company: fuen</p>
 * @author 杨振朋
 * @version 1.0
 */
public class ccc {
   private String srcFile;
   private String destFile;
   private int width;
   private int height;
   private Image img;  
  
   /**
    * 构造函数
    * @param fileName String
    * @throws IOException
    */
   public ccc(String fileName) throws IOException {
     File _file = new File(fileName); //读入文件
     this.srcFile = _file.getName();
     this.destFile = "c:/aa.jpg";//this.srcFile.substring(0, this.srcFile.lastIndexOf(".")) +"_s.jpg";
     img = javax.imageio.ImageIO.read(_file); //构造Image对象
     width = img.getWidth(null); //得到源图宽
     height = img.getHeight(null); //得到源图长
   }   /**
    * 强制压缩/放大图片到固定的大小
    * @param w int 新宽度
    * @param h int 新高度
    * @throws IOException
    */
   public void resize(int w, int h) throws IOException {
     BufferedImage _image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
     _image.getGraphics().drawImage(img, 0, 0, w, h, null); //绘制缩小后的图
     FileOutputStream newimageout = new FileOutputStream(destFile); //输出到文件流
     /*
      * JPEGImageEncoder 将图像缓冲数据编码为 JPEG 数据流。该接口的用户应在 Raster
      * 或 BufferedImage 中提供图像数据,在 JPEGEncodeParams 对象中设置必要的