bufferedimage

Resizing an image in swing

泄露秘密 提交于 2019-11-30 21:55:45
I have snippet of code that I am using for the purpose of resizing an image to a curtain size (I want to change the resolution to something like 200 dpi). Basically the reason I need it is because I want to display the image that the user have picked (somewhat large) and then if the user approves I want to display the same image in a different place but using a smaller resolution. Unfortunately, if I give it a large image nothing appears on the screen. Also, if I change imageLabel.setIcon(newIcon); to imageLabel.setIcon(icon); I get the image to display but not in the correct resolution that's

How do I get the X and Y location of an image in a JScrollPane

偶尔善良 提交于 2019-11-30 20:37:48
问题 I am trying to make a Map Editor for a 2D RPG, and what I currently am trying to do (to place tiles on the ground) is to have a JScrollPane with a JLabel (that has an image in it) and a Mouse Listener attached to the JScrollPane to determine the X and Y location of the image. The problem I run into is that it doesn't get the Images X and Y location but the JScrollPanes X and Y location. So I have a JScrollPane attached to an Image that is 512x4928, I attached a mouse listener to it. the

How do I create a BufferedImage from array containing pixels?

泄露秘密 提交于 2019-11-30 19:47:41
问题 I get the pixels from BufferedImage using the method getRGB() . The pixels are stored in array called data[] . After some manipulation on data array, I need to create a BufferedImage again so that I can pass it to a module which will display the modified image, from this data array, but I am stuck with it. 回答1: BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Then set the pixels again. bufferedImage.setRGB(x, y, your_value); PS: as stated in the

Convert a 2D array of doubles to a BufferedImage

喜你入骨 提交于 2019-11-30 19:46:07
问题 I've got a two-dimensional array of doubles which is filtered values of an image. I want to convert this array back to a BufferedImage . How is it possible to cast an double[][] to a BufferedImage ? BufferedImage b = new BufferedImage(arr.length, arr[0].length, 3); Graphics c = b.getGraphics(); PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8"); for(int i=0; i< arr.length; i++){ for (int j=0; j<arr[0].length; j++){ c.drawString(String.valueOf(arr[i][j]), i, j); writer.print

How to convert BufferedImage to InputStream?

强颜欢笑 提交于 2019-11-30 18:01:12
I am uploading images using servlet. To perform resize operations i am converting InputStream to BufferedImage. Now i want to save it in mongoDB. Since, i am new to mongoDB as far as i know, GridFS takes InputStream. So, is there any way to convert BufferedImage to InputStream? You need to save the BufferedImage to a ByteArrayOutputStream using the ImageIO class , then create a ByteArrayInputStream from toByteArray() . First of all you must get your "bytes": byte[] buffer = ((DataBufferByte)(bufferedImage).getRaster().getDataBuffer()).getData(); And then use ByteArrayInputStream(byte[] buf)

How to calculate java BufferedImage filesize

眉间皱痕 提交于 2019-11-30 17:35:00
I have a servlet based application that is serving images from files stored locally. I have added logic that will allow the application to load the image file to a BufferedImage and then resize the image, add watermark text over the top of the image, or both. I would like to set the content length before writing out the image. Apart from writing the image to a temporary file or byte array, is there a way to find the size of the BufferedImage? All files are being written as jpg if that helps in calculating the size. No, you must write the file in memory or to a temporary file. The reason is

BufferedImage & createScreenCapture produces wrong colors

时间秒杀一切 提交于 2019-11-30 17:32:36
问题 In my Java program I need to analyze a color of a pixel in given coordinates. Because of the fact that I need to do it often, first I capture a part of the screen, and then get a pixel color. I am doing this with: BufferedImage bi = robot.createScreenCapture(new Rectangle(0,0,100,100)); ... pxcolor = GetPixelColor(bi,x,y); ... ImageIO.write(bi, "bmp", new File("myScreenShot.bmp")); The GetPixelColor function is quite obvious: public Color GetPixelColor(BufferedImage b, int x, int y) { int pc

Image changes color when saved with java [duplicate]

孤者浪人 提交于 2019-11-30 15:26:39
问题 This question already has answers here : JPEG image with wrong colors (8 answers) Closed 5 years ago . When I save this image: with this method: private final static Path ROOT_PATH = Paths.getPath("C:/images"); private static void saveImageFromWebSimple(final String url) { URL u = null; try { u = new URL(url); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String file = url.substring(url.indexOf("//") + 2); Path filePath = ROOT_PATH.resolve(file)

How to make gradient border of an image using java?

巧了我就是萌 提交于 2019-11-30 15:06:32
How can I make an image border as gradient. I googled a lot, but didn't find correct suggestion. Any one can help me... Any suggestion please... This is an interesting one. I first thought that there should be a simple solution, using some Graphics#drawRoundRect call with the appropriate Paint , but it's not sooo simple. However, one solution is implemented in the following example: The image is painted as-it-is into a new image. Then the edges and corners are painted. These consist of rectangles. Each rectangle for one edge is filled with a GradientPaint that interpolates between "completely

Image changes color when saved with java [duplicate]

醉酒当歌 提交于 2019-11-30 14:18:24
This question already has an answer here: JPEG image with wrong colors 7 answers When I save this image: with this method: private final static Path ROOT_PATH = Paths.getPath("C:/images"); private static void saveImageFromWebSimple(final String url) { URL u = null; try { u = new URL(url); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String file = url.substring(url.indexOf("//") + 2); Path filePath = ROOT_PATH.resolve(file); try { Files.createDirectories(filePath.getParent()); BufferedImage img = ImageIO.read(u); ImageIO.write(img, "jpg",