为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

生成图片代码

2017-11-13 5页 doc 21KB 45阅读

用户头像

is_321635

暂无简介

举报
生成图片代码生成图片代码 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() ...
生成图片代码
生成图片代码 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() { } }
/
本文档为【生成图片代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索