Results 1 to 4 of 4
  1. #1
    shakall is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default How save Image BMP Binary type?

    Hello, I think a picture and saved in a BMP file with 24bit color, but it got the file size of 2Mb, I would save this file in binary format, because the color is only black and white.
    This is possible?

    I managed to do using only AWT, but as I use the IBM J9 must do the same but in SWT.

    Could someone help me?

    Thank you!

    My Codes.

    AWT
    Java Code:
    BufferedImage image= new BufferedImage(x, y, BufferedImage.TYPE_BYTE_BINARY);   
                Graphics g = image.getGraphics();   
                g.setColor(Color.white);   
                g.fillRect(0, 0, x, y);   
                g.setColor(Color.black);   
                g.drawString("TESTE DE IMPRESSAO", 20,20);   
                ImageIO.write(image, "bmp", new File(file2));
    SWT
    Java Code:
                Image imagem = new Image(display, x, y);
                GC g = new GC(imagem);
                g.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
                g.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
                g.setFont(new Font(Display.getDefault(), "DIALOG", font, SWT.NORMAL));
                g.drawString("teste de impressap", 30, 30);
                g.dispose();
                ImageData data=imagem.getImageData();
                ImageLoader loader = new ImageLoader();
                loader.data = new ImageData[]{imagem.getImageData()};
                loader.save(file, SWT.IMAGE_BMP);
                imagem.dispose();
                display.dispose();

  2. #2
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Hi,

    you should manipulate the palette.
    Try this one:
    Note: that the platette data have to be in that order to achieve the white background. Play a little with the order and see what happens.

    Java Code:
    public static void main(String[] args) {
    		PaletteData palette = new PaletteData(new RGB[] {
            		 new RGB(255,255,255), new RGB(0,0,0)
            });
    	ImageData id = new ImageData(200, 200, 1, palette);
    
    	Image imagem = new Image(Display.getCurrent(), id);
            GC g = new GC(imagem);
            g.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
            g.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
            g.setFont(new Font(Display.getDefault(), "DIALOG", 8, SWT.NORMAL));
            g.drawString("teste de impressap", 30, 30);
            g.dispose();
            ImageData data=imagem.getImageData();
            ImageLoader loader = new ImageLoader();
            loader.data = new ImageData[]{data};
            loader.save("myBMP.bmp", SWT.IMAGE_BMP);
            imagem.dispose();
            Display.getCurrent().dispose();
    	}
    further look into eclipse.org/articles/Article-SWT-images/graphics-resources.html
    there is a chapter about saving images

  3. #3
    shakall is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    Hi, my code is:

    Java Code:
     Display display = new Display();
                int x = 384, y = 100;
                org.eclipse.swt.graphics.Color white = display.getSystemColor(SWT.COLOR_WHITE);
                org.eclipse.swt.graphics.Color black = display.getSystemColor(SWT.COLOR_BLACK);
                // Create a source ImageData of depth 1 (monochrome)
                PaletteData palette = new PaletteData(new RGB[]{white.getRGB(), black.getRGB(),});
                ImageData sourceData = new ImageData(x, y, 1, palette);
                Image im = new Image(display, sourceData);
                GC gc = new GC(im);
                Font normal = new Font(Display.getDefault(), "DIALOG", font, SWT.NORMAL);
                Font bold = new Font(Display.getDefault(), "DIALOG", font, SWT.BOLD);
                gc.setFont(normal);

  4. #4
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

Similar Threads

  1. how to save image as jpg?
    By katie in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 11:07 AM
  2. How to set the background of binary image?
    By hyz_zsu in forum Java 2D
    Replies: 1
    Last Post: 04-09-2009, 04:14 AM
  3. Save image as JPEG
    By aartheesrini in forum Java Applets
    Replies: 1
    Last Post: 03-16-2009, 06:56 AM
  4. how can i save an image from url
    By happyday in forum Advanced Java
    Replies: 2
    Last Post: 01-14-2009, 08:02 PM
  5. Save a jpg image on server
    By yadavjpr in forum Java Servlet
    Replies: 1
    Last Post: 11-21-2008, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •