- package com.zhao.myImage;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class ChangeImage {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- File inputFile = new File("c://test.bmp");
- try {
- BufferedImage input = ImageIO.read(inputFile);
- Image big = input.getScaledInstance(1920, 1080,Image.SCALE_DEFAULT);
- //将Image 强制转换成 BufferedImage 不行,Image是一个抽象类,
- //BufferedImage是继承Image的一个类
- BufferedImage inputbig ;
- inputbig = new BufferedImage(1920, 1080,BufferedImage.TYPE_INT_ARGB);
- inputbig.getGraphics().drawImage(big, 0, 0, 1920, 1080, null);
- File outputFile = new File("c://test.png");
- ImageIO.write(inputbig, "PNG", outputFile);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Image相关的数据类型转换和用法
首先定义用到的数据类型
Icon icon;//声明一个图标类
Image image;
ImageIcon icon;//使用ImageIcon类创建一个图标
BufferedImage bi;
byte[] bytes;
Blob blob;
String fileName;
一、ImageIcon类
构造函数
icon = new ImageIcon(fileName);
icon = new ImageIcon(bytes);
icon = new ImageIcon(image);
实用方法
int getIconHeight();
int getIconWidth();
void setImage(Image image);
Image getImage();
小结
ImageIcon与Image间的转换比较方便,更偏向于应用方面,主要是控件中的setIcon(ImageIcon)方法。
二、Image类
实用方法
1.缩放图片大小,其中hints通常用Image.SCALE_SMOOTH
Image getScaledInstance(int width, int height, int hints);
2.获取图片长宽值
int getHeight(null);
int getWidth(null);
小结
Image更像一个过渡类,将图形转换成别的图形类,如ImageIcon、BufferedImage等。
三、BufferedImage类
构造函数
bi = new BufferedImage(int width, int height, int imageType);
这里的imageType我习惯用BufferedImage.TYPE_INT_ARGB,构造出来的BufferedImage更像是一个模式,需要往里面加数据。
关于BufferedImage的一些用法
1.Image写入BufferedImage
bi.getGraphics().drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
2.BufferedImage转换到bytes[]或文件
bi = ImageIO.read(new File(fileName));
//放缩到36*36
image= bi.getScaledInstance (36,36,Image.SCALE_SMOOTH);
double wRatio = 36.0/bi.getWidth();
double hRatio = 36.0/bi.getHeight();
//AffineTransformOp.TYPE_BICUBIC 表示3次方放缩
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio, hRatio),AffineTransformOp.TYPE_BICUBIC);
bi = op.filter(bi, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", out);
bytes = out.toByteArray();
这里用到的ImageIO.write方法参数为 (RenderedImage im, String formatName, ImageOutputStream output),其中参数 output是图形的输出流,可以定向到文件流new FileOutputStream()实现将图象写入到文件。
3.以上两个用法综合使用可以将Image写入到byte[]和文件。
四、byte[]和Blob的转换
图像在MySql中用BLOB数据类型(Binary Large Object)存储
1.byte[]到Blob转换
blob = new SerialBlob(bytes);
blob.setBytes(long pos, byte[] bytes);
2.Blob到byte[]转换
bytes = blob.getBytes(long pos, int length);
这里需要注意的一点是存储图形到MySql数据库时使用Blob类型,从MySql取出图形的Object应转换到byte[]
评论
# re: Image相关的数据类型转换和用法 2007-11-15 18:10 过路人
楼主分析得很细,我现在正好遇到了这个问题需要解决.
不过我提出一点:swt中的Image与swing中的Image不知楼主有没有比较好相互转换方法?因为我现在用的是swt来做界面.
http://www.blogjava.net/fengwei1314/archive/2007/04/25/113569.html