本文整理汇总了Java中javax.imageio.ImageReadParam.setSourceRegion方法的典型用法代码示例。如果您正苦于以下问题:Java ImageReadParam.setSourceRegion方法的具体用法?Java ImageReadParam.setSourceRegion怎么用?Java ImageReadParam.setSourceRegion使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.imageio.ImageReadParam
的用法示例。
在下文中一共展示了ImageReadParam.setSourceRegion方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: cut
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
public static BufferedImage cut(String format, InputStream src, int x, int y, int w, int h) throws IOException {
Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(format);
ImageReader reader = (ImageReader) iterator.next();
ImageInputStream iis = ImageIO.createImageInputStream(src);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
int imgWidth = reader.getWidth(0);
int imgHeight = reader.getHeight(0);
if (x + w > imgWidth) {
w = imgWidth - x;
}
if (y + h > imgHeight) {
h = imgHeight - y;
}
Rectangle rect = new Rectangle(x, y, w, h);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
return bi;
}
示例2: readTile
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
@Override
public BufferedImage readTile(int imageIndex, int tileX, int tileY) throws IOException {
checkIndex(imageIndex);
int xOffset = getTileGridXOffset(imageIndex);
int yOffset = getTileGridYOffset(imageIndex);
int tileWidth = getTileWidth(imageIndex);
int tileHeight = getTileHeight(imageIndex);
Rectangle region = new Rectangle(
xOffset + tileX * tileWidth,
yOffset + tileY * tileHeight,
tileWidth,
tileHeight);
if (region.x + region.width > getWidth(imageIndex) || region.y + region.height > getHeight(imageIndex)) {
throw new IllegalArgumentException("Tile indices out of bounds.");
}
ImageReadParam param = getDefaultReadParam();
param.setSourceRegion(region);
return this.read(imageIndex, param);
}
示例3: getReadParam
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
/** Determine parameters for image reading based on the IIIF selector and a given scaling factor **/
private ImageReadParam getReadParam(ImageReader reader, ImageApiSelector selector, double decodeScaleFactor) throws IOException {
ImageReadParam readParam = reader.getDefaultReadParam();
Dimension nativeDimensions = new Dimension(reader.getWidth(0), reader.getHeight(0));
Rectangle targetRegion = selector.getRegion().resolve(nativeDimensions);
// IIIF regions are always relative to the native size, while ImageIO regions are always relative to the decoded
// image size, hence the conversion
Rectangle decodeRegion = new Rectangle(
(int) Math.ceil(targetRegion.getX() * decodeScaleFactor),
(int) Math.ceil(targetRegion.getY() * decodeScaleFactor),
(int) Math.ceil(targetRegion.getWidth() * decodeScaleFactor),
(int) Math.ceil(targetRegion.getHeight() * decodeScaleFactor));
readParam.setSourceRegion(decodeRegion);
// TurboJpegImageReader can rotate during decoding
boolean didRotate = false;
if (selector.getRotation().getRotation() != 0 && reader instanceof TurboJpegImageReader) {
((TurboJpegImageReadParam) readParam).setRotationDegree((int) selector.getRotation().getRotation());
didRotate = true;
}
return readParam;
}
示例4: cutImage
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
/**
* @Description:截图
* @author:liuyc
* @time:2016年5月27日 上午10:18:23
* @param srcFile源图片、targetFile截好后图片全名、startAcross 开始截取位置横坐标、StartEndlong开始截图位置纵坐标、width截取的长,hight截取的高
*/
public static void cutImage(String srcFile, String targetFile, int startAcross, int StartEndlong, int width,
int hight) throws Exception {
// 取得图片读入器
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader = readers.next();
// 取得图片读入流
InputStream source = new FileInputStream(srcFile);
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
// 图片参数对象
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(startAcross, StartEndlong, width, hight);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, targetFile.split("\\.")[1], new File(targetFile));
}
示例5: readBuffereImage
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
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 (w / h > 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;
}
示例6: ByteArrayInputStream
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
/**
* 方法(私有)<br>
* 名称: cropImage<br>
* 描述: 根据指定的位置和尺寸剪切图片<br>
*
* @return BufferedImage - 图片字节数组
* @throws java.io.IOException
*/
private ByteArrayOutputStream cropImage
(ByteArrayOutputStream out,int x,int y,int width,int height)
throws IOException
{
Iterator iterator=ImageIO.getImageReadersByFormatName(this.outputFormat.getFormat());
ImageReader reader=(ImageReader)iterator.next();
InputStream in=new ByteArrayInputStream(out.toByteArray());
ImageInputStream iis=ImageIO.createImageInputStream(in);
reader.setInput(iis,true);
ImageReadParam param=reader.getDefaultReadParam();
Rectangle rect=new Rectangle(x,y,width,height);
param.setSourceRegion(rect);
BufferedImage bi=reader.read(0,param);
out.reset();
ImageIO.write(bi,this.outputFormat.getFormat(),out);
//System.out.println("dropImage:"+out.toByteArray().length);
return out;
}
示例7: call
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
@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;
}
示例8: call
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
@Override
public ImagePartLoader call() throws Exception {
Thread.currentThread().setPriority((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2);
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;
}
示例9: cut
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
public void cut() throws IOException {
FileInputStream is = null;
ImageInputStream iis = null;
try {
is = new FileInputStream(srcpath);
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(this.imageType);
ImageReader reader = it.next();
iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, this.imageType, new File(subpath));
} finally {
if (is != null)
is.close();
if (iis != null)
iis.close();
}
}
示例10: readTile
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
public BufferedImage readTile(int imageIndex, int tileX, int tileY)
throws IOException {
int w = getWidth(imageIndex);
int h = getHeight(imageIndex);
int tw = getTileWidth(imageIndex);
int th = getTileHeight(imageIndex);
int x = tw * tileX;
int y = th * tileY;
if (tileX < 0 || tileY < 0 || x >= w || y >= h) {
throw new IllegalArgumentException("Tile indices are out of bounds!");
}
if (x + tw > w) {
tw = w - x;
}
if (y + th > h) {
th = h - y;
}
ImageReadParam param = getDefaultReadParam();
Rectangle tileRect = new Rectangle(x, y, tw, th);
param.setSourceRegion(tileRect);
return read(imageIndex, param);
}
示例11: test
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
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);
}
示例12: readTestImage
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
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;
}
示例13: testReadRegionAligned
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
@Test
public void testReadRegionAligned() throws IOException {
ImageReader reader = getReader("crop_aligned.jpg");
ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(new Rectangle(32, 32, 96, 96));
BufferedImage img = reader.read(0, param);
assertThat(img)
.hasDimensions(96, 96)
.hasNoPixelsOfColor(-1 /* white */);
}
示例14: testReadRegionUnaligned
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
@Test
public void testReadRegionUnaligned() throws IOException {
ImageReader reader = getReader("crop_unaligned.jpg");
ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(new Rectangle(116, 148, 204, 172));
BufferedImage img = reader.read(0, param);
assertThat(img)
.hasDimensions(204, 172)
.hasNoPixelsOfColor(-1 /* white */);
}
示例15: testReadUnalignedScaled
import javax.imageio.ImageReadParam; //导入方法依赖的package包/类
@Test
public void testReadUnalignedScaled() throws IOException {
ImageReader reader = getReader("crop_unaligned.jpg");
ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(new Rectangle(87, 111, 152, 129));
BufferedImage img = reader.read(1, param);
assertThat(img)
.hasDimensions(152, 129)
.hasNoPixelsOfColor(-1 /* white */);
}