convert GIF file to the binary text file???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • minouparhizkar
    New Member
    • Mar 2008
    • 4

    convert GIF file to the binary text file???

    hi could anyone helping me to finish this i have no idea how to implement the program in java im trying to grabbing the pixel from image and then convert it to the txt file .i did that but it didnt give me back any result

    could you tell me whats wrong with that.


    here is the pixel grabber file:

    package digitalpen;

    import java.awt.image. *;
    import java.awt.*;
    // notice here that I extends an image observer, this is
    // not necessary, but I need the ImageObserver to be passed as
    // a paramater to get the width and height of the image
    public final class MyGifPixelGrabb er implements ImageObserver
    {
    private Image m_image=null; // pointer to original image
    private Object m_pixels=null; // either array of ints or bytes
    private int m_iNumOfColors= 0;
    private int m_iWidth, m_iHeight;
    private ColorModel m_colorModel=nu ll;

    public boolean isIndexed()
    {
    if (m_colorModel== null)
    return false;
    return ((m_colorModel instanceof IndexColorModel ));
    }

    MyGifPixelGrabb er(Image img)
    {
    m_image=img;
    }

    public int getNumOfColors( )
    {
    return m_iNumOfColors;
    }

    public void destroy()
    {
    m_image=null;
    m_pixels=null;
    }

    public void grabPixels()
    {
    m_iWidth=m_imag e.getWidth(this );
    m_iHeight=m_ima ge.getHeight(th is);
    PixelGrabber pixelGrabber=ne w PixelGrabber(m_ image, 0,0,
    m_iWidth, m_iHeight, false);

    try
    {
    pixelGrabber.gr abPixels();
    }
    catch (Exception e)
    {
    System.out.prin tln("PixelGrabb er exception");
    }
    m_pixels=(Objec t)pixelGrabber. getPixels();
    // get the palette of the image
    m_colorModel=pi xelGrabber.getC olorModel();
    if (!(m_colorModel instanceof IndexColorModel ))
    {
    // not an indexed file (ie: not a gif file)
    }
    else
    {
    m_iNumOfColors= ((IndexColorMod el)m_colorModel ).getMapSize();
    }
    }

    // you'd need to cast the return values, which will be an array of bytes
    // or an array of ints. if the file is a gif file, it will return an
    // array of bytes, if jpg, you will get an array of ints
    public Object getPixels()
    {
    return m_pixels;
    }

    public int getWidth()
    {
    return m_iWidth;
    }

    public int getHeight()
    {
    return m_iHeight;
    }

    public ColorModel getColorModel()
    {
    return m_colorModel;
    }

    public int getRed(int pixel)
    {
    if ((m_colorModel instanceof IndexColorModel ))
    return ((IndexColorMod el)m_colorModel ).getRed(pixel) ;
    else
    return ((DirectColorMo del)m_colorMode l).getRed(pixel );
    }

    public int getGreen(int pixel)
    {
    if ((m_colorModel instanceof IndexColorModel ))
    return ((IndexColorMod el)m_colorModel ).getGreen(pixe l);
    else
    return ((DirectColorMo del)m_colorMode l).getGreen(pix el);
    }

    public int getBlue(int pixel)
    {
    if ((m_colorModel instanceof IndexColorModel ))
    return ((IndexColorMod el)m_colorModel ).getBlue(pixel );
    else
    return ((DirectColorMo del)m_colorMode l).getBlue(pixe l);
    }

    public int getRGB(int pixel)
    {
    if ((m_colorModel instanceof IndexColorModel ))
    return ((IndexColorMod el)m_colorModel ).getRGB(pixel) ;
    else
    return pixel;
    }

    // we need this method just because we're extending ImageObserver.
    public boolean imageUpdate(Ima ge img, int infoflags, int x, int y, int width, int height)
    {
    return true;
    }
    }


    image load:


    package digitalpen;

    import java.awt.Image;
    import java.awt.Toolki t;

    /**
    * <p>Title: </p>
    * <p>Descriptio n: </p>
    * <p>Copyright: Copyright (c) 2008</p>
    * <p>Company: </p>
    * @author not attributable
    * @version 1.0
    */

    public class imageLoad {
    private Image IMG =null;

    public imageLoad() {
    }
    public void loadImage(){
    IMG = Toolkit.getDefa ultToolkit().cr eateImage("p3.g if");
    }
    public Image extracting(){
    return IMG;
    }
    }


    and GUI

    package digitalpen;

    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*;
    import java.io.Buffere dWriter;
    import java.io.FileWri ter;
    import java.io.IOExcep tion;

    /**
    * <p>Title: </p>
    * <p>Descriptio n: </p>
    * <p>Copyright: Copyright (c) 2008</p>
    * <p>Company: </p>
    * @author not attributable
    * @version 1.0
    */

    public class GUI extends JFrame {
    JPanel contentPane;
    JButton jButton1 = new JButton();
    MyGifPixelGrabb er MGPG =null;
    Object m_pixelData=nul l;
    imageLoad IMGload = new imageLoad();
    //Construct the frame
    public GUI() {
    enableEvents(AW TEvent.WINDOW_E VENT_MASK);
    try {
    jbInit();
    }
    catch(Exception e) {
    e.printStackTra ce();
    }
    }
    //Component initialization
    private void jbInit() throws Exception {
    contentPane = (JPanel) this.getContent Pane();
    jButton1.setBou nds(new Rectangle(19, 23, 129, 40));
    jButton1.setTex t("jButton1") ;
    jButton1.addAct ionListener(new GUI_jButton1_ac tionAdapter(thi s));
    contentPane.set Layout(null);
    this.setSize(ne w Dimension(400, 300));
    this.setTitle(" Frame Title");
    contentPane.add (jButton1, null);
    }
    //Overridden so we can exit when window is closed
    protected void processWindowEv ent(WindowEvent e) {
    super.processWi ndowEvent(e);
    if (e.getID() == WindowEvent.WIN DOW_CLOSING) {
    System.exit(0);
    }
    }
    void jButton1_action Performed(Actio nEvent e) {
    IMGload.loadIma ge();
    MGPG = new MyGifPixelGrabb er(IMGload.extr acting());
    MGPG.grabPixels ();
    m_pixelData = MGPG.getPixels( );
    String log = "Log";
    if (MGPG.isIndexed ())
    {
    byte[] pixelData=(byte[])m_pixelData;
    for (int i=0; MGPG.isIndexed( ) && i<MGPG.getHeigh t(); i++)
    {
    String s="";
    for (int j=0; j<MGPG.getWidth (); j++)
    {
    if ((int)pixelData[i*MGPG.getWidth ()+j]!=0)
    s+=(int)pixelDa ta[i*MGPG.getWidth ()+j];
    else
    s+=" ";
    }
    log = log+s+"\n";
    }
    }
    try {
    BufferedWriter out = new BufferedWriter( new FileWriter("log .txt"));
    System.out.prin tln("log"+log);
    out.write(log);
    out.close();
    } catch (IOException ea) {
    }

    }

    }

    class GUI_jButton1_ac tionAdapter implements java.awt.event. ActionListener {
    GUI adaptee;

    GUI_jButton1_ac tionAdapter(GUI adaptee) {
    this.adaptee = adaptee;
    }
    public void actionPerformed (ActionEvent e) {
    adaptee.jButton 1_actionPerform ed(e);
    }
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by minouparhizkar
    hi could anyone helping me to finish this i have no idea how to implement the program in java im trying to grabbing the pixel from image and then convert it to the txt file .i did that but it didnt give me back any result

    could you tell me whats wrong with that.
    That's too much code for me to read right now. Could you post your requirements?

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Originally posted by BigDaddyLH
      That's too much code for me to read right now. Could you post your requirements?
      I should also add that your code would almost certainly be simpler and shorter if you used newer classes like BufferedImage and left behind PixelGrabber.

      Comment

      Working...