Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Image BMP Binary with SWT(Save file BMP type binary image.)
Image BMP Binary with SWT [message #498455] Tue, 17 November 2009 03:24 Go to next message
Junior  is currently offline Junior Friend
Messages: 1
Registered: November 2009
Junior Member
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

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
            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(); 

Re: Image BMP Binary with SWT [message #498850 is a reply to message #498455] Wed, 18 November 2009 19:27 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi,

Here's an example snippet from Silenio that does what you want (it assumes
that the original image is black and white):

public static void main(String[] args) {
String IMAGE_FILENAME = "savedImage.bmp";
Display display = new Display();
Shell shell = new Shell(display);

final Image originalImage = new Image(display, 100, 100);
GC gc = new GC(originalImage);
gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
gc.fillOval(0, 0, 100, 100);
gc.dispose();
ImageData originalImageData = originalImage.getImageData();

ImageData newImageData = new ImageData (originalImageData.width,
originalImageData.height, 1, new PaletteData (new RGB[] {new RGB(0, 0, 0),
new RGB(255, 255, 255)}));
for (int y = 0; y < originalImageData.height; y++) {
for (int x = 0; x < originalImageData.width; x++) {
RGB rgb =
originalImageData.palette.getRGB(originalImageData.getPixel( x, y));
newImageData.setPixel(x, y, newImageData.palette.getPixel(rgb));
}
}
final Image newImage = new Image(display, newImageData);

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] {newImageData};
loader.save(IMAGE_FILENAME, SWT.IMAGE_BMP_RLE);
final Image loadedImage = new Image(display, IMAGE_FILENAME);

shell.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event event) {
event.gc.drawImage(originalImage, 10, 10);
event.gc.drawImage(newImage, 10, 120);
event.gc.drawImage(loadedImage, 10, 230);
}
});

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
loadedImage.dispose();
newImage.dispose();
originalImage.dispose();
display.dispose();
}

Grant


"Junior" <shakapp@hotmail.com> wrote in message
news:hdt528$p08$1@build.eclipse.org...
> 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
>
>
> 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
>
> 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();
>
>
Previous Topic:StyledText-AbstractDecoratedTextEditor
Next Topic:ScrollBar Position
Goto Forum:
  


Current Time: Tue Jul 07 17:23:19 GMT 2020

Powered by FUDForum. Page generated in 0.01364 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top