tesseract
3.03
|
00001 // Copyright 2007 Google Inc. All Rights Reserved. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); You may not 00004 // use this file except in compliance with the License. You may obtain a copy of 00005 // the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 00006 // applicable law or agreed to in writing, software distributed under the 00007 // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 00008 // OF ANY KIND, either express or implied. See the License for the specific 00009 // language governing permissions and limitations under the License. 00010 00011 package com.google.scrollview.ui; 00012 00013 import org.piccolo2d.nodes.PImage; 00014 00015 import java.io.BufferedReader; 00016 import java.io.ByteArrayInputStream; 00017 import java.io.IOException; 00018 import javax.imageio.ImageIO; 00019 import javax.xml.bind.DatatypeConverter; 00020 00029 public class SVImageHandler { 00030 /* All methods are static, so we forbid to construct SVImageHandler objects */ 00031 private SVImageHandler() { 00032 } 00033 00042 public static PImage readImage(int size, BufferedReader in) { 00043 char[] charbuffer = new char[size]; 00044 int numRead = 0; 00045 while (numRead < size) { 00046 int newRead = -1; 00047 try { 00048 newRead = in.read(charbuffer, numRead, size - numRead); 00049 } catch (IOException e) { 00050 System.out.println("Failed to read image data from socket:" + e.getMessage()); 00051 return null; 00052 } 00053 if (newRead < 0) { 00054 return null; 00055 } 00056 numRead += newRead; 00057 } 00058 if (numRead != size) { 00059 System.out.println("Failed to read image data from socket"); 00060 return null; 00061 } 00062 // Convert the character data to binary. 00063 byte[] binarydata = DatatypeConverter.parseBase64Binary(new String(charbuffer)); 00064 // Convert the binary data to a byte stream and parse to image. 00065 ByteArrayInputStream byteStream = new ByteArrayInputStream(binarydata); 00066 try { 00067 PImage img = new PImage(ImageIO.read(byteStream)); 00068 return img; 00069 } catch (IOException e) { 00070 System.out.println("Failed to decode image data from socket" + e.getMessage()); 00071 } 00072 return null; 00073 } 00074 }