This site requires JavaScript, please enable it in your browser!
Greenfoot back
CooliMC
CooliMC wrote ...

2014/8/1

Converting BufferedImage to ByteArray

CooliMC CooliMC

2014/8/1

#
Hey i write a programm that takes screenshots of the whole screen and searches for special RGB but the .getRed .getBlue ... Methodes are very slow so i heard if i convert the buffered to a byte arry and then search there for the special color is much faster so my question is how can i check this byte for the special RGB and how can i convert the BufferedImage to the Byte ?? Here my Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import java.io.File;
 
public class Algorythmus
{
    static Robot Robo;
    static byte[] data;
    public static void test()
    {
        try
        {
            Robo = new Robot();
            BufferedImage originalImage = Robo.createScreenCapture(null);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(originalImage,"png", baos );
            data = baos.toByteArray();
            System.out.println(data);
        }
        catch(Exception e)
        {
             
        }
    }
}
But when i try this with a loaded 1 Pixel .png i get ervery time i run the methode different outputs.
Zamoht Zamoht

2014/8/1

#
Maybe you can use this thread .
danpost danpost

2014/8/1

#
Instead of using 'getRed', 'getBlue' and 'getGreen', there is a 'getRGB' method that returns an int value of the color. By using it, the color does not need to be broke down to its color components. If you are looking for 'r' = red, 'g' is green and 'b' = blue, then get its RGB value by using
1
int rgb = new Color(r, g, b).getRGB();
then, compare that value to the RGB values of the pixels in the image. Hope this can speed things up in an easy way.
CooliMC CooliMC

2014/8/1

#
Thanks but i think ist faster toconvert the whole picture to an byte array and then compare the bytes with the bytes of the special color is that possible ???
danpost danpost

2014/8/1

#
CooliMC wrote...
Thanks but i think ist faster toconvert the whole picture to an byte array and then compare the bytes with the bytes of the special color is that possible ???
I seriously doubt that it would be faster that way. The image would have to be iterated through to convert to bytes, then the byte array would need to be iterated through for the comparisons. With what I suggested above, you should only have to iterate through the image one time for the comparisons.
CooliMC CooliMC

2014/8/1

#
ok i tested it it is faster with your methode thanks alot
You need to login to post a reply.