Rotating and saving an image

Hi, i'm trying to rotate an image and then save the rotated image to the hard drive in jpeg format. I've managed to get the rotation part working, but i can't seem to save the image correctly.

Here is some code. You'll need to catch exceptions etc.
BufferedImage expImage = new BufferedImage( (int)component.getWidth(), (int)component.getHeight(), BufferedImage.TYPE_INT_RGB );
               Graphics g2d = expImage.getGraphics();
               draftGrid.update(g2d);
               layoutGrid.update(g2d);
               g2d.dispose();
                    fileName = verifyFileName(fileName, "jpeg");
                    OutputStream out = new FileOutputStream( fileName );
                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                    encoder.encode(expImage);
                    out.flush();
                    out.close();
                    expImage.flush();
/** This draft has been saved to fileName **/

Similar Messages

  • When I have resized and saved an image in Photoshop (CS, version 8.0 for Mac) using image size, why is it so large when I open it in preview or another image viewer? Has it actually saved according to the size I specified?

    When I have resized and saved an image in Photoshop (CS, version 8.0 for Mac) using image size, why is it so large when I open it in preview or another image viewer? Has it actually saved according to the size I specified?

    You want to view the image at the Print Size, which represents the size that shows in the Image>Resize Dialog.
    View>Print Size
    Since screen resolution is almost always lower that the print resolution (somewhere between 72 and 96 usually), images will always look bigger on the screen, unless you view them at print size.
    (apple Retina displays have a much higher resolution than normal screens, closer to the average print size resolution)
    more info:
    Photoshop Help | Image size and resolution

  • Rotate and scale an image witohout deforming it

    Hello you all!
    I have to rotate and scale an image, but i don't wont to deform it.
    I'm thinking to something like 16/9 images on 4/3 screens (with two horizontal or vertical black lines around the scaled image)...
    I thinked to transform the image in bitmap format, then create a bigger image and fill the empty spaces with zero-pixels...
    Is there a simplest and more efficient way to do it with 2D java classes?
    Thank you!

    See reply 8 in Help to rotate image for an idea.

  • Is it possible to rotate and scale an image?

    Is it possible to rotate and scale an image with Grapchis2D at the same time?
    One method call to do it all?
    lets say the original image size is 200x200
    I can scale the image with
    Graphics.drawImage(image, 0,0, 500,500,this);
    But now i need to rotate it as well and keep the new size which is 500x500
    how do i do that ?

    Have you already tried the scale(double sx, double
    sy) and rotate(double theta) methods of Graphics2D?no.

  • Rotating and Translating an Image

    I am making a game with a tank that can rotate and move forwards and backwards.
    I am able to get the tank Image to rotate, but when I call a translation, it resets the image back to its original image. How can I fix this?

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.event.*;
    public class TankGame extends JPanel {
        Walker walker = new Walker(this);
        BufferedImage image;
        AffineTransform at = new AffineTransform();
        Point2D.Double loc = new Point2D.Double(200,150);
        double theta = 0;
        double t = 3.0;
        boolean goAhead = true;
        public TankGame(BufferedImage image) {
            this.image = image;
            setTransform();
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.drawRenderedImage(image, at);
            //g2.setPaint(Color.red);
            //g2.fill(new Ellipse2D.Double(loc.x-2, loc.y-2, 4, 4));
        public synchronized void step() {
            int sign = goAhead ? 1 : -1;
            double x = loc.x + sign*t*Math.cos(theta);
            double y = loc.y + sign*t*Math.sin(theta);
            loc.setLocation(x, y);
            setTransform();
            repaint();
        private void setTransform() {
            double x = loc.x - image.getWidth()/2;
            double y = loc.y - image.getHeight()/2;
            at.setToTranslation(x, y);
            at.rotate(theta, image.getWidth()/2, image.getHeight()/2);
        private JPanel getUIPanel() {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(1,0,1,0);
            gbc.weightx = 1.0;
            String[] ids = { "ahead", "stop", "back" };
            ActionListener al = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String ac = e.getActionCommand();
                    if(ac.equals("stop")) {
                        walker.stop();
                    } else {
                        if(ac.equals("ahead")) {
                            goAhead = true;
                        } else if(ac.equals("back")) {
                            goAhead = false;
                        walker.start();
            for(int j = 0; j < ids.length; j++) {
                JButton button = new JButton(ids[j]);
                button.addActionListener(al);
                if(j == ids.length-1)
                    gbc.gridwidth = GridBagConstraints.REMAINDER;
                panel.add(button, gbc);
            JSlider slider = new JSlider(-180, 180, 0);
            slider.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    int angle = ((JSlider)e.getSource()).getValue();
                    theta = Math.toRadians(angle);
                    setTransform();
                    repaint();
            gbc.gridwidth = 3;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            panel.add(slider, gbc);
            return panel;
        public static void main(String[] args) throws IOException {
            String path = "images/geek/geek----t.gif";
            BufferedImage image = ImageIO.read(new File(path));
            TankGame test = new TankGame(image);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.add(test.getUIPanel(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
    class Walker implements Runnable {
        TankGame game;
        Thread thread;
        boolean moving = false;
        long delay = 100;
        public Walker(TankGame tg) {
            game = tg;
        public void run() {
            while(moving) {
                try {
                    Thread.sleep(delay);
                } catch(InterruptedException e) {
                    moving = false;
                game.step();
        public void start() {
            if(!moving) {
                moving = true;
                thread = new Thread(this);
                thread.setPriority(Thread.NORM_PRIORITY);
                thread.start();
        public void stop() {
            moving = false;
            if(thread != null)
                thread.interrupt();
            thread = null;
    }

  • CROPPING and SAVING makes images disappear

    Cropping and saving makes my images disappear in CS5 Extended

    I have Mac OS 10.7.5 and Photoshop CS5 Extended. I cropped both a jpeg and tif file/photo
    and then saved it - save as and/or save - and the image disappeared. The only thing that was visible was the file title with .jpeg or.tif extension, and the gray box or outline that goes around the image, but also some of the images the thumbnail image was gone too - just a title was visible on the desktop screen where I saved it. Are these images gone? Where are they? Where did they go and how do I get them back? And why is my crop tool and save doing this and how do I fix it?
    I was able to make other corrections like using Curves to photos and saved them with no problem. Just the crop tool does makes the images do this after or before I save it
    Thanks for any help you can provide.

  • Rotating and saving in iPhoto?

    So I rotate and edit in iPhoto, and get it how I like it. I click done- however when I try to upload it to a website, it unrotates it and unsets the edits I've made. What is this? Is it me or the website? What can I do?

    How are you accessing the photo to upload? If you access it correctly (in the open/attach window look in the lower left under media ==> photos ==> iPhoto) you will get the current version
    click here for a discussion of correctly accessing your photos.
    LN

  • Rotating and cropping an image.

    I am taking a Photoshop for school and we are using CS5. I have CS6 and I'm stuck on my picture rotating back straight when I am trying to crop it.  I use the rotate view and rotate it 6degrees. As soon as I hit the crop button the picture goes straight. How can I keep my picture rotated to the 6degrees and crop it without the perspective crop tool?

    Rotate view only rotates...  the view; not the actual picture.  It's temporary and non-destructive. It's used to help move the canvas to a better angle for drawing brush strokes.
    You want to actually rotate the image or canvas.  Either Ctrl T for Free Transform and rotate by moving the cursor outside one of the corners and dragging.  Or Edit > Transform > Rotate

  • Rotate and save an image

    Hi All,
    I am a new comer. Could I ask you a question about java 2D? I want to rotate an image and then save it as another file, but now I don't know how to make the rotated iamge as a new one so that I can save it. Could you give me some ideas?
    Thanks,
    Regards,

    look up the Affine Transforms or use JAI.

  • Rotating and dragging of image using mouse

    hi everyone,
    I've got problems with dragging and rotating of images using the mouse
    Can anyone show me how to that?
    Thanks a million

    Implement a MouseMotionListener on the Panel that you want to drag the image on.
    public void mouseDragged  (   MouseEvent e   )
           if ( e.getModifiers() == MouseEvent.BUTTON1_MASK )
              if ( dragged )
                  processMove( new Point2D.Double( e.getPoint().x, e.getPoint().y ) );
             else
               wasDragged = true;
                  dragged    = true;
                  dragPoint  = new Point2D.Double( e.getPoint().x, e.getPoint().y );
                  xDiff      = 0;
                  yDiff      = 0;
    private void processMove
         Point2D.Double    point
              Point2D.Double paintPoint = new Point2D.Double( point.x - dragPoint.x, point.y - dragPoint.y );
           BufferedImage  imageBak   = getCurrentBackgroundImage();
           BufferedImage  background = new BufferedImage( imageBak.getWidth(), imageBak.getHeight(), imageBak.getType() );
           Graphics2D     gr         = background.createGraphics();
           gr.setColor( adaptee.getUserProfile().getBackgroundColor() );
           gr.fillRect( 0, 0, imageBak.getWidth(), imageBak.getHeight() );
           gr.drawImage( imageBak, null, (int) paintPoint.x, (int) paintPoint.y );
              gr.finalize();
           xDiff += paintPoint.x;
           yDiff += paintPoint.y;
           dragPoint = point;
           getGraphicsForPanelToDrawOn()..drawImage( background, null, 0, 0 );       
           setCurrentBackgroundImage(  background );
              }

  • Graphics2D and saving the image

    my program loads different images and display them on the screen.
    my question is how can make what is displayed on the screen saved in one jpg file?
    here is the code that loads and displays the first image:
    InputStream in = getClass().getResourceAsStream(filename);
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
    BufferedImage image = decoder.decodeAsBufferedImage();
    in.close();
    // Draw the loaded image on the offscreen image.
    g2.drawImage(image, 0, 0, this);
    the code that loads and displays the rest of the images
         for(int i=0; i<ex.length; i++){      
              String filename2 = ex[i] + ".jpg";
              //wait for the image to load
         Image img2 = getToolkit().getImage(filename2);
    loadImage(img2);
         InputStream in2 = getClass().getResourceAsStream(filename2);
         JPEGImageDecoder decoder2 = JPEGCodec.createJPEGDecoder(in2);
         BufferedImage image2 = decoder2.decodeAsBufferedImage();
         in2.close();
              g2.drawImage(image2, 50 + i*30, 120, this);
    PLEASE HELP !!!!!!:
    I already know that I need to use
    OutputStream os = new FileOutputStream("Holla.jpg");
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
    encoder.encode(imageBuffer );
    but I have many buffers here.
    Should I just add them up together into a one and encode that one?
    PLEASE HELP!!!
    Thank u.

    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    public class Example {
        public static void main(String[] args) throws IOException {
            URL url0 = new URL("http://today.java.net/jag/bio/JAG2001small.jpg");
            URL url1 = new URL("http://today.java.net/jag/bio/JagHeadshot-small.jpg");
            BufferedImage image0 = ImageIO.read(url0);
            BufferedImage image1 = ImageIO.read(url1);
            int w = image0.getWidth() + image1.getWidth();
            int h = Math.max(image0.getHeight(), image1.getHeight());
            BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = result.createGraphics();
            g.drawImage(image0, 0, 0, null);
            g.drawImage(image1, image0.getWidth(), 0, null);
            g.dispose();
            ImageIO.write(result, "jpeg", new File("result.jpeg"));
    }

  • Upside down pdf file. Rotated and save but still upsided.  How to solve this problem?

    I received a pdf file that is upside down. Rotated and saved.  When I open the file, it is still upsided.  How to solve this problem?

    Hi,
    I think you are rotating the PDFs from View and rotate. it would only change the current View of the PDF.
    Kindly click on Tools on the right hand side > Pages > Rotate. then save the PDF.
    Let me know if it helps.
    ~Pranav

  • External editing and saving

    For some reason pictures that I have edited in photoshop are no longer being saved to aperture. Before when I finished editing and saved an image it automatically went to the place where it was filed in aperture before I edited it. I am sure I have changed no settings and suddenly it stopped saving properly. I don't know how to get it set up to save the way it used to. Can anybody help? I have a project due and I can't seem to get past this.
    Thank you.

    This is probably not going to be any help, but just in case...
    Are you at any point in your Photoshop editing work saving the file with a new or revised name -- intentionally or otherwise? And then not saving to a separate Finder file from which you can subsequently import that "new" (from Aperture's perspective) image into your Aperture Library, as either a Managed or Referenced file (depending on your workflow)?
    I ask only because I frequently find myself creating a whole new file from versions of images I've sent to my External Editor (i.e. Photoshop) from Aperture -- an example being when I create a Photomerge. But also sometimes I end up wanting to save two different versions of the file while still in Photoshop. For those situations, I've created a folder on my Desktop called PICS TO IMPORT, and it's to that folder that any of those new or "Saved As" PS files get sent. Then, back in Aperture, I import those new PS files, (in my case using the "Move Files" option so they are placed into the correct sub-folders in my MASTER IMAGES folder, which my Aperture Library then references).
    The only thing I have to remember (and which I occasionally forget when I'm in a hurry) is to navigate to that "PICS TO IMPORT" folder in Photoshop's Save dialog BEFORE saving the new/revised file for the first time, otherwise it will be saved by Photoshop to the default location, which is deep within that MASTER IMAGES folder (wherever the files reside which Aperture used to create the original Exports for the External Editor). That means when I return to Aperture, I can't see the new PS file, since I still haven't officially imported it yet.
    Whereas if I just send a jpg from Aperture to my External Editor, and simply keep saving that new .psd version "as is" in Photoshop (no changes or revisions to the file name), then every time I flip back to Aperture, all my Photoshop revisions (up to my last save in PS) are reflected in the .psd file now residing in a Stack right next to the original jpg which "gave birth" to it.
    Again, I've no idea if any of this is even germane to the problem you're having, but I hope maybe it might at least spark an idea.
    Good luck!
    John Bertram
    Toronto

  • How Can I Rotate and Zoom?

    Let's say I have a circle that's a large, detailed image. I want to be able to rotate and zoom to different points on that circle. How can that be accomplished in Muse? Thanks!

    What you want to do isn't possible with just Muse if I understand what you are trying to do correctly. I am assuming you want the user to be able to rotate and zoom the image?
    If so, then Edge Animate would be the tool you need and then import that into your Muse project.

  • Help! Saving an image to stream and recreating it on client over network

    Hi,
    I have an application that uses JDK 1.1.8. I am trying to capture the UI screens of this application over network to a client (another Java app running on a PC). The client uses JDK 1.3.0. As AWT image is not serializable, I got code that converts UI screens to int[] and persist to client socket as objectoutputstream.writeObject and read the data on client side using ObjectInputStream.readObject() api. Then I am converting the int[] to an Image. Then saving the image as JPEG file using JPEG encoder codec of JDK 1.3.0.
    I found the image in black and white even though the UI screens are in color. I have the code below. I am sure JPEG encoder part is not doing that. I am missing something when recreating an image. Could be colormodel or the way I create an image on the client side. I am testing this code on a Win XP box with both server and client running on the same machine. In real scenario, the UI runs on an embedded system with pSOS with pretty limited flash space. I am giving below my code.
    I appreciate any help or pointers.
    Thanks
    Puri
         public static String getImageDataHeader(Image img, String sImageName)
             final String HEADER = "{0} {1}x{2} {3}";
             String params[] = {sImageName,
                                String.valueOf(img.getWidth(null)),
                                String.valueOf(img.getHeight(null)),
                                System.getProperty("os.name")
             return MessageFormat.format(HEADER, params);
         public static int[] convertImageToIntArray(Image img)
             if (img == null)
                 return null;
            int imgResult[] = null;
            try
                int nImgWidth = img.getWidth(null);
                int nImgHeight = img.getHeight(null);
                if (nImgWidth < 0 || nImgHeight < 0)
                    Trace.traceError("Image is not ready");
                    return null;
                Trace.traceInfo("Image size: " + nImgWidth + "x" + nImgHeight);
                imgResult = new int[nImgWidth*nImgHeight];
                PixelGrabber grabber = new PixelGrabber(img, 0, 0, nImgWidth, nImgHeight, imgResult, 0, nImgWidth);
                grabber.grabPixels();
                ColorModel model = grabber.getColorModel();
                if (null != model)
                    Trace.traceInfo("Color model is " + model);
                    int nRMask, nGMask, nBMask, nAMask;
                    nRMask = model.getRed(0xFFFFFFFF);
                    nGMask = model.getRed(0xFFFFFFFF);
                    nBMask = model.getRed(0xFFFFFFFF);
                    nAMask = model.getRed(0xFFFFFFFF);
                    Trace.traceInfo("The Red mask: " + Integer.toHexString(nRMask) + ", Green mask: " +
                                    Integer.toHexString(nGMask) + ", Blue mask: " +
                                    Integer.toHexString(nBMask) + ", Alpha mask: " +
                                    Integer.toHexString(nAMask));
                if ((grabber.getStatus() & ImageObserver.ABORT) != 0)
                    Trace.traceError("Unable to grab pixels from the image");
                    imgResult = null;
            catch(Throwable error)
                error.printStackTrace();
            return imgResult;
         public static Image convertIntArrayToImage(Component comp, int imgData[], int nWidth, int nHeight)
             if (imgData == null || imgData.length <= 0 || nWidth <= 0 || nHeight <= 0)
                 return null;
            //ColorModel cm = new DirectColorModel(32, 0xFF0000, 0xFF00, 0xFF, 0xFF000000);
            ColorModel cm = ColorModel.getRGBdefault();
            MemoryImageSource imgSource = new MemoryImageSource(nWidth, nHeight, cm, imgData, 0, nWidth);
            //MemoryImageSource imgSource = new MemoryImageSource(nWidth, nHeight, imgData, 0, nWidth);
            Image imgDummy = Toolkit.getDefaultToolkit().createImage(imgSource);
            Image imgResult = comp.createImage(nWidth, nHeight);
            Graphics gc = imgResult.getGraphics();
            if (null != gc)
                gc.drawImage(imgDummy, 0, 0, nWidth, nHeight, null);       
                gc.dispose();
                gc = null;       
             return imgResult;
         public static boolean saveImageToStream(OutputStream out, Image img, String sImageName)
             boolean bResult = true;
             try
                 ObjectOutputStream objOut = new ObjectOutputStream(out);
                int imageData[] = convertImageToIntArray(img);
                if (null != imageData)
                    // Now that our image is ready, write it to server
                    String sHeader = getImageDataHeader(img, sImageName);
                    objOut.writeObject(sHeader);
                    objOut.writeObject(imageData);
                    imageData = null;
                 else
                     bResult = false;
                objOut.flush();                
             catch(IOException error)
                 error.printStackTrace();
                 bResult = false;
             return bResult;
         public static Image readImageFromStream(InputStream in, Component comp, StringBuffer sbImageName)
             Image imgResult = null;
             try
                 ObjectInputStream objIn = new ObjectInputStream(in);
                 Object objData;
                 objData = objIn.readObject();
                 String sImageName, sSource;
                 int nWidth, nHeight;
                 if (objData instanceof String)
                     String sData = (String) objData;
                     int nIndex = sData.indexOf(' ');
                     sImageName = sData.substring(0, nIndex);
                     sData = sData.substring(nIndex+1);
                     nIndex = sData.indexOf('x');
                     nWidth = Math.atoi(sData.substring(0, nIndex));
                     sData = sData.substring(nIndex+1);
                     nIndex = sData.indexOf(' ');
                     nHeight = Math.atoi(sData.substring(0, nIndex));
                     sSource = sData.substring(nIndex+1);
                     Trace.traceInfo("Name: " + sImageName + ", Width: " + nWidth + ", Height: " + nHeight + ", Source: " + sSource);
                     objData = objIn.readObject();
                     if (objData instanceof int[])
                         int imgData[] = (int[]) objData;
                         imgResult = convertIntArrayToImage(comp, imgData, nWidth, nHeight);
                         sbImageName.setLength(0);
                         sbImageName.append(sImageName);
            catch(Exception error)
                error.printStackTrace();
             return imgResult;
         }   

    While testing more, I found that the client side is generating color UI screens if I use JDK 1.3 JVM for running the server (i.e the side that generates the img) without changing single line of code. But if I use JDK 1.1.8 JVM for the server, the client side is generating black and white versions (aka gray toned) of UI screens. So I added code to save int array that I got from PixelGrabber to a text file with 8 ints for each line in hex format. Generated these files on server side with JVM 1.1.8 and JVM 1.3. What I found is that the 1.1.8 pixel grabber is setting R,G,B components to same value where as 1.3 version is setting them to different values thus resulting in colored UI screens. I don't know why.

Maybe you are looking for

  • How to select multiple addresses from the contacts list to send an email?

    How do I select multiple addresses from the contacts list at one time to send an email. Each time I select one, the address book closes and I have to touch the "+" key to open it back to select another address. Is there a way to select all of them at

  • WPA security changeover from WEP

    I would like to increase the security level on my wireless network(air port extreme). Presently I am using WEP. What is the best NEW security level suggested and how can I most cleanly move up to, say, WPA enterprise level?

  • Dynamically updating JList.

    This is my code snippet for showing images as a preview in scrollpane. In ListTableModel, u can get the Jlist and scrollpane of the JList. setFileStats method is used to set those data (images) into the JList after making imageicon from the filepath

  • How to insert jquery in DW cs6?

    Hi there, I am trying to create an automatic fading slideshow for my company's website using dw cs6 and i'm trying not to use any plug ins. Is it not possible to load Spry Assests into the css coding in cs6? I can't seem to locate the Spry Assets fol

  • Where is the node for ETM

    Hi, I am looking for ETM Node in ECC6 ( Equipment and Tools Management.) Can I get this in ECC 6 R/3 or I need to plug in any add ons? I need to work on this in a Construction company.... Plz help in this regard. Regards, Maven.