Skip to main content

Adding a date stamp to your digital pictures

Posted by kirillcool on October 30, 2006 at 10:17 AM PST

You can call me old-fashioned, but i prefer holding a photo album in my hands (the same goes for the books). Sure, it feels bulky and may require more energy spent to page through it, but it just feels right. Especially now with the new baby in town we take quite a lot of pictures and send them to the proud grandparents and other selected family members. How do we do it - copy the pictures to a disk-on-key, drop it at the nearest photoshop and pick up the printed pictures an hour later. The only problem is that neither our camera (one of this Pentax line, not that photoshop can add a timestamp in the bottom-right (or any for that matter) corner of the picture.

This may be fine for now, but when i look through older pictures of me and my family, it's nice to know when the pictures were taken. Of course, i could look for another photoshop, but then i can do it myself. So, without further ado, here is how you can add a date stamp in the bottom-right corner and have it not cut-off when your 4:3 file is printed on a 3:2 paper:

  public static void addDate(File inputFilethrows Exception {

    String inputFilename = inputFile.getPath();

    System.out.println("Starting with '" + inputFilename + "'");

    BufferedImage input = ImageIO.read(inputFile);

    Graphics2D graphics = (Graphics2Dinput.createGraphics();

    Date lastModified = new Date(inputFile.lastModified());

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    String dateStamp = sdf.format(lastModified);

    int fontSize = (int) (0.4 * input.getHeight() 10.0);

    Font font = new Font("Arial", Font.BOLD, fontSize);

    graphics.setFont(font);

    int height = graphics.getFontMetrics().getHeight();

    int width = graphics.getFontMetrics().stringWidth(dateStamp);



    BufferedImage dateImage = new BufferedImage(* fontSize + width, 2

        * fontSize + height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D dateGraphics = (Graphics2DdateImage.getGraphics().create();

    dateGraphics.setColor(new Color(0000));

    dateGraphics.setComposite(AlphaComposite.Src);

    dateGraphics.fillRect(00, width, height);



    dateGraphics.setComposite(AlphaComposite.SrcOver);

    dateGraphics.setFont(font);

    dateGraphics.setColor(Color.black);

    dateGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

        RenderingHints.VALUE_ANTIALIAS_ON);

    GlyphVector gv = font.createGlyphVector(dateGraphics

        .getFontRenderContext(), dateStamp);

    // dateGraphics.drawString(dateStamp, fontSize, fontSize);

    dateGraphics.translate(fontSize, * fontSize);

    for (int i = 0; i < gv.getNumGlyphs(); i++) {

      Shape glyph = gv.getGlyphOutline(i);

      dateGraphics.setStroke(new BasicStroke(fontSize / 10));

      dateGraphics.draw(glyph);

    }

    float[] kernel = new float[25];

    for (int i = 0; i < kernel.length; i++)

      kernel[i1.0f / kernel.length;

    ConvolveOp cOp = new ConvolveOp(new Kernel(55, kernel));

    BufferedImage blurred = cOp.filter(dateImage, null);

    dateGraphics.dispose();



    int iWidth = input.getWidth();

    int iHeight = input.getHeight();

    int cHeight = iWidth * 3;

    int dHeight = (iHeight - cHeight2;



    int y = (iHeight - dHeight- blurred.getHeight();

    int x = iWidth - blurred.getWidth();



    graphics.drawImage(blurred, x - fontSize, y - * fontSize, null);

    graphics.setFont(font);

    graphics.setColor(Color.white);

    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

        RenderingHints.VALUE_ANTIALIAS_ON);

    graphics.drawString(dateStamp, x, y);

    graphics.dispose();



    int lastDotIndex = inputFilename.lastIndexOf('.');

    String outputFilename = inputFilename.substring(0, lastDotIndex)

        ".new" + inputFilename.substring(lastDotIndex);

    FileOutputStream out = new FileOutputStream(outputFilename);

    /* encodes image as a JPEG data stream */

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(input);

    param.setQuality(0.9ftrue);

    encoder.setJPEGEncodeParam(param);

    encoder.encode(input);

    System.out.println("Ending with '" + inputFilename + "'");

  }

Of course, it's not a pinnacle of Java2D programming, nor was it intended to. Since i don't have any imaging-related java.net project, i'm not intending to polish it to perfection. It even uses classes in the com.sun.image.codec.jpeg package. Hereby it is released to the public domain - feel free to use, reuse and abuse it in any way - but don't come back if your nuclear reactor stops functioning because of this :)

The result is quite OK:

imagedate.png

Related Topics >>