comparison mupdf-source/thirdparty/tesseract/java/com/google/scrollview/ui/SVImageHandler.java @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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
11 package com.google.scrollview.ui;
12
13 import org.piccolo2d.nodes.PImage;
14
15 import java.io.BufferedReader;
16 import java.io.ByteArrayInputStream;
17 import java.io.IOException;
18 import javax.imageio.ImageIO;
19 import javax.xml.bind.DatatypeConverter;
20
21 /**
22 * The ScrollViewImageHandler is a helper class which takes care of image
23 * processing. It is used to construct an Image from the message-stream and
24 * basically consists of a number of utility functions to process the input
25 * stream.
26 *
27 * @author wanke@google.com
28 */
29 public class SVImageHandler {
30 /* All methods are static, so we forbid to construct SVImageHandler objects. */
31 private SVImageHandler() {
32 }
33
34 /**
35 * Reads size bytes from the stream in and interprets it as an image file,
36 * encoded as png, and then text-encoded as base 64, returning the decoded
37 * bitmap.
38 *
39 * @param size The size of the image file.
40 * @param in The input stream from which to read the bytes.
41 */
42 public static PImage readImage(int size, BufferedReader in) {
43 char[] charbuffer = new char[size];
44 int numRead = 0;
45 while (numRead < size) {
46 int newRead = -1;
47 try {
48 newRead = in.read(charbuffer, numRead, size - numRead);
49 } catch (IOException e) {
50 System.out.println("Failed to read image data from socket:" + e.getMessage());
51 return null;
52 }
53 if (newRead < 0) {
54 return null;
55 }
56 numRead += newRead;
57 }
58 if (numRead != size) {
59 System.out.println("Failed to read image data from socket");
60 return null;
61 }
62 // Convert the character data to binary.
63 byte[] binarydata = DatatypeConverter.parseBase64Binary(new String(charbuffer));
64 // Convert the binary data to a byte stream and parse to image.
65 ByteArrayInputStream byteStream = new ByteArrayInputStream(binarydata);
66 try {
67 PImage img = new PImage(ImageIO.read(byteStream));
68 return img;
69 } catch (IOException e) {
70 System.out.println("Failed to decode image data from socket" + e.getMessage());
71 }
72 return null;
73 }
74 }