生成图片代码
import java.io.File;
import java.io.FileOutputStream;
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("d:\\1.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("D:\\newfile.jpg"); //输出到文件流 //File file = new File("D:\\newFile.jpg"); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//JPEGImageEncoder encoder = JPEGCodec.c encoder.encode(tag); //近JPEG编码
out.close();
}
public static void main(String[] args){
try{
new JpgTest().jpgTset();
}catch(Exception e){
e.printStackTrace();
}
}
}
//////////////////////////////////////////////////////////////
复制图片的JAVA原代码:2007-05-08 14:32import java.io.*; public class copypicture {
public static void main(String[] args){ try{FileInputStream fin=new FileInputStream("C://img.jpg");
FileOutputStream fout=new FileOutputStream("C://img1.jpg");
byte[] buf=new byte[fin.available()]; fin.read(buf);
fout.write(buf);
fin.close();
fout.close();
System.out.println("file copy success!");
}catch(IOException e){System.out.println("copy file failed!");} }
}
///////////////////////////////////////////////////////////////////////
public class Image extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public static final String SESSION_AUTHEN_CODE = "session_authen_code";
private int width = 60;
private int height = 20;
public Color getRandColor(int color1, int color2) {
Random random = new Random();
if (color1 > 255)
color1 = 255;
if (color2 > 255)
color2 = 255;
int r = color1 + random.nextInt(color2 - color1);
int g = color1 + random.nextInt(color2 - color1);
int b = color1 + random.nextInt(color2 - color1);
return new Color(r, g, b);
}
// Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
// OutputStream out = response.getOutputStream();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String sessionName = request.getParameter("authenName");
if (sessionName == null) {
sessionName = SESSION_AUTHEN_CODE;
}
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 160; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(20);
int yl = random.nextInt(20);
g.drawLine(x, y, x + xl, y + yl);
}
String chose = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
String var;
char ran[] = { '0', '0', '0', '0' }, rand;
for (int i = 0; i < 4; i++) {
rand = chose.charAt(random.nextInt(chose.length()));
ran[i] = rand;
var = String.valueOf(rand);
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(var, 13 * i + 6, 16);
}
// ServletContext application = getServletContext();
// application.setAttribute("rand",String.valueOf(ran));
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
// encoder.encode(image);
// out.close();
HttpSession session = request.getSession();
session.removeAttribute("Randnum");
session.setAttribute("Randnum", String.valueOf(ran));
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream()); }
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void destroy() {
}
}