RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java程序裁剪图片代码 java程序裁剪图片代码是什么

java怎么剪裁图片

BufferedImage类有一个getSubimage()方法,以下来自API

沧源网站建设公司创新互联,沧源网站设计制作,有大型网站制作公司丰富经验。已为沧源上千余家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的沧源做网站的公司定做!

public BufferedImage getSubimage(int x,

int y,

int w,

int h)

返回由指定矩形区域定义的子图像。返回的 BufferedImage 与源图像共享相同的数据数组。

参数:

x - 指定矩形区域左上角的 X 坐标

y - 指定矩形区域左上角的 Y 坐标

w - 指定矩形区域的宽度

h - 指定矩形区域的高度

返回:

BufferedImage,它是此 BufferedImage 的子图像。

抛出:

RasterFormatException - 如果指定区域不包含在此 BufferedImage 中。

如何在Java中进行图片剪裁 疯狂JAVA

package test;

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.geom.AffineTransform;

import java.awt.image.AffineTransformOp;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.nio.Buffer;

import javax.imageio.ImageIO;

import javax.imageio.stream.ImageOutputStream;

/**

* 裁剪、缩放图片工具类

* @author CSDN 没有梦想-何必远方

*/

public class ImgUtils {

/**

 * 缩放图片方法

 * 

 * @param srcImageFile

 *            要缩放的图片路径

 * @param result

 *            缩放后的图片路径

 * @param height

 *            目标高度像素

 * @param width

 *            目标宽度像素

 * @param bb

 *            是否补白

 */

public final static void scale(String srcImageFile, String result,

int height, int width, boolean bb) {

try {

double ratio = 0.0; // 缩放比例

File f = new File(srcImageFile);

BufferedImage bi = ImageIO.read(f);

Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);// bi.SCALE_SMOOTH

// 选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。

// 计算比例

if ((bi.getHeight()  height) || (bi.getWidth()  width)) {

double ratioHeight = (new Integer(height)).doubleValue()

/ bi.getHeight();

double ratioWhidth = (new Integer(width)).doubleValue()

/ bi.getWidth();

if (ratioHeight  ratioWhidth) {

ratio = ratioHeight;

} else {

ratio = ratioWhidth;

}

AffineTransformOp op = new AffineTransformOp(AffineTransform// 仿射转换

.getScaleInstance(ratio, ratio), null);// 返回表示剪切变换的变换

itemp = op.filter(bi, null);// 转换源 BufferedImage 并将结果存储在目标

// BufferedImage 中。

}

if (bb) {// 补白

BufferedImage image = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);// 构造一个类型为预定义图像类型之一的

// BufferedImage。

Graphics2D g = image.createGraphics();// 创建一个

// Graphics2D,可以将它绘制到此

// BufferedImage 中。

g.setColor(Color.white);// 控制颜色

g.fillRect(0, 0, width, height);// 使用 Graphics2D 上下文的设置,填充 Shape

// 的内部区域。

if (width == itemp.getWidth(null))

g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,

itemp.getWidth(null), itemp.getHeight(null),

Color.white, null);

else

g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,

itemp.getWidth(null), itemp.getHeight(null),

Color.white, null);

g.dispose();

itemp = image;

}

ImageIO.write((BufferedImage) itemp, "JPEG", new File(result)); // 输出压缩图片

} catch (IOException e) {

e.printStackTrace();

}

}

/**

 * 裁剪图片方法

 * 

 * @param bufferedImage

 *            图像源

 * @param startX

 *            裁剪开始x坐标

 * @param startY

 *            裁剪开始y坐标

 * @param endX

 *            裁剪结束x坐标

 * @param endY

 *            裁剪结束y坐标

 * @return

 */

public static BufferedImage cropImage(BufferedImage bufferedImage,

int startX, int startY, int endX, int endY) {

int width = bufferedImage.getWidth();

int height = bufferedImage.getHeight();

if (startX == -1) {

startX = 0;

}

if (startY == -1) {

startY = 0;

}

if (endX == -1) {

endX = width - 1;

}

if (endY == -1) {

endY = height - 1;

}

BufferedImage result = new BufferedImage(endX - startX, endY - startY,

4);

for (int x = startX; x  endX; ++x) {

for (int y = startY; y  endY; ++y) {

int rgb = bufferedImage.getRGB(x, y);

result.setRGB(x - startX, y - startY, rgb);

}

}

return result;

}

public static void main(String[] args) throws IOException {

File input = new File("input.jpg");

BufferedImage img = ImageIO.read(input);

cropImage(img, 10, 10, 20, 20);

File output = new File("output.jpg");

ImageIO.write(img, "jpg", output);

}

}

java中如何用鼠标点击截取一张图片的某部分(希望有具体代码)

用map标签,在drw里用热区,拖动就可以了,你试试

代码会自动生成,如下:

map

name="Map"

id="Map"area

shape="rect"

coords="104,303,223,357"

href=""

/

/map

你只要换掉104,303,223,357(图片区域上下左右坐标的位置)和超链接地址即可

java图片裁剪原理

总体思想

前台网页用js得到裁剪图片的id及x y 宽度和高度

服务端根据id取出要裁剪的图片

根据这些参数来生成裁剪的图像 后台代码如下

java 代码

package wodexiangce;

import java awt Rectangle;

import java awt image BufferedImage;

import java io File;

import java io FileInputStream;

import java io IOException;

import java util Iterator;

import javax imageio ImageIO;

import javax imageio ImageReadParam;

import javax imageio ImageReader;

import javax imageio stream ImageInputStream;

/**

*

*

*

*/

public class OperateImage {

// ===源图片路径名称如 c:\ jpg

private String srcpath ;

// ===剪切图片存放路径名称 如 c:\ jpg

private String subpath ;

// ===剪切点x坐标

private int x ;

private int y ;

// ===剪切点宽度

private int width ;

private int height ;

public OperateImage() {

}

public OperateImage( int x int y int width int height) {

this x = x ;

this y = y ;

this width = width ;

this height = height ;

}

/**

* 对图片裁剪 并把裁剪完蛋新图片保存

*/

public void cut() throws IOException {

FileInputStream is = null ;

ImageInputStream iis = null ;

try {

// 读取图片文件

is = new FileInputStream(srcpath)

/*

* 返回包含所有当前已注册 ImageReader 的 Iterator 这些 ImageReader

* 声称能够解码指定格式 参数 formatName 包含非正式格式名称

*(例如 jpeg 或 tiff )等

*/

Iterator ImageReader it = ImageIO getImageReadersByFormatName( jpg )

ImageReader reader = it next()

// 获取图片流

iis = ImageIO createImageInputStream(is)

/*

* piis:读取源 true:只向前搜索 /p 将它标记为 只向前搜索

* 此设置意味着包含在输入源中的图像将只按顺序读取 可能允许 reader

* 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分

*/

reader setInput(iis true ) ;

/*

* p描述如何对流进行解码的类p 用于指定如何在输入时从 Java Image I/O

* 框架的上下文中的流转换一幅图像或一组图像 用于特定图像格式的插件

* 将从其 ImageReader 实现的 getDefaultReadParam 方法中返回

* ImageReadParam 的实例

*/

ImageReadParam param = reader getDefaultReadParam()

/*

* 图片裁剪区域 Rectangle 指定了坐标空间中的一个区域 通过 Rectangle 对象

* 的左上顶点的坐标(x y) 宽度和高度可以定义这个区域

*/

Rectangle rect = new Rectangle(x y width height)

// 提供一个 BufferedImage 将其用作解码像素数据的目标

param setSourceRegion(rect)

/*

* 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象 并将

* 它作为一个完整的 BufferedImage 返回

*/

BufferedImage bi = reader read( param)

// 保存新图片

ImageIO write(bi jpg new File(subpath))

} finally {

if (is != null )

is close() ;

if (iis != null )

iis close()

}

}

public int getHeight() {

return height;

}

public void setHeight( int height) {

this height = height;

}

public String getSrcpath() {

return srcpath;

}

public void setSrcpath(String srcpath) {

this srcpath = srcpath;

}

public String getSubpath() {

return subpath;

}

public void setSubpath(String subpath) {

this subpath = subpath;

}

public int getWidth() {

return width;

}

public void setWidth( int width) {

this width = width;

}

public int getX() {

return x;

}

public void setX( int x) {

this x = x;

}

public int getY() {

return y;

}

public void setY( int y) {

this y = y;

}

public static void main(String[] args) throws Exception {

String name = d:\ jpg ;

OperateImage o = new OperateImage( )

o setSrcpath(name)

o setSubpath( D:\ jpg )

o cut() ;

}

lishixinzhi/Article/program/Java/hx/201311/26771


当前标题:java程序裁剪图片代码 java程序裁剪图片代码是什么
转载源于:http://scyingshan.cn/article/hpeocg.html