/** * 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); }
// 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
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; }
// 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 } }
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); } } }
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); }
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); }
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); }
/** * 缩放图像(按高度和宽度缩放) * * @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(); } }
/** * 缩放 * * @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(); } }
/** * @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; }
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; }
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); }
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())); }
/** * 缩放图像(按高度和宽度缩放) * * @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(); } }
/* * 图片缩放 */ 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(); } }
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
// 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()); } } }
@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
/** * 生成源图像的缩影图像 * * @param pic_big_pathfilename 源图像文件名,包含路径(如 windows 下 C:\\pic.jpg ; Linux 下 * /home/abner/pic/pic.jpg ) * @param pic_small_pathfilename 生成的缩影图像文件名,包含路径(如 windows 下 C:\\pic_small.jpg ; Linux 下 * /home/abner/pic/pic_small.jpg ) * @throws JpegToolException */ public void doFinal(String pic_big_pathfilename, String pic_small_pathfilename) throws JpegToolException { if (!this.isInitFlag) { throw new JpegToolException(" 对象参数没有初始化! "); } if (pic_big_pathfilename == null || pic_small_pathfilename == null) { throw new JpegToolException(" 包含文件名的路径为空! "); } if ((!pic_big_pathfilename.toLowerCase().endsWith("jpg")) && (!pic_big_pathfilename.toLowerCase().endsWith("jpeg"))) { throw new JpegToolException(" 只能处理 JPG/JPEG 文件! "); } if ((!pic_small_pathfilename.toLowerCase().endsWith("jpg")) && (!pic_small_pathfilename.toLowerCase().endsWith("jpeg"))) { throw new JpegToolException(" 只能处理 JPG/JPEG 文件! "); } int smallw = 0; int smallh = 0; // 新建源图片和生成的小图片的文件对象 File fi = new File(pic_big_pathfilename); File fo = new File(pic_small_pathfilename); // 生成图像变换对象 AffineTransform transform = new AffineTransform(); // 通过缓冲读入源图片文件 BufferedImage bsrc = null; try { bsrc = ImageIO.read(fi); } catch (IOException ex) { throw new JpegToolException(" 读取源图像文件出错! "); } int srcw = bsrc.getWidth(); // 原图像的长度 int srch = bsrc.getHeight(); // 原图像的宽度 double scale = (double) srcw / srch; // 图像的长宽比例 if (this.smallpicwidth != 0) { // 根据设定的宽度求出长度 smallw = this.smallpicwidth; // 新生成的缩略图像的长度 smallh = (smallw * srch) / srcw; // 新生成的缩略图像的宽度 } else if (this.smallpicheight != 0) { // 根据设定的长度求出宽度 smallh = this.smallpicheight; // 新生成的缩略图像的长度 smallw = (smallh * srcw) / srch; // 新生成的缩略图像的宽度 } else if (this.picscale != 0) { // 根据设置的缩小比例设置图像的长和宽 smallw = (int) ((float) srcw * this.picscale); smallh = (int) ((float) srch * this.picscale); } else { throw new JpegToolException(" 对象参数初始化不正确! "); } System.out.println(" 源文件大小: " + srcw + "X" + srch); System.out.println(" 新文件大小: " + smallw + "X" + smallh); double sx = (double) smallw / srcw; // 小 / 大图像的宽度比例 double sy = (double) smallh / srch; // 小 / 大图像的高度比例 transform.setToScale(sx, sy); // 设置图像转换的比例 // 生成图像转换操作对象 AffineTransformOp ato = new AffineTransformOp(transform, null); // 生成缩小图像的缓冲对象 BufferedImage bsmall = new BufferedImage(smallw, smallh, BufferedImage.TYPE_3BYTE_BGR); // 生成小图像 ato.filter(bsrc, bsmall); // 输出小图像 try { ImageIO.write(bsmall, "jpeg", fo); } catch (IOException ex1) { throw new JpegToolException(" 写入缩略图像文件出错! "); } }