Advertisement
Guest User

Untitled

a guest
Jul 17th, 2012
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 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.  
  35. /**
  36. * The main controller.
  37. */
  38. @Controller
  39. public class DrawingManageController {
  40.  
  41. protected final Log log = LogFactory.getLog(getClass());
  42.  
  43. @RequestMapping(value = "/module/drawing/manage", method = RequestMethod.GET)
  44. public void manage(ModelMap model, HttpServletRequest request) {
  45.  
  46. if (StringUtils.isNotBlank(request.getParameter("obsId"))) {
  47. int obsId = Integer.parseInt(request.getParameter("obsId"));
  48. Obs obs = Context.getObsService().getComplexObs(obsId, "");
  49. if (obs == null || !obs.getConcept().isComplex()) {
  50. log.error("obs is not complex ");
  51. } else {
  52. AnnotatedImage ai = (AnnotatedImage) obs.getComplexData().getData();
  53. String encodedImage ="/"+ WebConstants.WEBAPP_NAME+"/complexObsServlet?obsId="+obs.getId();
  54.  
  55. //String encodedImage=encodeComplexData(ai.getImage());
  56. model.addAttribute("encodedImage", encodedImage);
  57. model.addAttribute("annotations", ai.getAnnotations());
  58. model.addAttribute("obsId", obs.getId());
  59. }
  60. }else
  61. model.addAttribute("obsId", null);
  62. model.addAttribute("user", Context.getAuthenticatedUser());
  63.  
  64. }
  65.  
  66. public String encodeComplexData(Object o) {
  67. String encodedImage = null;
  68. try {
  69. BufferedImage img = (BufferedImage) o;
  70. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  71. ImageIO.write(img, "png", baos);
  72. encodedImage = "data:image/png;base64," + Base64.encodeBase64String(baos.toByteArray());
  73. }
  74. catch (Exception e) {
  75. log.error("cannot write image", e);
  76.  
  77. }
  78. return encodedImage;
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement