Beispiel #1
0
  public static BufferedImage rotateImage(BufferedImage image, double theta) {
    int degrees = (int) Math.abs(Math.toDegrees(theta));
    double xCenter = image.getWidth() / 2;
    double yCenter = image.getHeight() / 2;
    AffineTransform rotateTransform = AffineTransform.getRotateInstance(-theta, xCenter, yCenter);

    // Translation adjustments so image still centered after rotate width/height changes
    if (image.getHeight() != image.getWidth() && degrees != 180 && degrees != 0) {
      Point2D origin = new Point2D.Double(0.0, 0.0);
      origin = rotateTransform.transform(origin, null);
      double yTranslate = origin.getY();

      Point2D yMax = new Point2D.Double(0, image.getHeight());
      yMax = rotateTransform.transform(yMax, null);
      double xTranslate = yMax.getX();

      AffineTransform translationAdjustment =
          AffineTransform.getTranslateInstance(-xTranslate, -yTranslate);
      rotateTransform.preConcatenate(translationAdjustment);
    }

    AffineTransformOp op = new AffineTransformOp(rotateTransform, AffineTransformOp.TYPE_BILINEAR);
    // Have to recopy image because of JDK bug #4723021, AffineTransformationOp throwing exception
    // sometimes
    image = copyImage(image, BufferedImage.TYPE_INT_ARGB);

    // Need to create filter dest image ourselves since AffineTransformOp's own dest image creation
    // throws exceptions in some cases.
    Rectangle bounds = op.getBounds2D(image).getBounds();
    BufferedImage finalImage =
        new BufferedImage(
            (int) bounds.getWidth(), (int) bounds.getHeight(), BufferedImage.TYPE_INT_ARGB);

    return op.filter(image, finalImage);
  }
Beispiel #2
0
  /**
   * @param drop
   * @param i
   * @return
   */
  public static BufferedImage rotate(final BufferedImage src, final int degree) {
    final int w = src.getWidth(null);
    final int h = src.getHeight(null);

    // final Graphics2D g = image.createGraphics();
    // g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    // RenderingHints.VALUE_ANTIALIAS_ON);

    final AffineTransform at = new AffineTransform();
    at.rotate(degree * Math.PI / 180.0);
    Point2D p2din, p2dout;

    p2din = new Point2D.Double(0.0, 0.0);
    p2dout = at.transform(p2din, null);
    double ytrans = p2dout.getY();
    double xtrans = p2dout.getX();
    p2din = new Point2D.Double(0, h);
    p2dout = at.transform(p2din, null);
    ytrans = Math.min(ytrans, p2dout.getY());
    xtrans = Math.min(xtrans, p2dout.getX());
    p2din = new Point2D.Double(w, h);
    p2dout = at.transform(p2din, null);
    ytrans = Math.min(ytrans, p2dout.getY());
    xtrans = Math.min(xtrans, p2dout.getX());
    p2din = new Point2D.Double(w, 0);
    p2dout = at.transform(p2din, null);
    ytrans = Math.min(ytrans, p2dout.getY());
    xtrans = Math.min(xtrans, p2dout.getX());

    final AffineTransform tat = new AffineTransform();
    tat.translate(-xtrans, -ytrans);

    at.preConcatenate(tat);
    final AffineTransformOp bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);

    final Rectangle r = bio.getBounds2D(src).getBounds();

    BufferedImage image = new BufferedImage(r.width, r.height, BufferedImage.TYPE_INT_ARGB);

    image = bio.filter(src, image);
    // final Graphics g = image.getGraphics();
    // g.setColor(Color.RED);
    // g.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
    // g.dispose();
    // try {
    // Dialog.getInstance().showConfirmDialog(0, "", "", new
    // ImageIcon(image), null, null);
    // } catch (final DialogClosedException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (final DialogCanceledException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    return image;
  }
Beispiel #3
0
  protected void doDrawOnTo(BufferedImageRaster canvas) {
    Sector sector = this.getSector();
    if (null == sector) {
      String message = Logging.getMessage("nullValue.SectorIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    if (!sector.intersects(canvas.getSector())) {
      return;
    }

    java.awt.Graphics2D g2d = null;
    java.awt.Shape prevClip = null;
    java.awt.Composite prevComposite = null;
    java.lang.Object prevInterpolation = null, prevAntialiasing = null;

    try {
      int canvasWidth = canvas.getWidth();
      int canvasHeight = canvas.getHeight();

      // Apply the transform that correctly maps the image onto the canvas.
      java.awt.geom.AffineTransform transform =
          this.computeSourceToDestTransform(
              this.getWidth(),
              this.getHeight(),
              this.getSector(),
              canvasWidth,
              canvasHeight,
              canvas.getSector());

      AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
      Rectangle2D rect = op.getBounds2D(this.getBufferedImage());

      int clipWidth =
          (int) Math.ceil((rect.getMaxX() >= canvasWidth) ? canvasWidth : rect.getMaxX());
      int clipHeight =
          (int) Math.ceil((rect.getMaxY() >= canvasHeight) ? canvasHeight : rect.getMaxY());

      if (clipWidth <= 0 || clipHeight <= 0) {
        return;
      }

      g2d = canvas.getGraphics();

      prevClip = g2d.getClip();
      prevComposite = g2d.getComposite();
      prevInterpolation = g2d.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
      prevAntialiasing = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);

      // Set the alpha composite for appropriate alpha blending.
      g2d.setComposite(java.awt.AlphaComposite.SrcOver);
      g2d.setRenderingHint(
          RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

      g2d.drawImage(this.getBufferedImage(), transform, null);
    }
    //        catch (java.awt.image.ImagingOpException ioe)
    //        {
    //            // If we catch a ImagingOpException, then the transformed image has a width or
    // height of 0.
    //            // This indicates that there is no intersection between the source image and the
    // canvas,
    //            // or the intersection is smaller than one pixel.
    //        }
    //        catch (java.awt.image.RasterFormatException rfe)
    //        {
    //            // If we catch a RasterFormatException, then the transformed image has a width or
    // height of 0.
    //            // This indicates that there is no intersection between the source image and the
    // canvas,
    //            // or the intersection is smaller than one pixel.
    //        }
    catch (Throwable t) {
      String reason = WWUtil.extractExceptionReason(t);
      Logging.logger().log(java.util.logging.Level.SEVERE, reason, t);
    } finally {
      // Restore the previous clip, composite, and transform.
      try {
        if (null != g2d) {
          if (null != prevClip) g2d.setClip(prevClip);

          if (null != prevComposite) g2d.setComposite(prevComposite);

          if (null != prevInterpolation)
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, prevInterpolation);

          if (null != prevAntialiasing)
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, prevAntialiasing);
        }
      } catch (Throwable t) {
        Logging.logger().log(java.util.logging.Level.FINEST, WWUtil.extractExceptionReason(t), t);
      }
    }
  }