drawImage

来源:互联网 发布:移动软件开发是什么 编辑:程序博客网 时间:2024/05/21 22:57

使用方法:

1、

<!DOCTYPE html>
<html>
<body>

<p>要使用的图像:</p>
<img id="tulip" src="/i/eg_tulip.jpg" alt="The Tulip" />

<p>画布:</p>
<canvas id="myCanvas" width="500" height="300" style="border:1px solid #d3d3d3;background:#ffffff;">
Your browser does not support the HTML5 canvas tag.
</canvas>

<script>

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var img=document.getElementById("tulip");
ctx.drawImage(img,5,5,120,80);

</script>

</body>
</html>


2、

 /**
    * 把图片印刷到图片上
    * 
    * @param pressImg --
    *            水印文件
    * @param targetImg --
    *            目标文件
    * @param x
    *            --x坐标
    * @param y
    *            --y坐标
    */
   public final static void pressImage(String pressImg, String targetImg,
           int x, int y) {
       try {
           //目标文件
           File _file = new File(targetImg);
           Image src = ImageIO.read(_file);
           int wideth = src.getWidth(null);
           int height = src.getHeight(null);
           BufferedImage image = new BufferedImage(wideth, height,
                   BufferedImage.TYPE_INT_RGB);         //建立一个图片缓存

           Graphics g = image.createGraphics();      //获取图片缓存的Graphics对象 
           g.drawImage(src, 0, 0, wideth, height, null);


           //水印文件
           File _filebiao = new File(pressImg);
           Image src_biao = ImageIO.read(_filebiao);
           int wideth_biao = src_biao.getWidth(null);
           int height_biao = src_biao.getHeight(null);
           //g.drawImage(src_biao, (wideth - wideth_biao) / 2,(height - height_biao) / 2, wideth_biao, height_biao, null);
           g.drawImage(src_biao, x, y, wideth_biao, height_biao, null);  
           //水印文件结束
           g.dispose();
           FileOutputStream out = new FileOutputStream(targetImg);       // 获取响应的输出流
           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   //获取JPEG编码器

           encoder.encode(image);   //JPEG编码图片并且输出到响应。

           out.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }


3、关于图片处理的解释

response.setContentType("image/jpeg");  //设置响应类型是jpeg图片 BufferedImage image = new BufferedImage(60,20,BufferedImage.TYPE_INT_RGB);  //建立一个图片缓存 Graphics g = image.getGraphics(); //获取图片缓存的Graphics对象 Random r = new Random(); //获取随机数对象 g.setColor(new Color(r.nextInt(255),r.nextInt(255), r.nextInt(255))); //设置图片充填颜色白色 g.fillRect(0, 0, 60, 20);//绘制一个矩形 g.setColor(new Color(0,0,0)); //设置充填颜色黑色   String number = String.valueOf( r.nextInt(99999)); //生成一个0-99999的随机数HttpSession session = request.getSession();  //获取用户的Session session.setAttribute("number", number); //给用户的Session中添加number属性 值是刚才生成的随机数g.drawString(number, 5, 15); //把生成的随机数画到图片上 g.setColor( new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));//设置图片充填颜色白色 g.drawLine(r.nextInt(60),r.nextInt(20),r.nextInt(60), r.nextInt(20));//在随机的位置画上白色线条干扰OCR识别OutputStream os =response.getOutputStream(); // 获取响应的输出流JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(os); //获取JPEG编码器 encoder.encode(image);//JPEG编码图片并且输出到响应。

0 0