Tuesday, April 29, 2008

Java 6 and java.awt.Desktop

In a current application we're writing, we've got a use case where you can upload a file, and later download it by pressing the "View" button, which will open the file in correct application for the user.

We're using Java, and I thought this would be a nasty task full of pain and suffering, but to my suprise Java 6 includes a class which does exactly this: java.awt.Desktop. Here's some sample code which will open a PDF file on my disk.

import java.awt.*;
import java.io.*;

public class Test {
public static void main(String[] args) throws Exception {
Desktop.getDesktop().open(new File("/home/nathanr/Desktop/file.pdf"));
}
}


Cool, huh? Now I'm sure you Windows / .NET people are saying "that's not hard - we've been able to do that for $x years". Sure, but the above will work on Linux and Mac too.

Sunday, April 6, 2008

Fix: F-Spot opening Gimp for RAW files on Ubuntu Hardy

In the previous post, I described how to fix Ubuntu (Hardy - 8.0.4) to get photo importing working with the F-Spot photo management application it ships with, so now to get F-Spot to use Gimp to edit the photos you've imported.

Now, it would seem that Ubuntu ships in a working way if you've imported JPEG files off your camera. However, if you've got a decent camera (ie. DSLR) you've no doubt switched your camera to RAW mode, which for me is Canon CR2. You'll find that:
  1. You can view the RAW files in Nautilus (the Gnome file manager)
  2. You can view the RAW files in F-Spot
  3. When you right click in Nautilus, you'll notice that Gimp is there and will open the file
  4. But you'll notice that when you right click on the photo in F-Spot, the "Open With" menu is blank.
The rest of this post deals with how to get Gimp to appear in the "Open With" menu for F-Spot.

Ok, so there are two things missing.
  1. The MIME type for the raw file type is missing
  2. The Gimp application description file is missing the mime type that says "I can open raw files"
But firstly, make sure you have ufraw, gimp and f-spot installed, including the gimp-ufraw package.

Now, to fix problem #1, add a file in the directory "~/.local/share/mime/packages" called "canonraw-mimetypes.xml", with the contents:
<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
<mime-type type="image/x-canon-cr2">
<comment>Canon RAW</comment>
<glob pattern="*.cr2"/>
</mime-type>
</mime-info>
Now, to fix problem #2, you need to edit the gimp.desktop file. As root, edit the file "/usr/share/applications/gimp.desktop", and on the line that begins with "MimeType=" you need to add "image/x-canon-cr2;" to the end of it.

Now, update the Gnome MIME database:
nathanr@falcon:~$ update-mime-database ~/.local/share/mime/
After logging out and logging in the "Open With" menu should include Gimp for *.cr2 files.

Note to Mac users: you can ignore this post. It's another case of "it just works on the Mac".

Fix: Importing photos on Ubuntu Hardy

A few weeks ago importing from my Canon EOS 350D to my Ubuntu machines was working, but at some point an update has broken it. I spent a bit of time this morning finding out why. (For those of you who have never tried using an Apple Mac, this stuff never happens on a Mac - things just work.) Anyway...

There are two separate stuff-ups at the moment. The first is that the pop-up that asks you whether you'd like to import your photos doesn't come up when you plug-in your camera. Don't know why this default was changed, but you can switch it back on as described in this forum post.

Ok, so now if you plug in your camera, F-Spot will pop up and say:

Received error "Could not claim the USB device" while connecting to camera
The answer is based on a forum post and a blog entry I found on the web. However, they're not 100% accurate, because the file "/etc/udev/rules.d/45-libgphoto2.rules" no longer exists. So I added the file back with the one line:
SYSFS{idVendor}=="04a9", SYSFS{idProduct}=="30ee", MODE="0660", GROUP="plugdev"

The "04a9" is the ID for Canon, and the "30ee" is the ID for the EOS 350D. You can find your IDs by the "lsusb" command:
nathanr@raptor:~$ lsusb
Bus 008 Device 004: ID 04a9:30ee Canon, Inc. EOS 350D
[...]
Look for the entry for your camera, and copy the bit before the ":" as your vendor ID, and the bit after the ":" as your product ID.

After that, and a restart of udev:
nathanr@raptor:~$ sudo /etc/init.d/udev restart
Then things started working; and once again we relearn the age old truth - Linux is only free if your time has no value. How I miss Mac OS X and Apple's iPhoto.