// Button Event Edited Image Zoom Out (-)
  private void jButton4ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton4ActionPerformed

    zoomEdit = zoomEdit - 1;
    if (gambarEdit == null) {
      return;
    }
    if (zoomEdit > 0 && iEdit > 0) {
      iEdit = iEdit - 1;
      AffineTransform transform = new AffineTransform();
      transform.setToScale(zoomEdit, zoomEdit);
      AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
      BufferedImage filteredImage =
          new BufferedImage(
              gambarEdit.getWidth() / 2, gambarEdit.getHeight() / 2, gambarEdit.getType());
      op.filter(gambarEditZoomOut[iEdit], filteredImage);
      gambarEditZoomOut[iEdit] = filteredImage;
      gambarEdit = gambarEditZoomOut[iEdit];
      jLabel2.setIcon(null);
      jLabel2.setIcon(new ImageIcon(gambarEditZoomOut[iEdit]));
    } else if (iEdit <= 0) {
      iEdit = 0;
      zoomEdit = 1;
    }
  } // GEN-LAST:event_jButton4ActionPerformed
Example #2
0
 public static Icon getFixedBoundIcon(String filePath, int height, int width) throws Exception {
   double Ratio = 0.0;
   // 缩放比例
   File F = new File(filePath);
   if (!F.isFile()) throw new Exception(F + "  is not image file error in getFixedBoundIcon! ");
   Icon ret = new ImageIcon(filePath);
   BufferedImage Bi = ImageIO.read(F);
   if ((Bi.getHeight() > height) || (Bi.getWidth() > width)) {
     if (Bi.getHeight() > Bi.getWidth()) {
       Ratio = (new Integer(height)).doubleValue() / Bi.getHeight();
     } else {
       Ratio = (new Integer(width)).doubleValue() / Bi.getWidth();
     }
     int lastLength = filePath.lastIndexOf(" . ");
     String subFilePath = filePath.substring(0, lastLength);
     String fileType = filePath.substring(lastLength);
     File zoomFile = new File(subFilePath + " _ " + height + " _ " + width + fileType);
     Image Itemp = Bi.getScaledInstance(width, height, Bi.SCALE_SMOOTH);
     AffineTransformOp op =
         new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
     Itemp = op.filter(Bi, null);
     try {
       ImageIO.write((BufferedImage) Itemp, " jpg ", zoomFile);
       ret = new ImageIcon(zoomFile.getPath());
     } catch (Exception ex) {
       System.out.println(" ######## here error :  " + ex);
     }
   }
   return ret;
 }
  public byte[] getThumbNail() {
    byte[] img = this.getImage();
    byte[] out = null;
    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

    try {
      BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(img));
      int w = bufferedImage.getWidth() / 10;
      int h = bufferedImage.getHeight() / 10;
      BufferedImage after = new BufferedImage(w, h, bufferedImage.getType());
      AffineTransform at = new AffineTransform();
      at.scale(0.1, 0.1);
      AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
      after = scaleOp.filter(bufferedImage, after);
      ImageIO.write(after, "jpg", byteArrayOut);
      out = byteArrayOut.toByteArray();

    } catch (IOException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }

    return out;
  }
 protected void makeSheet() {
   if (false) { // TODO deal with sheets
     // sprite sheet is East 0, clockwise
     // direction sheet is North 0, clockwise
     /*int sheetIndex = (dir.ordinal() - Direction.EAST.ordinal() + 8) % 8;
        int soldierHeight = image.getHeight();
        if (!isAttacking()) {
     sheetIndex += 8;
        }
        image = image.getSubimage(sheetIndex * soldierHeight, 0,
     		      soldierHeight, soldierHeight);
        */
   } else {
     sprites = new BufferedImage[8];
     sprites[0] = image;
     for (int i = 1; i < 8; i++) {
       sprites[i] =
           new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
       double rotationRequired = Math.toRadians(i * 45);
       double locationX = image.getWidth() / 2;
       double locationY = image.getHeight() / 2;
       AffineTransform tx =
           AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
       AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
       // Drawing the rotated image at the required drawing locations
       sprites[i].createGraphics().drawImage(op.filter(image, null), 0, 0, null);
     }
   }
 }
  // TODO currently allows for collisions if same name & different filetype
  public void loadImage(String path) {
    BufferedImage readImage = null;
    try {
      readImage = ImageIO.read(new File("images/" + path));
    } catch (IOException e) {
      e.printStackTrace();
    }
    if (readImage != null) {
      BufferedImage aImages[] = new BufferedImage[8];
      BufferedImage image =
          new BufferedImage(
              readImage.getWidth(), readImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
      Graphics g = image.getGraphics();
      g.drawImage(readImage, 0, 0, null);
      for (int i = 0; i < 8; i++) {
        AffineTransform trans = new AffineTransform();
        trans.translate(image.getWidth() / 2, image.getHeight() / 2);
        trans.rotate(Math.PI * i / 4.0);
        trans.translate(-image.getWidth() / 2, -image.getHeight() / 2);
        AffineTransformOp op = new AffineTransformOp(trans, AffineTransformOp.TYPE_BILINEAR);
        aImages[i] =
            new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        op.filter(image, aImages[i]);
      }

      images.add(
          new Asset(
              path.substring(0, path.length() - 4), aImages)); // assumes standard 3 letter postfix
    }
  }
Example #6
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);
  }
Example #7
0
 /**
  * Simple image flipper. Can flip image either vertically, horizontally or both directions
  *
  * @param source _
  * @param flipType _
  * @return BufferedImage
  * @throws NullPointerException _
  * @throws IllegalArgumentException _
  */
 public static <T> BufferedImage flipImage(final T source, final FlipType flipType)
     throws NullPointerException, IllegalArgumentException, IOException {
   if (verifyNotNull(source, flipType)) {
     BufferedImage sourceImage = convertToBufferedImage(source);
     BufferedImage target =
         new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), sourceImage.getType());
     AffineTransform affineTransform;
     AffineTransformOp affineTransformOp;
     if (flipType.equals(FlipType.HORIZONTAL) || flipType.equals(FlipType.BOTH)) {
       affineTransform = AffineTransform.getScaleInstance(1, -1);
       affineTransform.translate(-sourceImage.getWidth(), 0);
       affineTransformOp =
           new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
       target = affineTransformOp.filter(sourceImage, target);
     }
     if (flipType.equals(FlipType.VERTICAL) || flipType.equals(FlipType.BOTH)) {
       affineTransform = AffineTransform.getScaleInstance(1, -1);
       affineTransformOp =
           new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
       affineTransform.translate(0, -sourceImage.getHeight());
       target = affineTransformOp.filter(sourceImage, target);
     }
     return target;
   }
   throw new NullPointerException(E_OBJECT_WAS_NULL);
 }
Example #8
0
 private BufferedImage rotateImage180(BufferedImage image) {
   // Flip the image vertically and horizontally; equivalent to rotating the image 180 degrees
   AffineTransform tx = AffineTransform.getScaleInstance(-1, -1);
   tx.translate(-image.getWidth(null), -image.getHeight(null));
   AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
   image = op.filter(image, null);
   return image;
 }
 /**
  * This method used for transformation of the image.
  *
  * @param scale double
  * @param srcImg BufferedImage
  * @return BufferedImage
  */
 private BufferedImage scale(double scale, BufferedImage srcImg) {
   if (scale == 1) {
     return srcImg;
   }
   AffineTransformOp op =
       new AffineTransformOp(AffineTransform.getScaleInstance(scale, scale), null);
   return op.filter(srcImg, null);
 }
Example #10
0
 public BufferedImage getRotate() {
   AffineTransformOp atop =
       new AffineTransformOp(
           AffineTransform.getRotateInstance(
               3.141592653589793D, this.image.getWidth() / 2, this.image.getHeight() / 2),
           1);
   return this.image = atop.filter(this.image, null);
 }
Example #11
0
 /**
  * 缩放图像(按高度和宽度缩放)
  *
  * @param srcImageFile 源图像文件地址
  * @param result 缩放后的图像地址
  * @param height 缩放后的高度
  * @param width 缩放后的宽度
  * @param bb 比例不对时是否需要补白:true为补白; false为不补白;
  */
 @SuppressWarnings("static-access")
 public static final void scale2(
     String srcImageFile, String result, int height, int width, boolean bb) {
   try {
     double ratio = 0.0; // 缩放比例
     File f = new File(srcImageFile);
     BufferedImage bi = ImageIO.read(f);
     Image itemp =
         bi.getScaledInstance(
             width, height, bi.SCALE_SMOOTH); // bi.SCALE_SMOOTH  选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。
     // 计算比例
     if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
       if (bi.getHeight() > bi.getWidth()) {
         ratio = (new Integer(height)).doubleValue() / bi.getHeight();
       } else {
         ratio = (new Integer(width)).doubleValue() / bi.getWidth();
       }
       AffineTransformOp op =
           new AffineTransformOp(
               AffineTransform // 仿射转换
                   .getScaleInstance(ratio, ratio),
               null); // 返回表示剪切变换的变换
       itemp = op.filter(bi, null); // 转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
     }
     if (bb) { // 补白
       BufferedImage image =
           new BufferedImage(
               width, height, BufferedImage.TYPE_INT_RGB); // 构造一个类型为预定义图像类型之一的 BufferedImage。
       Graphics2D g = image.createGraphics(); // 创建一个 Graphics2D,可以将它绘制到此 BufferedImage 中。
       g.setColor(Color.white); // 控制颜色
       g.fillRect(0, 0, width, height); // 使用 Graphics2D 上下文的设置,填充 Shape 的内部区域。
       if (width == itemp.getWidth(null))
         g.drawImage(
             itemp,
             0,
             (height - itemp.getHeight(null)) / 2,
             itemp.getWidth(null),
             itemp.getHeight(null),
             Color.white,
             null);
       else
         g.drawImage(
             itemp,
             (width - itemp.getWidth(null)) / 2,
             0,
             itemp.getWidth(null),
             itemp.getHeight(null),
             Color.white,
             null);
       g.dispose();
       itemp = image;
     }
     ImageIO.write((BufferedImage) itemp, "JPEG", new File(result));
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Example #12
0
  /**
   * 缩放
   *
   * @param filePath 图片路径
   * @param height 高度
   * @param width 宽度
   * @param bb 比例不对时是否需要补白
   */
  public static void resize(String filePath, int height, int width, boolean bb) {
    try {
      double ratio = 0.0; // 缩放比例
      File f = new File(filePath);
      BufferedImage bi = ImageIO.read(f);
      /*AffineTransform affineTransform = new AffineTransform();
      affineTransform.scale(width, height);
      RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_DEFAULT);

      AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, hints);
      BufferedImage image = new BufferedImage(width, height, bi.getType());
      image = affineTransformOp.filter(bi, image);*/
      Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
      // 计算比例
      if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
        if (bi.getHeight() > bi.getWidth()) {
          ratio = (new Integer(height)).doubleValue() / bi.getHeight();
        } else {
          ratio = (new Integer(width)).doubleValue() / bi.getWidth();
        }
        AffineTransformOp op =
            new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
        itemp = op.filter(bi, null);
      }
      if (bb) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, width, height);
        if (width == itemp.getWidth(null))
          g.drawImage(
              itemp,
              0,
              (height - itemp.getHeight(null)) / 2,
              itemp.getWidth(null),
              itemp.getHeight(null),
              Color.white,
              null);
        else
          g.drawImage(
              itemp,
              (width - itemp.getWidth(null)) / 2,
              0,
              itemp.getWidth(null),
              itemp.getHeight(null),
              Color.white,
              null);
        g.dispose();
        itemp = image;
      }
      ImageIO.write((BufferedImage) itemp, "jpg", f);
      // ImageIO.write(image, "jpg", f);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Example #13
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;
  }
Example #14
0
  public static String getImage(String json, int numOfImages, String imagesIds, String path) {
    System.out.println("entrou no service");
    ArrayList<byte[]> imagesList = new ArrayList<byte[]>();
    Gson gson = new GsonBuilder().create();
    DeviceSpecifications ds = gson.fromJson(json, DeviceSpecifications.class);
    int width = ds.getWidth();
    int height = ds.getHeight();
    if (ds.getScreenSize() < 3.5) {
      width = width / 2;
      height = height / 2;
    }
    if (ds.getAvaiableMemory() < 52428800) { // 52428800==50mb
      width = width / 2;
      height = height / 2;
    }

    System.out.println("Connection type: " + ds.getConnectionType());
    if (ds.getConnectionType() == ConnectionType.DATA_CONNECTION) {
      width = width / 4;
      height = height / 4;
    }

    System.out.println("Battery percent: " + ds.getBatteryAvailablePercent() + "%");
    if (ds.getBatteryAvailablePercent() < 30) {
      width = width / 2;
      height = height / 2;
    }

    String[] images = imagesIds.split(",");

    try {
      for (int i = 0; i < numOfImages; i++) {
        System.out.println("CAMINHO DA IMAGEM: " + path + images[i]);
        File file = new File(path + images[i]);
        BufferedImage requestedImage = ImageIO.read(file);
        int imageWidth = requestedImage.getWidth();
        int imageHeight = requestedImage.getHeight();
        double scaleX = (double) width / imageWidth;
        double scaleY = (double) height / imageHeight;
        BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        AffineTransform at = new AffineTransform();
        at.scale(scaleX, scaleY);
        AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        scaledImage = scaleOp.filter(requestedImage, scaledImage);

        byte[] adaptedImage = imageToArray(scaledImage);
        imagesList.add(adaptedImage);
      }
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    String jsonResponse = gson.toJson(imagesList, new TypeToken<ArrayList<byte[]>>() {}.getType());
    return jsonResponse;
  }
Example #15
0
 static BufferedImage scaleImage(BufferedImage before, float scale) {
   int w = (int) (before.getWidth() * scale);
   int h = (int) (before.getHeight() * scale);
   BufferedImage after = new BufferedImage(w, h, before.getType());
   AffineTransform at = new AffineTransform();
   at.scale(scale, scale);
   AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
   after = scaleOp.filter(before, after);
   System.out.println("Scaled from " + before.getHeight() + "px to " + after.getHeight() + "px");
   return after;
 }
  /**
   * Rotate an image using an AffineTransform.
   *
   * @param timg The image you want to rotate
   * @param degrees Degrees to rotate
   * @return The rotated image
   */
  public BufferedImage rotate(BufferedImage timg, double degrees) {
    AffineTransform xform = new AffineTransform();

    xform.setToTranslation(0.5 * timg.getWidth(), 0.5 * timg.getHeight());
    xform.rotate(degrees);
    xform.translate(-0.5 * timg.getHeight(), -0.5 * timg.getWidth());

    AffineTransformOp op = new AffineTransformOp(xform, AffineTransformOp.TYPE_BILINEAR);

    return op.filter(timg, null);
  }
Example #17
0
  public static BufferedImage getScaledImage(BufferedImage image, int width, int height)
      throws IOException {
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();
    double scaleX = (double) width / imageWidth;
    double scaleY = (height != -1) ? (double) height / imageHeight : scaleX;
    if (height == -1) height = (int) (image.getHeight() * scaleX);
    AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    AffineTransformOp bilinearScaleOp =
        new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);

    return bilinearScaleOp.filter(image, new BufferedImage(width, height, image.getType()));
  }
Example #18
0
 public void transformImage(
     SunGraphics2D sg, BufferedImage img, BufferedImageOp op, int x, int y) {
   if (op != null) {
     if (op instanceof AffineTransformOp) {
       AffineTransformOp atop = (AffineTransformOp) op;
       transformImage(sg, img, x, y, atop.getTransform(), atop.getInterpolationType());
       return;
     } else {
       img = op.filter(img, null);
     }
   }
   copyImage(sg, img, x, y, null);
 }
Example #19
0
 /**
  * 缩放图像(按高度和宽度缩放)
  *
  * @param srcImageFile 源图像文件地址
  * @param result 缩放后的图像地址
  * @param height 缩放后的高度
  * @param width 缩放后的宽度
  * @param bb 比例不对时是否需要补白:true为补白; false为不补白;
  */
 public static final void scale2(
     String srcImageFile, String result, int height, int width, boolean bb) {
   try {
     double ratio = 0.0; // 缩放比例
     File f = new File(srcImageFile);
     BufferedImage bi = ImageIO.read(f);
     Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
     // 计算比例
     if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
       if (bi.getHeight() > bi.getWidth()) {
         ratio = (new Integer(height)).doubleValue() / bi.getHeight();
       } else {
         ratio = (new Integer(width)).doubleValue() / bi.getWidth();
       }
       AffineTransformOp op =
           new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
       itemp = op.filter(bi, null);
     }
     if (bb) { // 补白
       BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
       Graphics2D g = image.createGraphics();
       g.setColor(Color.white);
       g.fillRect(0, 0, width, height);
       if (width == itemp.getWidth(null))
         g.drawImage(
             itemp,
             0,
             (height - itemp.getHeight(null)) / 2,
             itemp.getWidth(null),
             itemp.getHeight(null),
             Color.white,
             null);
       else
         g.drawImage(
             itemp,
             (width - itemp.getWidth(null)) / 2,
             0,
             itemp.getWidth(null),
             itemp.getHeight(null),
             Color.white,
             null);
       g.dispose();
       itemp = image;
     }
     ImageIO.write((BufferedImage) itemp, "JPEG", new File(result));
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Example #20
0
 /*
  * 图片缩放
  */
 public static void zoomImage(String src, String dest, int w, int h) throws Exception {
   double wr = 0, hr = 0;
   File srcFile = new File(src);
   File destFile = new File(dest);
   BufferedImage bufImg = ImageIO.read(srcFile);
   Image Itemp = bufImg.getScaledInstance(w, h, bufImg.SCALE_SMOOTH);
   wr = w * 1.0 / bufImg.getWidth();
   hr = h * 1.0 / bufImg.getHeight();
   AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
   Itemp = ato.filter(bufImg, null);
   try {
     ImageIO.write((BufferedImage) Itemp, dest.substring(dest.lastIndexOf(".") + 1), destFile);
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }
Example #21
0
 public static boolean createThumbnail(
     String fromFileStr,
     String saveToFileStr,
     String sysimgfile,
     String suffix,
     int width,
     int height)
     throws Exception {
   double Ratio = 1.0;
   File F = new File(fromFileStr);
   if (!F.isFile()) throw new Exception(F + " is not image file error in CreateThumbnail!");
   File ThF = new File(saveToFileStr, sysimgfile + "." + suffix);
   BufferedImage Bi = ImageIO.read(F);
   Image Itemp = Bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
   /*        if ((Bi.getHeight() > width) || (Bi.getWidth() > height)) {
       if (Bi.getHeight() > Bi.getWidth())
           Ratio = (double)width / Bi.getHeight();
       else
           Ratio = (double)height / Bi.getWidth();
   }  */
   // Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
   int imgwidth = Bi.getWidth();
   int imgheight = Bi.getHeight();
   int newheight = imgheight;
   int newwidth = imgwidth;
   // 以比例小的为基准
   if (imgwidth / width > imgheight / height) { // 以height为基准
     newheight = height;
     newwidth = imgwidth * height / imgheight;
     Ratio = (new Integer(height)).doubleValue() / imgheight;
   } else { // 以width为基准
     newwidth = width;
     newheight = imgheight * width / imgwidth;
     Ratio = (new Integer(width)).doubleValue() / imgwidth;
   }
   AffineTransformOp op =
       new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);
   Itemp = op.filter(Bi, null);
   try {
     ImageIO.write((BufferedImage) Itemp, suffix, ThF);
   } catch (Exception ex) {
     throw new Exception(" ImageIo.write error in CreatThum.: " + ex.getMessage());
   }
   return (true);
 }
  // Button Event Edited Image Rotate
  private void jButton6ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton6ActionPerformed

    if (gambarEdit == null) {
      return;
    }
    AffineTransform transform =
        AffineTransform.getRotateInstance(
            Math.toRadians(-90), gambarEdit.getWidth() / 2, gambarEdit.getHeight() / 2);
    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
    BufferedImage filteredImage =
        new BufferedImage(gambarEdit.getWidth(), gambarEdit.getHeight(), gambarEdit.getType());
    op.filter(gambarEdit, filteredImage);
    gambarEdit = filteredImage;
    gambarEditZoomOut[iEdit] = gambarEdit;
    jLabel2.setIcon(null);
    jLabel2.setIcon(new ImageIcon(gambarEdit));
  } // GEN-LAST:event_jButton6ActionPerformed
  // Button Event Original Image Zoom In (+)
  private void jButton1ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton1ActionPerformed

    zoom = zoom + 1;
    if (gambar == null) {
      return;
    }
    gambarAsliZoomOut[iOri] = gambar;
    AffineTransform transform = new AffineTransform();
    transform.setToScale(zoom, zoom);
    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
    BufferedImage filteredImage =
        new BufferedImage(gambar.getWidth() * zoom, gambar.getHeight() * zoom, gambar.getType());
    op.filter(gambar, filteredImage);
    gambar = filteredImage;
    iOri = iOri + 1;
    jLabel1.setIcon(null);
    jLabel1.setIcon(new ImageIcon(gambar));
  } // GEN-LAST:event_jButton1ActionPerformed
Example #24
0
  // IUavPainter_BEGIN
  @SuppressWarnings("unchecked")
  @Override
  public void paint(Graphics2D g, int width, int height, Object args) {

    receivedArgs = (Hashtable<String, Object>) args;

    if (!receivedArgs.isEmpty()) {

      frameImage = (BufferedImage) receivedArgs.get("image");
      rotation = (Integer) receivedArgs.get("image rotation");

      AffineTransform tx = new AffineTransform();
      tx.rotate(Math.toRadians(rotation), frameImage.getWidth() / 2, frameImage.getHeight() / 2);

      AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
      frameImage = op.filter(frameImage, null);

      g.drawImage(frameImage, 0, 0, width, height, null);
    }
  }
  public void paintComponent(Graphics g) {
    if (background != null) {
      float xRatio = (float) tk.getScreenSize().width / stream.getWidth();
      float yRatio = (float) tk.getScreenSize().height / stream.getHeight();
      int StreamWidth = stream.getWidth();
      int screenHeight = tk.getScreenSize().height;
      int screenWidth = tk.getScreenSize().width;
      int blockWidth = screenWidth / 3;
      int blockHeight = screenHeight / 2;

      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

      AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
      tx.scale(xRatio, yRatio);
      tx.translate(-StreamWidth, 0);

      AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
      background = op.filter(background, null);

      g.drawImage(background, 0, 0, null);

      if (x) {
        g.setFont(new Font("Arial", Font.BOLD, 20));
        g.setColor(new Color(255, 255, 255, 255));
        g.drawString("HammerTime", blockWidth / 5, blockHeight / 2);
        g.drawString("Mix 01", blockWidth / 3 + blockWidth, blockHeight / 4);
        g.drawString("Mix 03", blockWidth / 3 + (blockWidth * 2), blockHeight / 4);
        g.drawString("Record Loop", blockWidth / 4, blockHeight + (blockHeight / 4));
        g.drawString(recStatus, blockWidth / 4, blockHeight + (blockHeight / 2));
        g.drawString("Mix 02", blockWidth / 3 + blockWidth, blockHeight / 4 + blockHeight);
        g.drawString("Mix 04", blockWidth / 3 + (blockWidth * 2), blockHeight / 4 + blockHeight);
        g.drawLine(blockWidth, 0, (blockWidth) + 3, screenHeight);
        g.drawLine(2 * blockWidth, 0, (2 * blockWidth) + 3, screenHeight);
        g.drawLine(0, blockHeight, screenWidth, blockHeight + 3);
      } else {
        g.drawLine(blockWidth, 0, (blockWidth) + 2, stream.getHeight());
        g.drawLine(blockWidth + 200, 0, (blockWidth) + 202, stream.getHeight());
      }
    }
  }
Example #26
0
 @Override
 public void paintComponent(Graphics g) {
   g.setColor(getBackground());
   g.fillRect(0, 0, getWidth(), getHeight());
   int w = bufferedImage.getWidth(this);
   int h = bufferedImage.getHeight(this);
   if (mode == Flip.NONE) {
     g.drawImage(bufferedImage, 0, 0, w, h, this);
   } else if (mode == Flip.VERTICAL) {
     AffineTransform at = AffineTransform.getScaleInstance(1d, -1d);
     at.translate(0, -h);
     Graphics2D g2 = (Graphics2D) g.create();
     g2.drawImage(bufferedImage, at, this);
     g2.dispose();
   } else if (mode == Flip.HORIZONTAL) {
     AffineTransform at = AffineTransform.getScaleInstance(-1d, 1d);
     at.translate(-w, 0);
     AffineTransformOp atOp = new AffineTransformOp(at, null);
     g.drawImage(atOp.filter(bufferedImage, null), 0, 0, w, h, this);
   }
 }
  public BufferedImage transformImage(int maxWidth, int maxHeight, int maxSquare) {
    final BufferedImage image = getImage();
    double scale =
        Math.min((double) maxWidth / image.getWidth(), (double) maxHeight / image.getHeight());

    if (scale > 1) {
      scale = 1.0;
    }
    int width = (int) (Math.round(scale * image.getWidth()));
    int height = (int) (Math.round(scale * image.getHeight()));

    while (width * height > maxSquare) {
      scale = scale * 0.9;
      width = (int) (Math.round(scale * image.getWidth()));
      height = (int) (Math.round(scale * image.getHeight()));
    }

    final AffineTransformOp op1 =
        new AffineTransformOp(
            AffineTransform.getScaleInstance(scale, scale),
            AffineTransformOp.TYPE_NEAREST_NEIGHBOR);

    final BufferedImage scaledImage =
        op1.filter(image, new BufferedImage(width, height, image.getType()));

    Palette pal = Palette.getRainbowPalette();

    final IndexColorModel BINARY_COLOR_MODEL = new IndexColorModel(8, 256, pal.R, pal.G, pal.B, -1);

    final int w = scaledImage.getWidth();
    final int h = scaledImage.getHeight();

    final BufferedImage bufferedImage =
        new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, BINARY_COLOR_MODEL);

    bufferedImage.getGraphics().drawImage(scaledImage, 0, 0, null);

    return bufferedImage;
  }
  // Button Event Edited Image Zoom In (+)
  private void jButton3ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton3ActionPerformed

    zoomEdit = zoomEdit + 1;
    if (gambarEdit == null) {
      return;
    }
    gambarEditZoomOut[iEdit] = gambarEdit;
    AffineTransform transform = new AffineTransform();
    transform.setToScale(zoomEdit, zoomEdit);
    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
    BufferedImage filteredImage =
        new BufferedImage(
            gambarEdit.getWidth() * zoomEdit,
            gambarEdit.getHeight() * zoomEdit,
            gambarEdit.getType());
    op.filter(gambarEdit, filteredImage);
    gambarEdit = filteredImage;
    iEdit = iEdit + 1;
    jLabel2.setIcon(null);
    jLabel2.setIcon(new ImageIcon(gambarEdit));
  } // GEN-LAST:event_jButton3ActionPerformed
 public void imageScaling() {
   try {
     File fi = new File(file_path); // 大圖文件
     File fo = new File(file_path + ".jpg"); // 將要轉換出的小圖文件
     int nw = 100;
     AffineTransform transform = new AffineTransform();
     BufferedImage bis = ImageIO.read(fi);
     int w = bis.getWidth();
     int h = bis.getHeight();
     double scale = (double) w / h;
     int nh = (nw * h) / w;
     double sx = (double) nw / w;
     double sy = (double) nh / h;
     transform.setToScale(sx, sy);
     System.out.println(w + " " + h);
     AffineTransformOp ato = new AffineTransformOp(transform, null);
     BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
     ato.filter(bis, bid);
     ImageIO.write(bid, "jpeg", fo);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  // Button Event Original Image Zoom Out (-)
  private void jButton2ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton2ActionPerformed

    zoom = zoom - 1;
    if (gambar == null) {
      return;
    }
    if (zoom > 0 && iOri > 0) {
      iOri = iOri - 1;
      AffineTransform transform = new AffineTransform();
      transform.setToScale(zoom, zoom);
      AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
      BufferedImage filteredImage =
          new BufferedImage(gambar.getWidth() / 2, gambar.getHeight() / 2, gambar.getType());
      op.filter(gambarAsliZoomOut[iOri], filteredImage);
      gambarAsliZoomOut[iOri] = filteredImage;
      gambar = gambarAsliZoomOut[iOri];
      jLabel1.setIcon(null);
      jLabel1.setIcon(new ImageIcon(gambarAsliZoomOut[iOri]));
    } else if (iOri <= 0) {
      iOri = 0;
      zoom = 1;
    }
  } // GEN-LAST:event_jButton2ActionPerformed