Java Code Examples for javax.imageio.ImageReader#getDefaultReadParam()
The following examples show how to use
javax.imageio.ImageReader#getDefaultReadParam() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageUtil.java From javautils with Apache License 2.0 | 6 votes |
/** * 图片截取 ImageReader截取 速度比Graphic通过画布绘画快很多 * @param image 源图片 * @param rect 待截取区域的坐标位置 * @return */ public static BufferedImage cutImage2(File image, Rectangle rect) { if (image.exists() && rect != null) { BufferedImage bi = null; try { ImageReader reader = getImageReader(image.getAbsolutePath()); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(rect); bi = reader.read(0, param); } catch (IOException e) { e.printStackTrace(); } return bi; } return null; }
Example 2
Source File: ImageHelper.java From MillionHero with MIT License | 6 votes |
/** * 图片裁剪通用接口 * * @param src 图片源地址,图片格式PNG * @param dest 图片目的地址 * @param x 图片起始点x坐标 * @param y 图片起始点y坐标 * @param w 图片宽度 * @param h 图片高度 */ public void cutImage(String src, String dest, int x, int y, int w, int h) { try{ Iterator iterator = ImageIO.getImageReadersByFormatName("png"); ImageReader reader = (ImageReader) iterator.next(); InputStream in = new FileInputStream(src); ImageInputStream iis = ImageIO.createImageInputStream(in); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); Rectangle rect = new Rectangle(x, y, w, h); param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); ImageIO.write(bi, "png", new File(dest)); }catch (IOException e){ System.err.println("裁剪图片失败"); } }
Example 3
Source File: BigBufferedImage.java From OpenRS with GNU General Public License v3.0 | 6 votes |
@Override public ImagePartLoader call() throws Exception { Thread.currentThread().setPriority((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2); try (ImageInputStream stream = ImageIO.createImageInputStream(file);) { Iterator<ImageReader> readers = ImageIO.getImageReaders(stream); if (readers.hasNext()) { ImageReader reader = readers.next(); reader.setInput(stream, true, true); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(region); BufferedImage part = reader.read(0, param); Raster source = part.getRaster(); WritableRaster target = image.getRaster(); target.setRect(0, y, source); } } return ImagePartLoader.this; }
Example 4
Source File: ImageUtils.java From springboot-admin with Apache License 2.0 | 6 votes |
private static BufferedImage readBuffereImage(ImageReader reader, int w, int h) throws IOException { ImageReadParam param = reader.getDefaultReadParam(); int srcWidth = reader.getWidth(0); int srcHeight = reader.getHeight(0); Rectangle rect = null; if ((float) w / h > (float) srcWidth / srcHeight) { h = h * srcWidth / w; w = srcWidth; rect = new Rectangle(0, (srcHeight - h) / 2, w, h); } else { w = w * srcHeight / h; h = srcHeight; rect = new Rectangle((srcWidth - w) / 2, 0, w, h); } param.setSourceRegion(rect); BufferedImage srcBuffered = reader.read(0, param); return srcBuffered; }
Example 5
Source File: ImageFileReaders.java From MyBox with Apache License 2.0 | 6 votes |
public static BufferedImage readFileBySample(String format, String filename, int x1, int y1, int x2, int y2, int sampleWidth, int sampleHeight) { BufferedImage bufferedImage; try { if (x1 >= x2 || y1 >= y2 || x1 < 0 || x2 < 0 || y1 < 0 || y2 < 0 || sampleWidth <= 0 || sampleHeight <= 0) { return null; } ImageReader reader = getReader(format); try ( ImageInputStream in = ImageIO.createImageInputStream(new BufferedInputStream(new FileInputStream(filename)))) { reader.setInput(in, false); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1)); param.setSourceSubsampling(sampleWidth, sampleHeight, 0, 0); bufferedImage = reader.read(0, param); // logger.debug(bufferedImage.getWidth() + " " + bufferedImage.getHeight()); reader.dispose(); } } catch (Exception e) { bufferedImage = readBrokenImage(e, new File(filename), 0, sampleWidth, sampleHeight); } return bufferedImage; }
Example 6
Source File: ReadAsGrayTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
private static void testType(ImageReader reader, ImageTypeSpecifier t, BufferedImage src) throws IOException { ImageReadParam p = reader.getDefaultReadParam(); p.setDestinationType(t); BufferedImage dst = reader.read(0, p); verify(src, dst, t); }
Example 7
Source File: ReadAsGrayTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private static void testType(ImageReader reader, ImageTypeSpecifier t, BufferedImage src) throws IOException { ImageReadParam p = reader.getDefaultReadParam(); p.setDestinationType(t); BufferedImage dst = reader.read(0, p); verify(src, dst, t); }
Example 8
Source File: MultiReadTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private static void test(String format) throws IOException { System.out.println("Format: " + format); BufferedImage src = createSrc(); ImageInputStream iis = prepareInput(src, format); ImageReader reader = ImageIO.getImageReaders(iis).next(); reader.setInput(iis); ImageReadParam p = reader.getDefaultReadParam(); int cnt = 0; do { System.out.println("cnt: " + cnt); p.setSourceRegion(new Rectangle(width / 4, height / 4, width / 2, height / 2)); BufferedImage dst = reader.read(0, p); final Color c = new Color(dst.getRGB(10, 10), true); if (!sameColor(c, srcColor)) { throw new RuntimeException( String.format("Test failed: read color 0x%X\n", c.getRGB())); } } while (++cnt < max); }
Example 9
Source File: ReadAsGrayTest.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
private static void testType(ImageReader reader, ImageTypeSpecifier t, BufferedImage src) throws IOException { ImageReadParam p = reader.getDefaultReadParam(); p.setDestinationType(t); BufferedImage dst = reader.read(0, p); verify(src, dst, t); }
Example 10
Source File: ReadAsGrayTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
private static void testType(ImageReader reader, ImageTypeSpecifier t, BufferedImage src) throws IOException { ImageReadParam p = reader.getDefaultReadParam(); p.setDestinationType(t); BufferedImage dst = reader.read(0, p); verify(src, dst, t); }
Example 11
Source File: ImageFileReaders.java From MyBox with Apache License 2.0 | 5 votes |
public static BufferedImage readFileByScale(String format, String filename, int scale) { BufferedImage bufferedImage; try { if ("ico".equals(format) || "icon".equals(format)) { bufferedImage = readIcon(new File(filename)); return ImageManufacture.scaleImage(bufferedImage, scale); } ImageReader reader = getReader(format); try ( ImageInputStream in = ImageIO.createImageInputStream(new BufferedInputStream(new FileInputStream(filename)))) { reader.setInput(in, false); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(scale, scale, 0, 0); bufferedImage = reader.read(0); // bufferedImage = reader.read(0, param); reader.dispose(); } } catch (Exception e) { bufferedImage = readBrokenImage(e, new File(filename), scale); } return bufferedImage; }
Example 12
Source File: ImageFileReaders.java From MyBox with Apache License 2.0 | 5 votes |
public static BufferedImage readFileByWidth(String format, String filename, int width) { BufferedImage bufferedImage; int scale = 1; try { if ("ico".equals(format) || "icon".equals(format)) { bufferedImage = readIcon(new File(filename)); return ImageManufacture.scaleImageWidthKeep(bufferedImage, width); } ImageReader reader = getReader(format); try ( ImageInputStream in = ImageIO.createImageInputStream(new BufferedInputStream(new FileInputStream(filename)))) { reader.setInput(in, false); if (reader.getWidth(0) <= width) { scale = 1; } else { scale = reader.getWidth(0) / width + 1; if (scale < 2) { scale = 2; } } ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(scale, scale, 0, 0); bufferedImage = reader.read(0, param); // logger.debug(bufferedImage.getWidth() + " " + bufferedImage.getHeight()); reader.dispose(); } } catch (Exception e) { bufferedImage = readBrokenImage(e, new File(filename), scale); } return bufferedImage; }
Example 13
Source File: JPEGsNotAcceleratedTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static BufferedImage readTestImage(String fileName, BufferedImage dest, Rectangle srcROI) { BufferedImage bi = null; try { FileImageInputStream is = new FileImageInputStream(new File(fileName)); ImageReader reader = (ImageReader)ImageIO.getImageReaders(is).next(); ImageReadParam param = reader.getDefaultReadParam(); if (dest != null) { param.setDestination(dest); } if (srcROI != null) { param.setSourceRegion(srcROI); } reader.setInput(is); bi = reader.read(0, param); } catch (IOException e) { System.err.println("Error " + e + " when reading file: " + fileName); throw new RuntimeException(e); } return bi; }
Example 14
Source File: BufferedImageHttpMessageConverter.java From java-technology-stack with MIT License | 5 votes |
@Override public BufferedImage read(@Nullable Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { ImageInputStream imageInputStream = null; ImageReader imageReader = null; try { imageInputStream = createImageInputStream(inputMessage.getBody()); MediaType contentType = inputMessage.getHeaders().getContentType(); if (contentType == null) { throw new HttpMessageNotReadableException("No Content-Type header", inputMessage); } Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString()); if (imageReaders.hasNext()) { imageReader = imageReaders.next(); ImageReadParam irp = imageReader.getDefaultReadParam(); process(irp); imageReader.setInput(imageInputStream, true); return imageReader.read(0, irp); } else { throw new HttpMessageNotReadableException( "Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]", inputMessage); } } finally { if (imageReader != null) { imageReader.dispose(); } if (imageInputStream != null) { try { imageInputStream.close(); } catch (IOException ex) { // ignore } } } }
Example 15
Source File: ReadAsGrayTest.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private static void testType(ImageReader reader, ImageTypeSpecifier t, BufferedImage src) throws IOException { ImageReadParam p = reader.getDefaultReadParam(); p.setDestinationType(t); BufferedImage dst = reader.read(0, p); verify(src, dst, t); }
Example 16
Source File: ReadAsGrayTest.java From hottub with GNU General Public License v2.0 | 5 votes |
private static void testType(ImageReader reader, ImageTypeSpecifier t, BufferedImage src) throws IOException { ImageReadParam p = reader.getDefaultReadParam(); p.setDestinationType(t); BufferedImage dst = reader.read(0, p); verify(src, dst, t); }
Example 17
Source File: ImageUtils.java From albert with MIT License | 4 votes |
/** * 剪裁图片(x,y,w,h,r) * @param srcImageFile 文件流:源图像地址 * @param dest 新图像 * @param x 目标切片起点x坐标 * @param y 目标切片起点y坐标 * @param w 目标切片宽度 * @param h 目标切片高度 * @param r 目标切片旋转度 * @return */ public static boolean cutAndRotateImage(InputStream srcImageFile,File dest,int x,int y,int w,int h,int r){ try{ /* * BufferedImage是Image的一个子类,BufferedImage生成的图片在内存里有一个图像缓冲区, * 利用这个缓冲区我们可以很方便的操作这个图片,通常用来做图片修改操作如大小变换、图片变灰、设置图片透明或不透明等。 */ BufferedImage src = ImageIO.read(srcImageFile); // 读入文件 //图片旋转指定角度 BufferedImage tag= Rotate(src, r); //读取图片文件 Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName("jpg"); ImageReader reader = (ImageReader)iterator.next(); //获取图片流 ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(bs); ImageIO.write(tag, "jpg", ios); InputStream in=new ByteArrayInputStream(bs.toByteArray()); ImageInputStream iis=ImageIO.createImageInputStream(in); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); x=(x>0?x:0); y=(y>0?y:0); //定义一个矩形 Rectangle rect = new Rectangle(x, y, w,h); //提供一个 BufferedImage,将其用作解码像素数据的目标。 param.setSourceRegion(rect); BufferedImage bi = reader.read(0,param); //保存新图片 ImageIO.write(bi, "jpg", dest); return true; }catch (Exception e) { return false; } }
Example 18
Source File: ImageResizeAndCompression.java From o2oa with GNU Affero General Public License v3.0 | 4 votes |
/** * 先压缩后剪切工具方法 * * @param srcfile 源图片 * @param outfile 剪切之后的图片 * @param width 剪切区域宽度 * @param height 剪切区域高度 * * @throws IOException * @author cevencheng */ public static void cut( String srcImgPath, String outImgPath, int width, int height ) throws IOException { BufferedImage src = InputImage( srcImgPath ); // 得到图片对象 int old_w = src.getWidth(); //得到源图宽 int old_h = src.getHeight(); //得到源图高 int new_w = width; int new_h = height; File outfile = new File(outImgPath); BufferedImage tempImg = new BufferedImage( old_w, old_h, BufferedImage.TYPE_INT_RGB ); Graphics2D g = tempImg.createGraphics(); g.setColor( Color.white ); // 从原图上取颜色绘制新图 g.fillRect(0, 0, old_w, old_h); g.drawImage( src, 0, 0, old_w, old_h, Color.white, null ); g.dispose(); // 根据图片尺寸压缩比得到新图的尺寸 if( ( old_w*1.0 )/new_w < ( new_h*1.0 )/new_h ){//纵向缩放比例大 if( old_w > new_w ){//实际的图片宽度,大于预期的缩放宽度,需要进行高度按比例缩小 new_h = Integer.parseInt( new java.text.DecimalFormat("0").format(old_h * new_w/(old_w*1.0)) ); } } else {//横向绽放比例大 if(old_h > new_h){ new_w = Integer.parseInt(new java.text.DecimalFormat("0").format(old_w * new_h/(old_h*1.0))); } } BufferedImage newImg = new BufferedImage( new_w, new_h, BufferedImage.TYPE_INT_RGB); newImg.getGraphics().drawImage( tempImg.getScaledInstance( new_w, new_h, Image.SCALE_SMOOTH), 0, 0, null ); //准备进行裁剪 ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 调用方法输出图片文件 outImage( baos, newImg, 1 ); ByteArrayInputStream is = new ByteArrayInputStream(baos.toByteArray()); ImageInputStream iis = null; try { /* * 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader 声称能够解码指定格式。 * 参数:formatName - 包含非正式格式名称 .(例如 "jpeg" 或 "tiff")等 。 */ Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg"); ImageReader reader = it.next(); // 获取图片流 iis = ImageIO.createImageInputStream(is); /* * <p>iis:读取源.true:只向前搜索 </p>.将它标记为 ‘只向前搜索’。 * 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许 reader 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。 */ reader.setInput(iis, true); /* * <p>描述如何对流进行解码的类<p>.用于指定如何在输入时从 Java Image I/O * 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件 将从其 ImageReader 实现的 * getDefaultReadParam 方法中返回 ImageReadParam 的实例。 */ ImageReadParam param = reader.getDefaultReadParam(); /* * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象 * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。 */ int x=0, y=0; // if( new_w > width ){ //水平中间截取 // x = ( new_w-width )/2; // }else{ // y = ( new_h-height )/2; // } Rectangle rect = new Rectangle( x, y, width, height); // 提供一个 BufferedImage,将其用作解码像素数据的目标。 param.setSourceRegion(rect); /* * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将 它作为一个完整的 * BufferedImage 返回。 */ BufferedImage bi = reader.read(0, param); // 保存新图片 ImageIO.write(bi, "jpg", outfile); } finally { if (is != null) { is.close(); } if (iis != null) { iis.close(); } } }
Example 19
Source File: ImageResizeAndCompression.java From o2oa with GNU Affero General Public License v3.0 | 4 votes |
public static BufferedImage cut( BufferedImage src, Integer width, Integer height ) throws IOException { int old_w = src.getWidth(); //得到源图宽 int old_h = src.getHeight(); //得到源图高 int new_w = width; int new_h = height; BufferedImage tempImg = new BufferedImage( old_w, old_h, BufferedImage.TYPE_INT_RGB ); Graphics2D g = tempImg.createGraphics(); g.setColor( Color.white ); // 从原图上取颜色绘制新图 g.fillRect(0, 0, old_w, old_h); g.drawImage( src, 0, 0, old_w, old_h, Color.white, null ); g.dispose(); // 根据图片尺寸压缩比得到新图的尺寸 if( ( old_w*1.0 )/new_w < ( new_h*1.0 )/new_h ){//纵向缩放比例大 if( old_w > new_w ){//实际的图片宽度,大于预期的缩放宽度,需要进行高度按比例缩小 new_h = Integer.parseInt( new java.text.DecimalFormat("0").format(old_h * new_w/(old_w*1.0)) ); } } else {//横向绽放比例大 if(old_h > new_h){ new_w = Integer.parseInt(new java.text.DecimalFormat("0").format(old_w * new_h/(old_h*1.0))); } } BufferedImage newImg = new BufferedImage( new_w, new_h, BufferedImage.TYPE_INT_RGB); newImg.getGraphics().drawImage( tempImg.getScaledInstance( new_w, new_h, Image.SCALE_SMOOTH), 0, 0, null ); //准备进行裁剪 ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 调用方法输出图片文件 outImage( baos, newImg, 1 ); ByteArrayInputStream is = new ByteArrayInputStream(baos.toByteArray()); ImageInputStream iis = null; try { /* * 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader 声称能够解码指定格式。 * 参数:formatName - 包含非正式格式名称 .(例如 "jpeg" 或 "tiff")等 。 */ Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg"); ImageReader reader = it.next(); // 获取图片流 iis = ImageIO.createImageInputStream(is); /* * <p>iis:读取源.true:只向前搜索 </p>.将它标记为 ‘只向前搜索’。 * 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许 reader 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。 */ reader.setInput(iis, true); /* * <p>描述如何对流进行解码的类<p>.用于指定如何在输入时从 Java Image I/O * 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件 将从其 ImageReader 实现的 * getDefaultReadParam 方法中返回 ImageReadParam 的实例。 */ ImageReadParam param = reader.getDefaultReadParam(); /* * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象 * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。 */ int x=0, y=0; if( new_w > width ){ //水平中间截取 x = ( new_w-width )/2; }else{ y = ( new_h-height )/2; } Rectangle rect = new Rectangle( x, y, width, height); // 提供一个 BufferedImage,将其用作解码像素数据的目标。 param.setSourceRegion(rect); /* * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将 它作为一个完整的 * BufferedImage 返回。 */ BufferedImage bi = reader.read(0, param); // 保存新图片 return bi; } finally { if (is != null) { is.close(); } if (iis != null) { iis.close(); } } }
Example 20
Source File: ToolImageTailor.java From protools with Apache License 2.0 | 4 votes |
/** * 裁剪 * * @param srcPath * 源图片路径名称如:c:/1.jpg * @param savePath * 剪切图片存放路径名称.如:c:/2.jpg * @param x * 剪切点x坐标 * @param y * @param width * 剪切点大小 * @param height * * @throws IOException */ public static void cut(String srcPath, String savePath, int x, int y, int width, int height) throws IOException { String endWiths = srcPath.substring(srcPath.lastIndexOf(".") + 1);// 后缀 /* * 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader 声称能够解码指定格式。 参数:formatName - 包含非正式格式名称 .(例如 "jpeg" 或 "tiff")等 。 */ Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg"); ImageReader reader = it.next(); try ( // 读取图片文件 FileInputStream is = new FileInputStream(srcPath); // 获取图片流 ImageInputStream iis = ImageIO.createImageInputStream(is) ) { /* * <p>iis:读取源.true:只向前搜索 </p>.将它标记为 ‘只向前搜索’。 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许 reader 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。 */ reader.setInput(iis, true); /* * <p>描述如何对流进行解码的类<p>.用于指定如何在输入时从 Java Image I/O 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件 将从其 ImageReader 实现的 getDefaultReadParam 方法中返回 * ImageReadParam 的实例。 */ ImageReadParam param = reader.getDefaultReadParam(); /* * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。 */ Rectangle rect = new Rectangle(x, y, width, height); // 提供一个 BufferedImage,将其用作解码像素数据的目标。 param.setSourceRegion(rect); /* * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将 它作为一个完整的 BufferedImage 返回。 */ BufferedImage bi = reader.read(0, param); // 保存新图片 ImageIO.write(bi, endWiths, new File(savePath)); // "jpg" } }