Advertisement
Guest User

Untitled

a guest
Jul 17th, 2012
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /**
  2. * The contents of this file are subject to the OpenMRS Public License
  3. * Version 1.0 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. * http://license.openmrs.org
  6. *
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. * License for the specific language governing rights and limitations
  10. * under the License.
  11. *
  12. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  13. */
  14. package org.openmrs.module.drawing.web.controller;
  15.  
  16. import java.awt.image.BufferedImage;
  17. import java.io.ByteArrayOutputStream;
  18.  
  19. import javax.imageio.ImageIO;
  20. import javax.servlet.http.HttpServletRequest;
  21.  
  22. import org.apache.commons.codec.binary.Base64;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.openmrs.Obs;
  27. import org.openmrs.api.context.Context;
  28. import org.openmrs.module.drawing.AnnotatedImage;
  29. import org.openmrs.web.WebConstants;
  30. import org.springframework.stereotype.Controller;
  31. import org.springframework.ui.ModelMap;
  32. import org.springframework.web.bind.annotation.RequestMapping;
  33. import org.springframework.web.bind.annotation.RequestMethod;
  34. import org.springframework.web.bind.annotation.RequestParam;
  35.  
  36. /**
  37. * The main controller.
  38. */
  39. @Controller
  40. public class DrawingManageController {
  41.  
  42. protected final Log log = LogFactory.getLog(getClass());
  43.  
  44. @RequestMapping(value = "/module/drawing/manage", method = RequestMethod.GET)
  45. public void manage(ModelMap model, HttpServletRequest request) {
  46.  
  47. if (StringUtils.isNotBlank(request.getParameter("obsId"))) {
  48. int obsId = Integer.parseInt(request.getParameter("obsId"));
  49. Obs obs = Context.getObsService().getComplexObs(obsId, "");
  50. if (obs == null || !obs.getConcept().isComplex()) {
  51. log.error("obs is not complex ");
  52. } else {
  53. AnnotatedImage ai = (AnnotatedImage) obs.getComplexData().getData();
  54. String encodedImage = WebConstants.WEBAPP_NAME+"/complexObsServlet?obsId="+obs.getId();
  55.  
  56. //String encodedImage=encodeComplexData(ai.getImage());
  57. model.addAttribute("encodedImage", encodedImage);
  58. model.addAttribute("annotations", ai.getAnnotations());
  59. model.addAttribute("obsId", obs.getId());
  60. }
  61. }
  62. model.addAttribute("user", Context.getAuthenticatedUser());
  63. }
  64.  
  65. public String encodeComplexData(Object o) {
  66. String encodedImage = null;
  67. try {
  68. BufferedImage img = (BufferedImage) o;
  69. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  70. ImageIO.write(img, "png", baos);
  71. encodedImage = "data:image/png;base64," + Base64.encodeBase64String(baos.toByteArray());
  72. }
  73. catch (Exception e) {
  74. log.error("cannot write image", e);
  75.  
  76. }
  77. return encodedImage;
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement