bufferedimage

Set Color as int value for use in setRGB(int x, int y, int rgb) method? — Java

跟風遠走 提交于 2019-12-02 06:07:24
Any other errors aside, I need a way of converting my Color grayScale into an int. When I plug in the Color, I get an error. setRGB method takes an x, a y, and an rgb int as parameters. How do I change my Color into an int? import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import javax.swing.*; import java.awt.image.*; import javax.imageio.ImageIO; public class Picture{ Container content; BufferedImage image, image2; public Picture(String filename) { File f = new File(filename); //assume file is the image file try { image = ImageIO.read(f); } catch (IOException

Buffered image pixel manipulation

落爺英雄遲暮 提交于 2019-12-02 05:15:35
问题 I have this code: public Image toNegative() { int imageWidth = originalImage.getWidth(); int imageHeight = originalImage.getHeight(); int [] rgb = null; // new int[imageWidth * imageWidth]; originalImage.getRGB(0, 0, imageWidth, imageHeight, rgb, 0,imageWidth); for (int y = 0; y < imageHeight; y++) { for (int x = 0; x < imageWidth; x++) { int index = y * imageWidth + x; int R = (rgb[index] >> 16) & 0xff; //bitwise shifting int G = (rgb[index] >> 8) & 0xff; int B = rgb[index] & 0xff; R = 255 -

When creating a BufferedImage from a JPanel (w/o a JFrame), can I also use a layout manager?

落花浮王杯 提交于 2019-12-02 04:47:28
问题 I am trying to create a BufferedImage from a JPanel, without using a JFrame. Yesterday, I was finally able to get the image to appear with help from this community (see below), but now I'm having some layout trouble. All of the components on the BufferedImage begin drawing at 0,0, instead of following the layout manager. Does anyone know of a solution for this problem? Yesterday's question: Can I create a BufferedImage from a JPanel without rendering in a JFrame? In the code below, the two

Resizing BufferedImages and storing them to file results in black background for JPG images

本小妞迷上赌 提交于 2019-12-02 04:44:31
问题 I have the following code: import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class JavaApplication { public static void main(String[] args) throws Exception { File orig = new File ("/home/xxx/Pictures/xxx.jpg"); BufferedImage bm1 = ImageIO.read(orig); Image scaled = bm1.getScaledInstance(100, 200, BufferedImage.SCALE_SMOOTH); BufferedImage bm2 = toBufferedImage(scaled); File resized = new File ("

BufferedImage fill rectangle with transparent pixels

北战南征 提交于 2019-12-02 04:22:42
I have a BufferedImage and I am trying to fill a rectangle with transparent pixels. The problem is, instead of replacing the original pixels, the transparent pixels just go on top and do nothing. How can I get rid of the original pixel completely? The code works fine for any other opaque colors. public static BufferedImage[] slice(BufferedImage img, int slices) { BufferedImage[] ret = new BufferedImage[slices]; for (int i = 0; i < slices; i++) { ret[i] = copyImage(img); Graphics2D g2d = ret[i].createGraphics(); g2d.setColor(new Color(255, 255, 255, 0)); for(int j = i; j < img.getHeight(); j +=

Image size getting decreased after converting it into byte[] using BufferedImage and ImageIO

非 Y 不嫁゛ 提交于 2019-12-02 04:10:02
I am converting an Image into byte[] using following code. public static byte[] extractBytes (String ImageName) throws IOException { ByteArrayOutputStream baos=new ByteArrayOutputStream(); BufferedImage img=ImageIO.read(new File(ImageName)); ImageIO.write(img, "jpg", baos); return baos.toByteArray(); } Now when I am testing my code: public static void main(String[] args) throws IOException { String filepath = "image_old.jpg"; File outp=new File(filepath); System.out.println("Size of original image="+outp.length()); byte[] data = extractBytes(filepath); System.out.println("size of byte[] data="

extending BufferedImage

廉价感情. 提交于 2019-12-02 03:12:52
Why does the following code show a black image instead of the picture? How to properly extend BufferedImage? class SizeOfImage { public static void main(String[] args) throws Exception { URL url = new URL("http://cloudbite.co.uk/wp-content/uploads/2011/03/google-chrome-logo-v1.jpg"); final BufferedImage bi = ImageIO.read(url); final String size = bi.getWidth() + "x" + bi.getHeight(); final CustomImg cstImg = new CustomImg(bi.getWidth(), bi.getHeight(), bi.getType()); SwingUtilities.invokeLater(new Runnable() { public void run() { JLabel l = new JLabel(size, new ImageIcon(cstImg),

BufferedImage rotated, change resulting background

南笙酒味 提交于 2019-12-02 03:01:34
When I rotate an image using Graphics2D.rotate() obviously it leaves some empty space in the corners. The empty corners become transparents. I want my program to rotate the BufferedImage and to fill the remaining empty corners with a white color. How do I do this? In other words, I want to rotate the image while preserving a white background for the whole image. This is my function: public BufferedImage rotateImage(BufferedImage image, double degreesAngle) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage result = new BufferedImage(w, h, image.getType()); Graphics2D g2 =

Merged two images --> 4 times the size! How do I reduce the file size?

拜拜、爱过 提交于 2019-12-02 02:23:37
问题 I merge two images using the code below. One base image without transparency, one overlay image with transparency. The file-size of the images one there own is 20kb and 5kb, respectively. Once I merged the two images, the resulting file-size is > 100kb, thus at least 4 times the combined size of 25kb. I expected a file-size less than 25kb. public static void mergeTwoImages(BufferedImage base, BufferedImage overlay, String destPath, String imageName) { // create the new image, canvas size is

BufferedImage rotated, change resulting background

一曲冷凌霜 提交于 2019-12-02 02:19:12
问题 When I rotate an image using Graphics2D.rotate() obviously it leaves some empty space in the corners. The empty corners become transparents. I want my program to rotate the BufferedImage and to fill the remaining empty corners with a white color. How do I do this? In other words, I want to rotate the image while preserving a white background for the whole image. This is my function: public BufferedImage rotateImage(BufferedImage image, double degreesAngle) { int w = image.getWidth(); int h =