Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: moved anchors above the titles so titles appear at the top when clicked on.

...

  1. #IRequestTargetUrlCodingStrategy which is mounted and returns the contextual image for a fixed path.
  2. #Resource which wraps the image creation logic but where the URL is auto-generated by wicket.

Anchor
IRequestTargetUrlCodingStrategy
IRequestTargetUrlCodingStrategy

Create custom IRequestTargetUrlCodingStrategy and bind it to the /surveyed-date.png Image

...

IRequestTargetUrlCodingStrategyOverview:

  1. mount the custom IRequestTargetUrlCodingStrategy in application.init();
    1. My custom strategy is hardcoded with the name of the image that it responds to: /surveyed-date.png
  2. When ever a request is sent to that image the strategy will either returned a cached copy or generate a new image by stamping the current date onto a template image.
    1. The page that places the image will have an image link with the
      Code Block
      <img src="/surveyed-date.png" />
      HTML tag.

...

The tripTimeService.getImageResource(surveyedDate, true) method is what does the work of converting the date into a particular format and then writing the text onto an image template.

Anchor
stampedImage
stampedImage

How to create an image stamped with user specific context

...

...

Code Block
public class TripTimeImageServiceImpl implements TripTimeImageService {

	private static final Logger										log				= Logger
																							.getLogger(TripTimeImageServiceImpl.class);


        // spring resource that points at the template image 
	private Resource												baseTripTimeImageFile;

	private DateTimeFormatter										dayOfWeekFormatter;

	private DateTimeFormatter										dateFormatter;

// Font to use to write the string version of the date onto the image template
	private static Font												font			= new Font(
																							Font.SANS_SERIF,
																							Font.BOLD,
																							14);
 
        // cache containing the created image for each 
	private Map<String, BufferedDynamicImageResource>	dateToImageMap	= new LinkedHashMap<String, BufferedDynamicImageResource>();

	private BufferedDynamicImageResource generateImage(DateTime surveyedDate,
			SurveyTime st, boolean cacheable) throws IOException {

		BufferedDynamicImageResource generatedImage = new BufferedDynamicImageResource(
				"png");

		
		String dow = dayOfWeekFormatter.print(surveyedDate);

		String date = dateFormatter.print(surveyedDate);

		DateTime nextDay = surveyedDate.plusDays(1);

		String nextDow = dayOfWeekFormatter.print(nextDay);
		String nextDate = dateFormatter.print(nextDay);

		BufferedImage overlayImage = ImageIO.read(baseTripTimeImageFile.getFile());

		int width = overlayImage.getWidth();

		int height = overlayImage.getHeight();

		
		
		Graphics2D overlayG2d = overlayImage.createGraphics();

		overlayG2d.setFont(font);
		

		overlayG2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		overlayG2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

	overlayG2d.setPaint(Color.BLACK);
		
		/*
		 * These offsets were calculated using a test case to repeatably place the details
		 * and with the Gimp to locate viable insertion points.
		 */
		// day of week
		overlayG2d.drawString(dow, 32, 90);

		// date
		overlayG2d.drawString(date, 32, 107);

		// next day of week
		overlayG2d.drawString(nextDow, 332, 90);

		// next date
		overlayG2d.drawString(nextDate, 332, 107);				
		
		BufferedImage target = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB);
		
		Graphics2D targetG2d = target.createGraphics();
		
		targetG2d.setFont(font);
		

		targetG2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		targetG2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

		
		targetG2d.setPaint(Color.white);
		
		targetG2d.fillRect(0, 0, width, height);
		
		targetG2d.setPaint(Color.RED);
		
		// fill with the background color (white)
//		targetG2d.fillRect(0, 0, width, height);
				
// draw the overlay (the printed surveyedDate detail) onto the target.	
		targetG2d.drawImage(overlayImage, 0, 0, null);

	
// do the final setup on the BufferedDynamicImageResource that will contain this image
		// note that the generated image does not change the base image.
		generatedImage.setImage(target);

		generatedImage.setCacheable(cacheable);
		generatedImage.setFormat("png");
		
		log.info("generated image");
		

		return generatedImage;
		
	}

	
	public void setBaseTripTimeImageFile(Resource baseTripTimeImageFile) {

		this.baseTripTimeImageFile = baseTripTimeImageFile;
	}

	
	public TripTimeImageServiceImpl() {

		// e.g Wednesday
		dayOfWeekFormatter = DateTimeFormat.forPattern("EEEE");

		// e.g. February 18, 2009
		dateFormatter = DateTimeFormat.forPattern("MMMM dd, yyyy");
	}

	
	@Override
	public org.apache.wicket.Resource getImageResource(DateTime surveyedDate, boolean cacheable)
			throws IOException {

		return getImageResource(surveyedDate, null, cacheable);

	}

	
	@Override
	public org.apache.wicket.Resource getImageResource(DateTime surveyedDate,
			SurveyTime previousDepartureTime, boolean cacheable) throws IOException {

			BufferedDynamicImageResource cachedImage = null;

			
			String cachedImageKey = TimeUtils.getInstance().getString(
					surveyedDate);


			cachedImage = this.dateToImageMap.get(cachedImageKey);
			
			
			if (cachedImage == null) {
				
				cachedImage = generateImage(surveyedDate, previousDepartureTime, cacheable);

				this.dateToImageMap.put(cachedImageKey, cachedImage);

			}
			
				return cachedImage;
		

	}

}

Anchor
Resource
Resource

Using Custom Resource to stamp the image.

...

Overview:

The resource is aware of the current state and will adjust the image based upon it.

...