SVImageHandler.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2007 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); You may not
  4. // use this file except in compliance with the License. You may obtain a copy of
  5. // the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
  6. // applicable law or agreed to in writing, software distributed under the
  7. // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  8. // OF ANY KIND, either express or implied. See the License for the specific
  9. // language governing permissions and limitations under the License.
  10. package com.google.scrollview.ui;
  11. import org.piccolo2d.nodes.PImage;
  12. import java.io.BufferedReader;
  13. import java.io.ByteArrayInputStream;
  14. import java.io.IOException;
  15. import javax.imageio.ImageIO;
  16. import javax.xml.bind.DatatypeConverter;
  17. /**
  18. * The ScrollViewImageHandler is a helper class which takes care of image
  19. * processing. It is used to construct an Image from the message-stream and
  20. * basically consists of a number of utility functions to process the input
  21. * stream.
  22. *
  23. * @author wanke@google.com
  24. */
  25. public class SVImageHandler {
  26. /* All methods are static, so we forbid to construct SVImageHandler objects. */
  27. private SVImageHandler() {
  28. }
  29. /**
  30. * Reads size bytes from the stream in and interprets it as an image file,
  31. * encoded as png, and then text-encoded as base 64, returning the decoded
  32. * bitmap.
  33. *
  34. * @param size The size of the image file.
  35. * @param in The input stream from which to read the bytes.
  36. */
  37. public static PImage readImage(int size, BufferedReader in) {
  38. char[] charbuffer = new char[size];
  39. int numRead = 0;
  40. while (numRead < size) {
  41. int newRead = -1;
  42. try {
  43. newRead = in.read(charbuffer, numRead, size - numRead);
  44. } catch (IOException e) {
  45. System.out.println("Failed to read image data from socket:" + e.getMessage());
  46. return null;
  47. }
  48. if (newRead < 0) {
  49. return null;
  50. }
  51. numRead += newRead;
  52. }
  53. if (numRead != size) {
  54. System.out.println("Failed to read image data from socket");
  55. return null;
  56. }
  57. // Convert the character data to binary.
  58. byte[] binarydata = DatatypeConverter.parseBase64Binary(new String(charbuffer));
  59. // Convert the binary data to a byte stream and parse to image.
  60. ByteArrayInputStream byteStream = new ByteArrayInputStream(binarydata);
  61. try {
  62. PImage img = new PImage(ImageIO.read(byteStream));
  63. return img;
  64. } catch (IOException e) {
  65. System.out.println("Failed to decode image data from socket" + e.getMessage());
  66. }
  67. return null;
  68. }
  69. }