コード例 #1
0
  private void createImage(OutputStream out) {
    int width = 100;
    int height = 100;

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = bi.createGraphics();

    // set background color
    g.setBackground(Color.BLUE);
    g.clearRect(0, 0, width, height);

    // set  color
    g.setColor(Color.RED);

    g.drawLine(0, 0, 99, 99);
    g.dispose();
    bi.flush();

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
    param.setQuality(1.0f, false);
    encoder.setJPEGEncodeParam(param);

    try {
      encoder.encode(bi);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
コード例 #2
0
  public static void saveJPG(Image img, String s) {
    BufferedImage bi =
        new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bi.createGraphics();
    g2.drawImage(img, null, null);

    FileOutputStream out = null;
    try {
      out = new FileOutputStream(s);
    } catch (java.io.FileNotFoundException io) {
      System.out.println("File Not Found");
    }

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
    param.setQuality(0.5f, false);
    encoder.setJPEGEncodeParam(param);

    try {
      encoder.encode(bi);
      out.close();
    } catch (java.io.IOException io) {
      System.out.println("IOException");
    }
  }
コード例 #3
0
  /**
   * @param bufInputStream
   * @param outputStream
   */
  private void writeScaledImage(BufferedInputStream bufInputStream, OutputStream outputStream) {

    long millis = System.currentTimeMillis();
    try {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();

      int bytesRead = 0;
      byte[] buffer = new byte[8192];
      while ((bytesRead = bufInputStream.read(buffer, 0, 8192)) != -1) {
        bos.write(buffer, 0, bytesRead);
      }
      bos.close();

      byte[] imageBytes = bos.toByteArray();

      Image image = Toolkit.getDefaultToolkit().createImage(imageBytes);
      MediaTracker mediaTracker = new MediaTracker(new Container());
      mediaTracker.addImage(image, 0);
      mediaTracker.waitForID(0);
      // determine thumbnail size from WIDTH and HEIGHT
      int thumbWidth = 300;
      int thumbHeight = 200;
      double thumbRatio = (double) thumbWidth / (double) thumbHeight;
      int imageWidth = image.getWidth(null);
      int imageHeight = image.getHeight(null);
      double imageRatio = (double) imageWidth / (double) imageHeight;
      if (thumbRatio < imageRatio) {
        thumbHeight = (int) (thumbWidth / imageRatio);
      } else {
        thumbWidth = (int) (thumbHeight * imageRatio);
      }
      // draw original image to thumbnail image object and
      // scale it to the new size on-the-fly
      BufferedImage thumbImage =
          new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
      Graphics2D graphics2D = thumbImage.createGraphics();
      graphics2D.setRenderingHint(
          RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
      JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
      int quality = 70;
      quality = Math.max(0, Math.min(quality, 100));
      param.setQuality((float) quality / 100.0f, false);
      encoder.setJPEGEncodeParam(param);
      encoder.encode(thumbImage);
    } catch (IOException ex) {
      log.error(ex.getMessage(), ex);
    } catch (InterruptedException ex) {
      log.error(ex.getMessage(), ex);
    } finally {
      log.debug("Time for thumbnail: " + (System.currentTimeMillis() - millis) + "ms");
    }
  }
コード例 #4
0
 /** Derived classes should implement this method and encode the input BufferedImage as needed */
 public void encodeImage(BufferedImage buf, File imageFile) throws SVGGraphics2DIOException {
   try {
     OutputStream os = new FileOutputStream(imageFile);
     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
     JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buf);
     param.setQuality(1, false);
     encoder.encode(buf, param);
     os.flush();
     os.close();
   } catch (IOException e) {
     throw new SVGGraphics2DIOException(ERR_WRITE + imageFile.getName());
   }
 }
コード例 #5
0
ファイル: ImageCompare.java プロジェクト: merterhk/HomeGuard
 // write a buffered image to a jpeg file.
 protected static void saveJPG(Image img, String filename) {
   BufferedImage bi = imageToBufferedImage(img);
   FileOutputStream out = null;
   try {
     out = new FileOutputStream(filename);
   } catch (java.io.FileNotFoundException io) {
     System.out.println("File Not Found");
   }
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
   param.setQuality(0.8f, false);
   encoder.setJPEGEncodeParam(param);
   try {
     encoder.encode(bi);
     out.close();
   } catch (java.io.IOException io) {
     System.out.println("IOException");
   }
 }
コード例 #6
0
 /**
  * ����gifͼƬ
  *
  * @param originalFile ԭͼƬ
  * @param resizedFile ���ź��ͼƬ
  * @param newWidth ���
  * @param quality ���ű��� (�ȱ���)
  * @throws IOException
  */
 private static void resize(File originalFile, File resizedFile, int newWidth, float quality)
     throws IOException {
   if (quality < 0 || quality > 1)
     throw new IllegalArgumentException("Quality has to be between 0 and 1");
   ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
   Image i = ii.getImage();
   Image resizedImage = null;
   int iWidth = i.getWidth(null);
   int iHeight = i.getHeight(null);
   if (iWidth > iHeight)
     resizedImage = i.getScaledInstance(newWidth, newWidth * iHeight / iWidth, Image.SCALE_SMOOTH);
   else
     resizedImage = i.getScaledInstance(newWidth * iWidth / iHeight, newWidth, Image.SCALE_SMOOTH);
   // This code ensures that all the pixels in the image are loaded.
   Image temp = new ImageIcon(resizedImage).getImage();
   // Create the buffered image.
   BufferedImage bufferedImage =
       new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
   // Copy image to buffered image.
   Graphics g = bufferedImage.createGraphics();
   // Clear background and paint the image.
   g.setColor(Color.white);
   g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
   g.drawImage(temp, 0, 0, null);
   g.dispose();
   // Soften.
   float softenFactor = 0.05f;
   float[] softenArray = {
     0, softenFactor, 0, softenFactor, 1 - softenFactor * 4, softenFactor, 0, softenFactor, 0
   };
   Kernel kernel = new Kernel(3, 3, softenArray);
   ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
   bufferedImage = cOp.filter(bufferedImage, null);
   // Write the jpeg to a file.
   FileOutputStream out = new FileOutputStream(resizedFile);
   // Encodes image as a JPEG data stream
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
   param.setQuality(quality, true);
   encoder.setJPEGEncodeParam(param);
   encoder.encode(bufferedImage);
 }
コード例 #7
0
ファイル: JPEGImageWriter.java プロジェクト: Revlin/batik
 /**
  * @see ImageWriter#writeImage(java.awt.image.RenderedImage, java.io.OutputStream,
  *     org.apache.batik.ext.awt.image.spi.ImageWriterParams)
  */
 public void writeImage(RenderedImage image, OutputStream out, ImageWriterParams params)
     throws IOException {
   BufferedImage bi;
   if (image instanceof BufferedImage) {
     bi = (BufferedImage) image;
   } else {
     // TODO Is this the right way?
     bi = GraphicsUtil.makeLinearBufferedImage(image.getWidth(), image.getHeight(), false);
   }
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   if (params != null) {
     JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
     if (params.getJPEGQuality() != null) {
       param.setQuality(
           params.getJPEGQuality().floatValue(), params.getJPEGForceBaseline().booleanValue());
     }
     encoder.encode(bi, param);
   } else {
     encoder.encode(bi);
   }
 }