说明图片是切好,但切出的图片显示全黑色。请高手帮忙!
/**
* 图片切割
* @param srcImageFile 图片地址
* @param ImageFile 图片存放的地址
* @param w 切割宽度
* @param h 切割高度
* @param x1 开始x结点(left)
* @param y1 开始y结点(top)
* @param sw 图片宽度
* @param sh 图片高度
* @throws IOException
*/
public static void cut(String srcImageFile,String ImageFile, int w, int h, int x1, int y1, int sw, int sh) throws IOException {
// http://localhost:8080/ImpCra/createServlet?p=Sunset.jpg&x=117&y=201&w=61&h=50&pw=300&ph=400
Image img;
ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
if (sw >= w && sh >= h) {
//创建此图像的缩放版本
//Image image = bi.getScaledInstance(sw, sh, Image.SCALE_DEFAULT);
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
// 按指定 x、y、w 和 h 参数从源 Image 提取绝对矩形区域来构造 CropImageFilter
cropFilter = new CropImageFilter(x1, y1, w, h);
img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(bi.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "JPEG", new File(ImageFile));
}
}
/**
* 图片切割
* @param srcImageFile 图片地址
* @param ImageFile 图片存放的地址
* @param w 切割宽度
* @param h 切割高度
* @param x1 开始x结点(left)
* @param y1 开始y结点(top)
* @param sw 图片宽度
* @param sh 图片高度
* @throws IOException
*/
public static void cut(String srcImageFile,String ImageFile, int w, int h, int x1, int y1, int sw, int sh) throws IOException {
// http://localhost:8080/ImpCra/createServlet?p=Sunset.jpg&x=117&y=201&w=61&h=50&pw=300&ph=400
Image img;
ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
if (sw >= w && sh >= h) {
//创建此图像的缩放版本
//Image image = bi.getScaledInstance(sw, sh, Image.SCALE_DEFAULT);
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
// 按指定 x、y、w 和 h 参数从源 Image 提取绝对矩形区域来构造 CropImageFilter
cropFilter = new CropImageFilter(x1, y1, w, h);
img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(bi.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "JPEG", new File(ImageFile));
}
}
解决方案 »
- JS中出现NaN问题怎么解决?
- 一个sql子查询 分组问题!
- silverlight如何向webservice method传递List<Object>参数
- Hibernate可以直接访问public,private和protected的访问方法和field
- ssh
- 有哪位高人熟悉JAVA/CORBA,请指教啊!!!!!
- 为什么Hibernate能直接访问private的方法?
- 问一个很菜的问题,请多帮忙
- 在servlet中的doGet或init或destroy中new File("myfile").createNewFile()
- netty 百万级长连接
- Hibernate的delete问题!跪求解决!!
- pushlet/服务器推技术
setClip(int x, int y, int width, int height);BufferedImage
getSubimage(int x, int y, int width, int height);