网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java代码裁剪图片 java图片裁剪工具

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

package test;

我们提供的服务有:成都网站建设、网站建设、微信公众号开发、网站优化、网站认证、信州ssl等。为成百上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的信州网站制作公司

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怎么剪裁图片

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图片裁剪原理

总体思想

前台网页用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截取图片

呵呵,很明确的告诉你:可以!

代码半小时后出来!!!

……

终于出来了(呵呵,好像超过了半小时哈)且看代码:

import java.awt.Color;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.JApplet;

public class Test extends JApplet{

String addrs="F:\\images\\mm.bmp";//改成自己的图片路径

BufferedImage mm,child;

CutImage ci;

public Test(){

try {

mm=ImageIO.read(new File(addrs));

} catch (IOException e) {

System.out.println("图片读取失败!");

e.printStackTrace();

}

ci=new CutImage(mm);

child=ci.getChildImage(50, 0, 150, 220);

}

public void init(){

}

public void paint(Graphics g){

g.setColor(Color.red);

g.drawString("原图:",0,10);

g.drawImage(mm, 20,10,this);

g.drawString("ci.getChildImage(50, 0, 150, 220)截取后的图片",mm.getWidth()+30,10);

g.drawImage(child, mm.getWidth()+50,20,this);

}

}

import java.awt.Image;

import java.awt.image.BufferedImage;

public class CutImage {

private BufferedImage img;

private BufferedImage child;

public CutImage(){

}

public CutImage(BufferedImage im){

img=im;

}

public CutImage(Image im){

img=(BufferedImage)im;

}

public void setImg(BufferedImage img) {

this.img = img;

}

public BufferedImage getChildImage(int x,int y,int width,int height) {

int cw=width;

int ch=height;

int pw=img.getWidth();

int ph=img.getHeight();

if(pwx+width){

System.out.println("给出的参数超出原图片的范围!程序会自动减小宽度或高度");

cw=pw-x;

}

if(phy+height){

System.out.println("给出的参数超出原图片的范围!程序会自动减小宽度或高度");

ch=ph-y;

}

child=new BufferedImage(cw,ch,BufferedImage.TYPE_INT_ARGB );

for(int i=0;ich;i++){

for(int j=0;jcw;j++){

child.setRGB(j, i, img.getRGB(x+j, y+i));

}

}

return child;

}

}

呵呵,希望楼主能够满意哦,如果你愿意的话,稍微改一下代码就可以把截取的图片child报春到你的电脑上了。下面程序的运行效果吧!

您好!请问用java怎么将截取png的图片中间一部分,以及如何压缩一个png图片?

getSubimage方法是进行图片裁剪。

举例:

public static void main(String[] args) {

try {

//从特定文件载入

BufferedImage bi = ImageIO.read(new File("c:\\test.png"));

bi.getSubimage(0, 0, 10, 10);//前两个值是坐标位置X、Y,后两个是长和宽

} catch (IOException e) {

e.printStackTrace();

}

}

以下是进行的图片压缩,涉及到多个工具类。

/**

* 图片工具类

* 压缩图片大小

* @author Cyw

* @version 1.0

*/

public class ZIPImage {

private File file = null;

private String outPutFilePath;

private String inPutFilePath;

private String inPutFileName;

private boolean autoBuildFileName;

private String outPutFileName;

private int outPutFileWidth = 100; // 默认输出图片宽

private int outPutFileHeight = 100; // 默认输出图片高

private static boolean isScaleZoom = true; // 是否按比例缩放

public ZIPImage() {

outPutFilePath = "";

inPutFilePath = "";

inPutFileName = "";

autoBuildFileName = true;

outPutFileName = "";

}

/**

* @param ipfp

* 源文件夹路径

* @param ipfn

* 源文件名

* @param opfp

* 目标文件路径

* @param opfn

* 目标文件名

*/

public ZIPImage(String ipfp, String ipfn, String opfp, String opfn) {

outPutFilePath = opfp;

inPutFilePath = ipfp;

inPutFileName = ipfn;

autoBuildFileName = true;

outPutFileName = opfn;

}

/**

* @param ipfp

* 源文件夹路径

* @param ipfn

* 源文件名

* @param opfp

* 目标文件路径

* @param opfn

* 目标文件名

* @param aBFN

* 是否自动生成目标文件名

*/

public ZIPImage(String ipfp, String ipfn, String opfp, String opfn,

boolean aBFN) {

outPutFilePath = opfp;

inPutFilePath = ipfp;

inPutFileName = ipfn;

autoBuildFileName = aBFN;

outPutFileName = opfn;

}

public boolean isAutoBuildFileName() {

return autoBuildFileName;

}

public void setAutoBuildFileName(boolean autoBuildFileName) {

this.autoBuildFileName = autoBuildFileName;

}

public String getInPutFilePath() {

return inPutFilePath;

}

public void setInPutFilePath(String inPutFilePath) {

this.inPutFilePath = inPutFilePath;

}

public String getOutPutFileName() {

return outPutFileName;

}

public void setOutPutFileName(String outPutFileName) {

this.outPutFileName = outPutFileName;

}

public String getOutPutFilePath() {

return outPutFilePath;

}

public void setOutPutFilePath(String outPutFilePath) {

this.outPutFilePath = outPutFilePath;

}

public int getOutPutFileHeight() {

return outPutFileHeight;

}

public void setOutPutFileHeight(int outPutFileHeight) {

this.outPutFileHeight = outPutFileHeight;

}

public int getOutPutFileWidth() {

return outPutFileWidth;

}

public void setOutPutFileWidth(int outPutFileWidth) {

this.outPutFileWidth = outPutFileWidth;

}

public boolean isScaleZoom() {

return isScaleZoom;

}

public void setScaleZoom(boolean isScaleZoom) {

this.isScaleZoom = isScaleZoom;

}

public String getInPutFileName() {

return inPutFileName;

}

public void setInPutFileName(String inPutFileName) {

this.inPutFileName = inPutFileName;

}

/**

* 压缩图片大小

* @return boolean

*/

public boolean compressImage() {

boolean flag = false;

try {

if (inPutFilePath.trim().equals("")) {

throw new NullPointerException("源文件夹路径不存在。");

}

if (inPutFileName.trim().equals("")) {

throw new NullPointerException("图片文件路径不存在。");

}

if (outPutFilePath.trim().equals("")) {

throw new NullPointerException("目标文件夹路径地址为空。");

} else {

if (!ZIPImage.mddir(outPutFilePath)) {

throw new FileNotFoundException(outPutFilePath

+ " 文件夹创建失败!");

}

}

if (this.autoBuildFileName) { // 自动生成文件名

String tempFile[] = getFileNameAndExtName(inPutFileName);

outPutFileName = tempFile[0] + "_cyw." + tempFile[1];

compressPic();

} else {

if (outPutFileName.trim().equals("")) {

throw new NullPointerException("目标文件名为空。");

}

compressPic();

}

} catch (Exception e) {

flag = false;

e.printStackTrace();

return flag;

}

return flag;

}

// 图片处理

private void compressPic() throws Exception {

try {

// 获得源文件

file = new File(inPutFilePath + inPutFileName);

if (!file.exists()) {

throw new FileNotFoundException(inPutFilePath + inPutFileName

+ " 文件不存在!");

}

Image img = ImageIO.read(file);

// 判断图片格式是否正确

if (img.getWidth(null) == -1) {

throw new Exception("文件不可读!");

} else {

int newWidth;

int newHeight;

// 判断是否是等比缩放

if (ZIPImage.isScaleZoom == true) {

// 为等比缩放计算输出的图片宽度及高度

double rate1 = ((double) img.getWidth(null))

/ (double) outPutFileWidth + 0.1;

double rate2 = ((double) img.getHeight(null))

/ (double) outPutFileHeight + 0.1;

// 根据缩放比率大的进行缩放控制

double rate = rate1 rate2 ? rate1 : rate2;

newWidth = (int) (((double) img.getWidth(null)) / rate);

newHeight = (int) (((double) img.getHeight(null)) / rate);

} else {

newWidth = outPutFileWidth; // 输出的图片宽度

newHeight = outPutFileHeight; // 输出的图片高度

}

BufferedImage tag = new BufferedImage((int) newWidth,

(int) newHeight, BufferedImage.TYPE_INT_RGB);

/*

* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢

*/

tag.getGraphics().drawImage(

img.getScaledInstance(newWidth, newHeight,

Image.SCALE_SMOOTH), 0, 0, null);

FileOutputStream out = new FileOutputStream(outPutFilePath

+ outPutFileName);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);

out.close();

}

} catch (IOException ex) {

ex.printStackTrace();

}

}

/**

* 创建文件夹目录

* @param filePath

* @return

* @throws Exception

*/

@SuppressWarnings("unused")

private static boolean mddir(String filePath) throws Exception {

boolean flag = false;

File f = new File(filePath);

if (!f.exists()) {

flag = f.mkdirs();

} else {

flag = true;

}

return flag;

}

/**

* 获得文件名和扩展名

* @param fullFileName

* @return

* @throws Exception

*/

private String[] getFileNameAndExtName(String fullFileName)

throws Exception {

String[] fileNames = new String[2];

if (fullFileName.indexOf(".") == -1) {

throw new Exception("源文件名不正确!");

} else {

fileNames[0] = fullFileName.substring(0, fullFileName

.lastIndexOf("."));

fileNames[1] = fullFileName

.substring(fullFileName.lastIndexOf(".") + 1);

}

return fileNames;

}

public Image getSourceImage() throws IOException{

//获得源文件

file = new File(inPutFilePath + inPutFileName);

if (!file.exists()) {

throw new FileNotFoundException(inPutFilePath + inPutFileName

+ " 文件不存在!");

}

Image img = ImageIO.read(file);

return img;

}

/*

* 获得图片大小 

* @path :图片路径

*/

public long getPicSize(String path) {

File file = new File(path);

return file.length();

}

}

//下面是测试程序

package com.sun.util.cyw;

import java.awt.Image;

import java.io.IOException;

public class ImageTest {

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

ZIPImage zip=new ZIPImage("d:\\","1.jpg","d:\\test\\","处理后的图片.jpg",false);

zip.setOutPutFileWidth(1000);

zip.setOutPutFileHeight(1000);

Image image=zip.getSourceImage();

long size=zip.getPicSize("d:\\1.jpg");

System.out.println("处理前的图片大小 width:"+image.getWidth(null));

System.out.println("处理前的图片大小 height:"+image.getHeight(null));

System.out.println("处理前的图片容量:"+ size +" bit");

zip.compressImage();

}

}


网页题目:java代码裁剪图片 java图片裁剪工具
网页路径:http://bjjierui.cn/article/dodssig.html

其他资讯