use of javax.imageio.stream.ImageInputStream in project AozoraEpub3 by hmdev.
the class ImageInfo method getImageInfo.
/** 画像ストリームから画像情報を生成
* @param zipIndex Zipファイルの場合はエントリの位置 (再読込や読み飛ばし時のファイル名比較の省略用)
* @throws IOException */
public static ImageInfo getImageInfo(InputStream is, int zipIndex) throws IOException {
ImageInfo imageInfo = null;
ImageInputStream iis = ImageIO.createImageInputStream(is);
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (readers.hasNext()) {
ImageReader reader = readers.next();
if (readers.hasNext() && reader.getClass().getName().endsWith("CLibPNGImageReader"))
readers.next();
reader.setInput(iis);
String ext = reader.getFormatName();
imageInfo = new ImageInfo(ext, reader.getWidth(0), reader.getHeight(0), zipIndex);
reader.dispose();
}
return imageInfo;
}
use of javax.imageio.stream.ImageInputStream in project openhab1-addons by openhab.
the class Telegram method sendTelegramPhoto.
@ActionDoc(text = "Sends a Picture, protected by username/password authentication, via Telegram REST API")
public static boolean sendTelegramPhoto(@ParamDoc(name = "group") String group, @ParamDoc(name = "photoURL") String photoURL, @ParamDoc(name = "caption") String caption, @ParamDoc(name = "username") String username, @ParamDoc(name = "password") String password) {
if (groupTokens.get(group) == null) {
logger.error("Bot '{}' not defined, action skipped", group);
return false;
}
if (photoURL == null) {
logger.error("photoURL not defined, action skipped");
return false;
}
// load image from url
byte[] imageFromURL;
HttpClient getClient = new HttpClient();
if (username != null && password != null) {
getClient.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
getClient.getState().setCredentials(AuthScope.ANY, defaultcreds);
}
GetMethod getMethod = new GetMethod(photoURL);
getMethod.getParams().setSoTimeout(HTTP_PHOTO_TIMEOUT);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
try {
int statusCode = getClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
logger.error("Method failed: {}", getMethod.getStatusLine());
return false;
}
imageFromURL = getMethod.getResponseBody();
} catch (HttpException e) {
logger.error("Fatal protocol violation: {}", e.toString());
return false;
} catch (IOException e) {
logger.error("Fatal transport error: {}", e.toString());
return false;
} finally {
getMethod.releaseConnection();
}
// parse image type
String imageType;
try {
ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(imageFromURL));
Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis);
if (!imageReaders.hasNext()) {
logger.error("photoURL does not represent a known image type");
return false;
}
ImageReader reader = imageReaders.next();
imageType = reader.getFormatName();
} catch (IOException e) {
logger.error("cannot parse photoURL as image: {}", e.getMessage());
return false;
}
// post photo to telegram
String url = String.format(TELEGRAM_PHOTO_URL, groupTokens.get(group).getToken());
PostMethod postMethod = new PostMethod(url);
try {
postMethod.getParams().setContentCharset("UTF-8");
postMethod.getParams().setSoTimeout(HTTP_PHOTO_TIMEOUT);
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
Part[] parts = new Part[caption != null ? 3 : 2];
parts[0] = new StringPart("chat_id", groupTokens.get(group).getChatId());
parts[1] = new FilePart("photo", new ByteArrayPartSource(String.format("image.%s", imageType), imageFromURL));
if (caption != null) {
parts[2] = new StringPart("caption", caption, "UTF-8");
}
postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
HttpClient client = new HttpClient();
int statusCode = client.executeMethod(postMethod);
if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_ACCEPTED) {
return true;
}
if (statusCode != HttpStatus.SC_OK) {
logger.error("Method failed: {}", postMethod.getStatusLine());
return false;
}
} catch (HttpException e) {
logger.error("Fatal protocol violation: {}", e.toString());
return false;
} catch (IOException e) {
logger.error("Fatal transport error: {}", e.toString());
return false;
} finally {
postMethod.releaseConnection();
}
return true;
}
use of javax.imageio.stream.ImageInputStream in project nutz by nutzam.
the class Images method readJpeg.
/**
* 尝试读取JPEG文件的高级方法,可读取32位的jpeg文件
* <p/>
* 来自:
* http://stackoverflow.com/questions/2408613/problem-reading-jpeg-image-
* using-imageio-readfile-file
*
*/
private static BufferedImage readJpeg(InputStream in) throws IOException {
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
ImageReader reader = null;
while (readers.hasNext()) {
reader = readers.next();
if (reader.canReadRaster()) {
break;
}
}
if (reader == null)
return null;
ImageInputStream input = ImageIO.createImageInputStream(in);
reader.setInput(input);
// Read the image raster
Raster raster = reader.readRaster(0, null);
BufferedImage image = createJPEG4(raster);
ByteArrayOutputStream out = new ByteArrayOutputStream();
writeJpeg(image, out, 1);
out.flush();
return read(new ByteArrayInputStream(out.toByteArray()));
}
use of javax.imageio.stream.ImageInputStream in project robolectric by robolectric.
the class ImageUtil method getImageSizeFromStream.
public static Point getImageSizeFromStream(InputStream is) {
if (!initialized) {
// Stops ImageIO from creating temp files when reading images
// from input stream.
ImageIO.setUseCache(false);
initialized = true;
}
try {
ImageInputStream imageStream = ImageIO.createImageInputStream(is);
Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
if (!readers.hasNext())
return null;
ImageReader reader = readers.next();
try {
reader.setInput(imageStream);
return new Point(reader.getWidth(0), reader.getHeight(0));
} finally {
reader.dispose();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class AllowSearch method test.
private static void test(ImageReader reader, String format) throws IOException {
File f = File.createTempFile("imageio", ".tmp");
ImageInputStream stream = ImageIO.createImageInputStream(f);
reader.setInput(stream, true);
boolean gotISE = false;
try {
int numImages = reader.getNumImages(true);
} catch (IOException ioe) {
gotISE = false;
} catch (IllegalStateException ise) {
gotISE = true;
}
if (!gotISE) {
throw new RuntimeException("Failed to get desired exception for " + format + " reader!");
}
}
Aggregations