新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用imageIO 读取进入BufferedImage,检测图片非空白大小,建立新的BufferedImage,拷贝非空白区至新的BufferedImage, 用imageIO 再次保存。
创新互联建站是一家以网络技术公司,为中小企业提供网站维护、成都网站制作、做网站、外贸营销网站建设、网站备案、服务器租用、国际域名空间、软件开发、微信小程序等企业互联网相关业务,是一家有着丰富的互联网运营推广经验的科技公司,有着多年的网站建站经验,致力于帮助中小企业在互联网让打出自已的品牌和口碑,让企业在互联网上打开一个面向全国乃至全球的业务窗口:建站咨询热线:18982081108
package com.supben.util;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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;
import org.apache.log4j.Logger;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil {
private static final Logger log = Logger.getLogger(ImageUtil.class);
/**
* 切割图片
* @param x 截点横坐标 (从左开始计数)
* @param y 截点纵坐标 (从上开始计数)
* @param width 截取的宽度
* @param height 截取的长度
* @param oldpath 图片位置
* @param newpath 新生成的图片位置
*/
public static void cutImage(int x, int y, int width, int height, String oldpath, String newpath) {
FileInputStream is = null;
ImageInputStream iis = null;
//这个是获取图片扩展名的方法,比如:jpg。我这里有现成的,如果没有,自己实现
String imgType = StringUtil.getExt(oldpath);
try {
is = new FileInputStream(oldpath);
IteratorImageReader it = ImageIO.getImageReadersByFormatName(imgType);
ImageReader reader = it.next();
iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Point p = new Point();
p.setLocation(x, y);
Dimension d = new Dimension();
d.setSize(width, height);
Rectangle rect = new Rectangle(p, d);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, imgType, new File(newpath));
is.close();
iis.close();
} catch (Exception e) {
log.error(e);
}
}
/**
* 缩略图片
* @param oldpath 原图片
* @param newpath 新生成的图片存放地址
* @param wdith 缩略后的宽
* @param height 缩略后的高
*/
public static void scaleImage(String oldpath, String newpath, int wdith, int height) {
// 获取老的图片
File oldimg = new File(oldpath);
try {
BufferedImage bi = ImageIO.read(oldimg);
Image Itemp = bi.getScaledInstance(wdith, height, BufferedImage.SCALE_SMOOTH);
BufferedImage thumbnail = new BufferedImage(wdith, height, BufferedImage.TYPE_INT_RGB);
thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);
// 缩略后的图片路径
File newimg = new File(newpath);
FileOutputStream out = new FileOutputStream(newimg);
// 绘图
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbnail);
param.setQuality(1.0f, false);
encoder.encode(thumbnail);
out.close();
bi.flush();
bi = null;
} catch (IOException e) {
log.error(e);
}
}
public static void main(String[] args) {
scaleImage("D:/2.jpg", "D:/3.jpg", 50, 50);
}
}
用map标签,在drw里用热区,拖动就可以了,你试试
代码会自动生成,如下:
map
name="Map"
id="Map"area
shape="rect"
coords="104,303,223,357"
href=""
/
/map
你只要换掉104,303,223,357(图片区域上下左右坐标的位置)和超链接地址即可
呵呵,很明确的告诉你:可以!
代码半小时后出来!!!
……
终于出来了(呵呵,好像超过了半小时哈)且看代码:
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报春到你的电脑上了。下面程序的运行效果吧!