码迷,mamicode.com
首页 > 其他好文 > 详细

图片处理方法收集

时间:2015-04-14 14:27:55      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

package com.tools;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
* 制作简单图片工具
* @author 赵俊
*/
public class ImageTool {

/**
* 生成图片边框
* @param width 边框总宽度
* @param height 边框总高度
* @parma borderWidth 边框宽度
* @param borderColor 边框颜色
* @param comp 透明度
*/
public static BufferedImage getImageBorder(int width, int height, int borderWidth, int borderColor, float comp){
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB) ;
Graphics2D g2d = img.createGraphics() ;
String rgbStr = Integer.toHexString(borderColor) ;
float r = Integer.parseInt(rgbStr.substring(0, 2), 16) ;
float g = Integer.parseInt(rgbStr.substring(2, 4), 16) ;
float b = Integer.parseInt(rgbStr.substring(4, 6), 16) ;
g2d.setColor(new Color(r/0xFF, g/0xFF, b/0xFF, comp)) ;
//上边
g2d.fillRect(borderWidth, 0, width-borderWidth, borderWidth) ;
//右边
g2d.fillRect(width-borderWidth, borderWidth, borderWidth, height-borderWidth) ;
//下边
g2d.fillRect(0, height-borderWidth, width-borderWidth, borderWidth) ;
//左边
g2d.fillRect(0, 0, borderWidth, height-borderWidth) ;
g2d.dispose() ;
return img ;
}

/**
* 生成图片边框
* @param os 输出流
* @param formatName 图片类型(png/jpeg/...)
* @param width 边框总宽度
* @param height 边框总高度
* @parma borderWidth 边框宽度
* @param borderColor 边框颜色
* @param comp 透明度
* @throws IOException
*/
public static void getImageBorder(OutputStream os, String formatName, int width, int height, int borderWidth, int borderColor, float comp) throws IOException{
BufferedImage img = getImageBorder(width, height, borderWidth, borderColor, comp) ;
ImageIO.write(img, formatName, os) ;
}

/**
* 生成图片边框
* @param os 输出流
* @param width 边框总宽度
* @param height 边框总高度
* @parma borderWidth 边框宽度
* @param borderColor 边框颜色
* @param comp 透明度
* @throws IOException
*/
public static void getImageBorder(OutputStream os, int width, int height, int borderWidth, int borderColor, float comp) throws IOException{
BufferedImage img = getImageBorder(width, height, borderWidth, borderColor, comp) ;
ImageIO.write(img, "png", os) ;
}

/**
* 图片缩放
* @param srcImg 原图
* @param nw 缩放后图片的宽
* @param nh 缩放后图片的高
*/
public static BufferedImage zoomImage(BufferedImage srcImg, int nw, int nh){
BufferedImage img = null ;
img = new BufferedImage(nw, nh, srcImg.getType()) ;
Graphics g = img.getGraphics() ;
g.drawImage(srcImg, 0, 0, nw, nh, null) ;
g.dispose() ;
return img ;
}

/**
* 图片缩放
* @param srcImg 原图
* @param nw 缩放后图片的宽
* @param nh 缩放后图片的高
*/
public static BufferedImage zoomImage(Image srcImg, int nw, int nh){
BufferedImage img = null ;
img = new BufferedImage(nw, nh, BufferedImage.TYPE_INT_ARGB) ;
Graphics g = img.getGraphics() ;
g.drawImage(srcImg, 0, 0, nw, nh, null) ;
g.dispose() ;
return img ;
}

/**
* 图片缩放
* @param srcImg 原图
* @param scale 缩放倍数
*/
public static BufferedImage zoomImage(BufferedImage srcImg, float scale){
//新计算新图片的大小
int sw = srcImg.getWidth() ;
int sh = srcImg.getHeight() ;
int nw = (int) (sw * scale) ;
int nh = (int) (sh * scale) ;
return zoomImage(srcImg, nw, nh) ;
}

/**
* 图片缩放
* @param srcImg 原图
* @param nw 缩放后图片的宽
*/
public static BufferedImage zoomImageByNewWidth(BufferedImage srcImg, int nw){
//新计算新图片的大小
int sw = srcImg.getWidth() ;
int sh = srcImg.getHeight() ;
float scale = nw / (float) sw ;
int nh = (int) (sh * scale) ;
return zoomImage(srcImg, nw, nh) ;
}

/**
* 图片缩放
* @param srcImg 原图
* @param nw 缩放后图片的宽
*/
public static BufferedImage zoomImageByNewWidth(Image srcImg, int nw){
//新计算新图片的大小
Image _srcImg = new ImageIcon(srcImg).getImage() ;
int sw = _srcImg.getWidth(null) ;
int sh = _srcImg.getHeight(null) ;
float scale = nw / (float) sw ;
int nh = (int) (sh * scale) ;
return zoomImage(_srcImg, nw, nh) ;
}

/**
* 图片缩放
* @param srcImg 原图
* @param nw 缩放后图片的宽
* @throws IOException
*/
public static void zoomImageByNewWidth(OutputStream os, URL srcImg, int nw) throws IOException{
//使用ImageIO.read()读取图片会丢失部分信息,得到的图片感觉像蒙了一层东西(有人说是读取时ICC信息的丢失),
//所以使用Toolkit.getDefaultToolkit().getImage()
BufferedImage img = ImageTool.zoomImageByNewWidth(Toolkit.getDefaultToolkit().getImage(srcImg), nw) ;
ImageIO.write(img, "png", os) ;
}


public static void main(String[] args) {

try {
ImageTool.getImageBorder(new FileOutputStream("e:/v_border_n.png"),
174, 116, 5, 0x3FA1DD, 0.8f) ;

String url = "http://192.168.136.80:8080/remote-support/convert_files/video/201504/63f28cf5d8704c9188d4c7efe75134b3.jpg" ;
ImageTool.zoomImageByNewWidth(new FileOutputStream("e:/v_yyy.jpg"), new URL(url), 170) ;

BufferedImage img = ImageTool.zoomImageByNewWidth(Toolkit.getDefaultToolkit().getImage(new URL(
"http://192.168.136.80:8080/remote-support/convert_files/video/201504/63f28cf5d8704c9188d4c7efe75134b3.jpg")), 170) ;
ImageIO.write(img, "png", new FileOutputStream("e:/v_xxx.png")) ;

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

图片处理方法收集

标签:

原文地址:http://www.cnblogs.com/ooi-/p/4424733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!