图片java
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.swing.*;

public class FastJpeg {
private ImageReader reader = null;
private BufferedImage bimage = null;
private ImageReadParam param = null;

private int ideal_width = 0;
private int ideal_height = 0;

public FastJpeg(int width, int height) {
ideal_width = width;
ideal_height = height;

Iterator iter = ImageIO.getImageReadersByFormatName("JPEG");
if (iter.hasNext()) {
reader = (ImageReader) iter.next();
} else {
System.out.println(
"No jpeg reader available!");
}
param = new ImageReadParam();
bimage = new BufferedImage(width, height, 5);
param.setDestination(bimage);
}
public BufferedImage read(String filename)
throws FileNotFoundException, IOException
{
long timea = System.currentTimeMillis();

reader.setInput(new FileImageInputStream(new File(filename)),
false, true);
int w = reader.getWidth(0);
int sampling = w / ideal_width;

param.setSourceSubsampling(sampling, sampling, 0, 0);
BufferedImage ri = reader.read(0, param);

System.out.println(
"Read took " + (System.currentTimeMillis() - timea)
+
" ms, " + filename);

return(ri);
}
public static void main(String args[]) {
try {
long timea = System.currentTimeMillis();
FastJpeg fast = new FastJpeg(320, 240);

long timeb = System.currentTimeMillis();
Image i = fast.read(
"test.jpg");
long timec = System.currentTimeMillis();

System.out.println(
"Time to read image: " + (timec - timeb)
+
", total time: " + (timec - timea));
} catch (Throwable t) {
t.printStackTrace();
}
}
}
------

import java.io.File;
  import java.io.FileOutputStream;
  import java.awt.Graphics;
  import java.awt.Image;
  import java.awt.image.BufferedImage;
  
  import com.sun.image.codec.jpeg.JPEGCodec;
  import com.sun.image.codec.jpeg.JPEGImageEncoder;
  
  public class JpgTest {
  
  public void JpgTset() throws Exception{
  File _file = new File(
"/Order005-0001.jpg"); //读入文件
  Image src = javax.imageio.ImageIO.read(_file);
//构造Image对象
  int wideth=src.getWidth(null);
//得到源图宽
  int height=src.getHeight(null);
//得到源图长
  BufferedImage tag = new BufferedImage(wideth/2,height/2,BufferedImage.TYPE_INT_RGB);
  tag.getGraphics().drawImage(src,0,0,wideth/2,height/2,null);
//绘制缩小后的图
  FileOutputStream out=new FileOutputStream(
"newfile.jpg"); //输出到文件流
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  encoder.encode(tag);
//近JPEG编码
  
//System.out.print(width+"*"+height);
  out.close();
  }
  }
  


  过程很简单,从本地磁盘读取文件Order005-0001.jpg(2032*1524),变成Image对象src,接着构造目标文件tag,设置tag的长宽为源图的一半,对tag进行编码,输出到文件流out,最后关闭文件流。
  
  还有一些问题需要说明:
  第一,目前只能支持JPG(JPEG)、GIF、PNG三种格式。
  第二,对于源图的容量有限制,最好不要超过1M,否则会抛内存不足的错误,不过我试验过1.8M的源图,可以成功,但是也很容易抛内存不足。
  
  引用一位前辈的话:图象运算本身是密集型运算,需要大量的内存存放象素值。我用VC试了一下,4M的图象也有问题,而且越是压缩比大的图片在内存中还原成BITMAP时需要的内存越大。解决的方法,可以重写编码类,先开一定的内存,然后一段一段编码写到临时文件中,输出的时候再一段一段读出来。或利用nio的内存映象来操作。JavaMail由于采用了Builder模式,先生成一个邮件的每一个部分,然后合并成一个完整的邮件对象,这样每个构件都要先生成到内存中,你如果发送一个上百兆的附件,那么在构造Part时肯定内存溢出,所以我就改写了BodyPart的构造,让他和一个临时文件关联,然后用临时文件保存Part而不是构造在内存中,这样任义大小的附件(硬盘能放得下为限)都可以发送了。
  
  最后,如果大家对图像处理有更高的要求,不妨关注一下开源项目。比如JMagick,可以使用JMagick来实现图片的复制、信息获取、斜角、特效、组合、改变大小、加边框、旋转、切片、改变格式、去色等等功能。

------

回复此文章 |

package com.hedong.JMagick;
import org.apache.log4j.*;
import java.awt.Dimension;
import java.awt.Rectangle;
import magick.ImageInfo;
import magick.MagickImage;
import magick.MagickException;
import magick.QuantizeInfo;
import magick.ColorspaceType;
import magick.MagickApiException;
import magick.PixelPacket;
import magick.DrawInfo;
import magick.ResolutionType;
import magick.ProfileInfo;
import magick.MontageInfo;
import magick.Magick;
import magick.MagickInfo;
import java.io.IOException;
import java.io.FileOutputStream;
public class PicResize {
static Logger logger = Logger.getLogger(PicResize.class.getName());
public void resize(String picFrom,String picTo) {
try {
// Resize
ImageInfo info = new ImageInfo(picFrom);
MagickImage image = new MagickImage(new ImageInfo(picFrom));
MagickImage scaled = image.scaleImage(100, 80);
//小图片文件的大小.
scaled.setFileName(picTo);
scaled.writeImage(info);
logger.info(
"alter pic form "+picFrom +" to "+picTo);
} catch(MagickApiException ex) {
logger.error(
"MagickException: " + ex + ": " + ex.getReason() + ", " + ex.getDescription());
} catch(MagickException ex) {
logger.error(
"MagickException: " + ex);
}
}
}

------

回复此文章 |
Writing Text On an Image

*** First off, I would just like to say that I’m a French Canadian student so my English
is not very
hofman   2005-11-19 22:15:07 评论:0   阅读:2719   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog