?
Solved

Saving an array to JPG or GIF file format

Posted on 2003-02-23
1
Medium Priority
?
217 Views
Last Modified: 2013-11-19
I have a program where I have to locate a sub-image within a larger image.  I open the image files and convert them to 3D arrays, manipulate the images.  I have figured out how to save to bmp, but really need to save the image to jpg format.  I can supply the image in either a 3D array [col][row][32 bit color] or 2D array [index][32 bit color] or 1D array of 32 bit colors.  How do I save any of these to a jpg file format, whatever is easier.  This may seem trivial but I am not a JAVA programmer, please help!
0
Comment
Question by:ssche2bu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
CEHJ earned 60 total points
ID: 8003235
This contains the code you'd need for that:

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Shape2Jpeg {

  public static void main(String[] args){
    JFrame f = new JFrame();
    f.getContentPane().add(new ShapePanel());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 200);
    f.setVisible(true);
  }

}

class ShapePanel extends JPanel implements MouseListener {
  boolean highlighted;
  Shape shape;
  Paint normalPaint;
  BufferedImage imageJpeg;
  Graphics2D g2dJpeg;

  public ShapePanel() {
    super();
    shape = new Rectangle2D.Float(0.f, 0.f, 100.f, 50.f);
    // Create image to save as Jpeg
    imageJpeg = new BufferedImage(shape.getBounds().width, shape.getBounds().height, BufferedImage.TYPE_INT_RGB);
    g2dJpeg = imageJpeg.createGraphics();
    normalPaint = Color.red;
    addMouseListener(this);
  }

  public void paintComponent(Graphics _g) {
    Graphics2D g = (Graphics2D)_g;
    g.setPaint(normalPaint);
    g.fill(shape);
    g.draw(shape);
  }

  public void mouseClicked(MouseEvent e) {
    // Save shape as Jpeg
    try {
      g2dJpeg.setPaint(normalPaint);
      g2dJpeg.fill(shape);
      // draw the shape to the Jpeg image
      g2dJpeg.draw(shape);
      FileOutputStream in = new FileOutputStream("shape.jpg");
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(in);
      encoder.encode(imageJpeg);
      in.close();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }



  }

  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}

}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question