Ejemplo n.º 1
0
 public void save(File file) throws IOException {
   if (file.getName().endsWith(".gif")) {
     ImageIO.write(_i, "png", file);
   } else if (file.getName().endsWith(".jpg")) {
     ImageIO.write(_i, "jpg", file);
   } else {
     if (!file.getName().endsWith(".png")) {
       file = new File(file.toString() + ".png");
     }
     ImageIO.write(_i, "png", file);
   }
 }
Ejemplo n.º 2
0
 /*======== public void save() ==========
 Inputs:  String filename
 Returns:
 saves the bufferedImage as a png file with the given filename
 ====================*/
 public void save(String filename) {
   try {
     File fn = new File(filename);
     ImageIO.write(bi, "png", fn);
   } catch (IOException e) {
   }
 }
Ejemplo n.º 3
0
 public void save(String filename, String type) {
   try {
     ImageIO.write(img, type, new File(filename + "." + type));
     System.out.println("Saving under " + filename + "." + type);
   } catch (Exception e) {
     System.out.println("Kann Datei " + filename + "." + type + " nicht anlegen");
   }
 }
Ejemplo n.º 4
0
  /**
   * Writes this map to a JPG file.
   *
   * @param filename the path and name of the file to create.
   */
  public void writeToJPGFile(String filename) throws IOException {

    this.prepareToDraw();

    BufferedImage buffImage =
        new BufferedImage(p.getWidth(), p.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics2D = buffImage.createGraphics();
    try {
      p.draw(graphics2D);
      System.out.println("Writing picture to " + filename);
      ImageIO.write(buffImage, "JPG", new File(filename));
    } finally {
      graphics2D.dispose();
    }
  }
Ejemplo n.º 5
0
  public void write() {
    System.out.println("Writing image");
    BufferedImage img = new BufferedImage(1600, 1200, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = img.createGraphics();

    g.setColor(Color.white);
    g.fillRect(0, 0, 1600, 1200);
    g.setColor(Color.black);
    counts = new double[depth];
    for (int i = 0; i < depth; i++) {
      counts[i] = 0;
    }
    draw(root, g, 800, 10);

    try {
      File f = new File(jpeg);
      ImageIO.write((RenderedImage) img, "jpeg", f);
    } catch (Exception e) {
      System.out.println("Error writing image data");
    }
  }
Ejemplo n.º 6
0
  // Takes a PImage and compresses it into a JPEG byte stream
  // Adapted from Dan Shiffman's UDP Sender code
  public byte[] compressImage(PImage img) {
    // We need a buffered image to do the JPG encoding
    BufferedImage bimg = new BufferedImage(img.width, img.height, BufferedImage.TYPE_INT_RGB);

    img.loadPixels();
    bimg.setRGB(0, 0, img.width, img.height, img.pixels, 0, img.width);

    // Need these output streams to get image as bytes for UDP communication
    ByteArrayOutputStream baStream = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baStream);

    // Turn the BufferedImage into a JPG and put it in the BufferedOutputStream
    // Requires try/catch
    try {
      ImageIO.write(bimg, "jpg", bos);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Get the byte array, which we will send out via UDP!
    return baStream.toByteArray();
  }
Ejemplo n.º 7
0
  // we define the method for downloading when pressing on the download button
  public void qrvidlink() {
    try {
      System.setSecurityManager(new SecurityManager());

      ipadd = tfIP.getText();
      Interface client = (Interface) Naming.lookup("rmi://" + ipadd + "/getvid");

      // Get the String entered into the TextField tfInput, convert to int
      link = tfInput.getText();
      vlink = client.getvid(link);

      // here we receive the image serialized into bytes from the server and
      // saved it on the client as png image
      byte[] bytimg = client.qrvid(vlink);
      BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytimg));
      File outputfile = new File("qrcode.png");
      ImageIO.write(img, "png", outputfile);

      // img= new ImageIcon(bytimg.toByteArray());
    } catch (Exception e) {
      System.out.println("[System] Server failed: " + e);
    }
  }
Ejemplo n.º 8
0
  protected void filePNG() {

    JFileChooser chooser = null;
    File pngFile = null;
    if (currentFile == null) {
      chooser = new JFileChooser(startDirectory);
    } else {
      pngFile = new File(currentFile.getName() + ".png");
      chooser = new JFileChooser(pngFile);
      if (!currentFile.isDirectory()) {
        chooser.setSelectedFile(currentFile);
      }
    }

    if (pngFile == null) return;
    try {
      BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
      paint(image.getGraphics());
      ImageIO.write(image, "png", pngFile);
    } catch (Exception e) {
    }
    return;
    /*		JFileChooser chooser = null;
    		File pngFile = null;
    		if (currentFile == null) {
    			chooser = new JFileChooser(startDirectory);
    		} else {
    			pngFile = new File(currentFile.getName()+".png");
    			chooser = new JFileChooser(pngFile);
    			if (!currentFile.isDirectory()) {
    				chooser.setSelectedFile(currentFile);
    			}
    		}
    		int returnVal = chooser.showSaveDialog(gw);
    		if (returnVal == JFileChooser.APPROVE_OPTION) {
    			File pngFile = chooser.getSelectedFile();

    			int maxX = Integer.MIN_VALUE;
    			int minX = Integer.MAX_VALUE;
    			int maxY = Integer.MIN_VALUE;
    			int minY = Integer.MAX_VALUE;

    			while(Node node : getNodes()) {
    				if(node.getX() > maxX) {
    					maxX = node.getX();
    				}
    				if(node.getX() < minX) {
    					minX = node.getX();
    				}
    				if(node.getY() > maxY) {
    					maxY = node.getY();
    				}
    				if(node.getY() < minY) {
    					minY = node.getY();
    				}
    			}

    			BufferedImage image = new BufferedImage(maxX-minX+50,maxY-minY+50,BufferedImage.TYPE_INT_RGB);



    			ImageIO.write(image,"png",pngFile);
    		}
    */ }
Ejemplo n.º 9
0
  public void _jspService(HttpServletRequest request, HttpServletResponse response)
      throws java.io.IOException, ServletException {

    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;

    try {
      response.setContentType("image/jpeg");
      pageContext =
          _jspxFactory.getPageContext(
              this, request, response, "/myhtml/errorpage/erroe.jsp", true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write('\r');
      out.write('\n');

      // 设置页面不缓存
      response.setHeader("Pragma", "No-cache");
      response.setHeader("Cache-Control", "no-cache");
      response.setDateHeader("Expires", 0);

      //   在内存中创建图象
      int width = 60, height = 20;
      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(new   Color());
      // g.drawRect(0,0,width-1,height-1);

      //   随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
      g.setColor(getRandColor(160, 200));
      for (int i = 0; i < 155; i++) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
        g.drawLine(x, y, x + xl, y + yl);
      }

      //   取随机产生的认证码(4位数字)
      String sRand = "";
      for (int i = 0; i < 4; i++) {
        String rand = String.valueOf(random.nextInt(10));
        sRand += rand;
        //   将认证码显示到图象中
        g.setColor(
            new Color(
                20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        // 调用函数出来的颜色相同,可能是å›
        // ä¸ºç§å­å¤ªæŽ¥è¿‘,所以只能直接生成
        g.drawString(rand, 13 * i + 6, 16);
      }

      //   将认证码存入SESSION
      session.setAttribute("rand", sRand);

      //   图象生效
      g.dispose();

      //   输出图象到页面
      ImageIO.write(image, "JPEG", response.getOutputStream());
      out.clear();
      out = pageContext.pushBody();

    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)) {
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try {
            out.clearBuffer();
          } catch (java.io.IOException e) {
          }
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else log(t.getMessage(), t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
Ejemplo n.º 10
0
  private static void decompTest(
      TJDecompressor tjd,
      byte[] jpegBuf,
      int jpegSize,
      int w,
      int h,
      int pf,
      String baseName,
      int subsamp,
      int flags,
      TJScalingFactor sf)
      throws Exception {
    String pfStr, tempstr;
    double t;
    int scaledWidth = sf.getScaled(w);
    int scaledHeight = sf.getScaled(h);
    int temp1, temp2, imgType = pf;
    BufferedImage img = null;
    byte[] dstBuf = null;

    if (yuv == YUVENCODE) return;

    if (bi) {
      pf = biTypePF(imgType);
      pfStr = biTypeStr(imgType);
    } else pfStr = pixFormatStr[pf];

    System.out.print("JPEG -> ");
    if (yuv == YUVDECODE) System.out.print("YUV " + subName[subsamp] + " ... ");
    else {
      System.out.print(pfStr + " ");
      if (bi) System.out.print("(" + pixFormatStr[pf] + ") ");
      if ((flags & TJ.FLAG_BOTTOMUP) != 0) System.out.print("Bottom-Up ");
      else System.out.print("Top-Down  ");
      if (!sf.isOne()) System.out.print(sf.getNum() + "/" + sf.getDenom() + " ... ");
      else System.out.print("... ");
    }

    t = getTime();
    tjd.setJPEGImage(jpegBuf, jpegSize);
    if (tjd.getWidth() != w || tjd.getHeight() != h || tjd.getSubsamp() != subsamp)
      throw new Exception("Incorrect JPEG header");

    temp1 = scaledWidth;
    temp2 = scaledHeight;
    temp1 = tjd.getScaledWidth(temp1, temp2);
    temp2 = tjd.getScaledHeight(temp1, temp2);
    if (temp1 != scaledWidth || temp2 != scaledHeight) throw new Exception("Scaled size mismatch");

    if (yuv == YUVDECODE) dstBuf = tjd.decompressToYUV(flags);
    else {
      if (bi) img = tjd.decompress(scaledWidth, scaledHeight, imgType, flags);
      else dstBuf = tjd.decompress(scaledWidth, 0, scaledHeight, pf, flags);
    }
    t = getTime() - t;

    if (bi) {
      tempstr =
          baseName
              + "_dec_"
              + pfStr
              + "_"
              + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD")
              + "_"
              + subName[subsamp]
              + "_"
              + (double) sf.getNum() / (double) sf.getDenom()
              + "x"
              + ".png";
      File file = new File(tempstr);
      ImageIO.write(img, "png", file);
    }

    if (yuv == YUVDECODE) {
      if (checkBufYUV(dstBuf, dstBuf.length, w, h, subsamp) == 1) System.out.print("Passed.");
      else {
        System.out.print("FAILED!");
        exitStatus = -1;
      }
    } else {
      if ((bi && checkImg(img, pf, subsamp, sf, flags) == 1)
          || (!bi
              && checkBuf(
                      dstBuf,
                      scaledWidth,
                      scaledWidth * TJ.getPixelSize(pf),
                      scaledHeight,
                      pf,
                      subsamp,
                      sf,
                      flags)
                  == 1)) System.out.print("Passed.");
      else {
        System.out.print("FAILED!");
        exitStatus = -1;
      }
    }
    System.out.format("  %.6f ms\n", t * 1000.);
  }
Ejemplo n.º 11
0
  private static int compTest(
      TJCompressor tjc,
      byte[] dstBuf,
      int w,
      int h,
      int pf,
      String baseName,
      int subsamp,
      int jpegQual,
      int flags)
      throws Exception {
    String tempstr;
    byte[] srcBuf = null;
    BufferedImage img = null;
    String pfStr;
    double t;
    int size = 0, ps, imgType = pf;

    if (bi) {
      pf = biTypePF(imgType);
      pfStr = biTypeStr(imgType);
    } else pfStr = pixFormatStr[pf];
    ps = TJ.getPixelSize(pf);

    System.out.print(pfStr + " ");
    if (bi) System.out.print("(" + pixFormatStr[pf] + ") ");
    if ((flags & TJ.FLAG_BOTTOMUP) != 0) System.out.print("Bottom-Up");
    else System.out.print("Top-Down ");
    System.out.print(" -> " + subNameLong[subsamp] + " ");
    if (yuv == YUVENCODE) System.out.print("YUV ... ");
    else System.out.print("Q" + jpegQual + " ... ");

    if (bi) {
      img = new BufferedImage(w, h, imgType);
      initImg(img, pf, flags);
      tempstr =
          baseName
              + "_enc_"
              + pfStr
              + "_"
              + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD")
              + "_"
              + subName[subsamp]
              + "_Q"
              + jpegQual
              + ".png";
      File file = new File(tempstr);
      ImageIO.write(img, "png", file);
    } else {
      srcBuf = new byte[w * h * ps + 1];
      initBuf(srcBuf, w, w * ps, h, pf, flags);
    }
    Arrays.fill(dstBuf, (byte) 0);

    t = getTime();
    tjc.setSubsamp(subsamp);
    tjc.setJPEGQuality(jpegQual);
    if (bi) {
      if (yuv == YUVENCODE) tjc.encodeYUV(img, dstBuf, flags);
      else tjc.compress(img, dstBuf, flags);
    } else {
      tjc.setSourceImage(srcBuf, w, 0, h, pf);
      if (yuv == YUVENCODE) tjc.encodeYUV(dstBuf, flags);
      else tjc.compress(dstBuf, flags);
    }
    size = tjc.getCompressedSize();
    t = getTime() - t;

    if (yuv == YUVENCODE)
      tempstr =
          baseName
              + "_enc_"
              + pfStr
              + "_"
              + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD")
              + "_"
              + subName[subsamp]
              + ".yuv";
    else
      tempstr =
          baseName
              + "_enc_"
              + pfStr
              + "_"
              + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD")
              + "_"
              + subName[subsamp]
              + "_Q"
              + jpegQual
              + ".jpg";
    writeJPEG(dstBuf, size, tempstr);

    if (yuv == YUVENCODE) {
      if (checkBufYUV(dstBuf, size, w, h, subsamp) == 1) System.out.print("Passed.");
      else {
        System.out.print("FAILED!");
        exitStatus = -1;
      }
    } else System.out.print("Done.");
    System.out.format("  %.6f ms\n", t * 1000.);
    System.out.println("  Result in " + tempstr);

    return size;
  }