本文整理汇总了Java中javax.imageio.ImageWriter.getDefaultWriteParam方法的典型用法代码示例。如果您正苦于以下问题:Java ImageWriter.getDefaultWriteParam方法的具体用法?Java ImageWriter.getDefaultWriteParam怎么用?Java ImageWriter.getDefaultWriteParam使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.imageio.ImageWriter
的用法示例。
在下文中一共展示了ImageWriter.getDefaultWriteParam方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compress
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static byte[] compress(BufferedImage image, float scale) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = writers.next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(scale);
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), param);
byte[] data = baos.toByteArray();
writer.dispose();
return data;
}
示例2: saveImage
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void saveImage(final String fileName, final BufferedImage image, ImageFormat imageFormat) {
try {
final File file = new File(fileName);
final String extension = FileUtilities.getExtension(fileName);
Iterator<ImageWriter> iter = null;
if (canWriteFormat(extension)) {
iter = ImageIO.getImageWritersByFormatName(extension);
} else {
iter = ImageIO.getImageWritersByFormatName(imageFormat.toString());
}
final ImageWriter writer = iter.next();
final ImageWriteParam iwp = writer.getDefaultWriteParam();
file.getParentFile().mkdirs();
try (final FileImageOutputStream output = new FileImageOutputStream(file.getAbsoluteFile())) {
writer.setOutput(output);
final IIOImage outimage = new IIOImage(image, null, null);
writer.write(null, outimage, iwp);
writer.dispose();
}
} catch (final IOException e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
}
示例3: compress
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
/**
* 根据质量压缩图片,不改变图片的尺寸
*
* @param imgContent
* 图片内容
* @param quality
* 缩放比例
* @param extName
* 后缀名
* @return
*/
public byte[] compress(byte[] imgContent, float quality, String extName) {
if (quality > 1 || quality <= 0 || imgContent == null || WakaUtils.string.isEmpty(extName)) {
throw new ZhhrUtilException("压缩文件失败!入参为空");
}
try (InputStream is = new ByteArrayInputStream(imgContent);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);) {
BufferedImage image = ImageIO.read(is);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(extName);
ImageWriter writer = writers.next();
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
writer.write(null, new IIOImage(image, null, null), param);
writer.dispose();
return os.toByteArray();
} catch (IOException e) {
throw new ZhhrUtilException(e.getMessage(), e);
}
}
示例4: compressSave
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public void compressSave(MultipartFile file, String uploadLocation) throws IOException {
//Compresses JPG file but kills the quality.
BufferedImage image = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
File compressedImageFile = new File(uploadLocation);
OutputStream os = new FileOutputStream(compressedImageFile);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if(param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
}
writer.write(null, new IIOImage(image, null, null), param);
}
示例5: main
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
Iterator witer = ImageIO.getImageWritersByFormatName("png");
ImageWriter w = (ImageWriter)witer.next();
File f = File.createTempFile("WriteProgressive", ".png");
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
w.setOutput(ios);
BufferedImage bi = new BufferedImage(100, 100,
BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = bi.createGraphics();
Random r = new Random(10);
for (int i = 0; i < 10000; i++) {
Color c =
new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
g.setColor(c);
g.fillRect(r.nextInt(100), r.nextInt(100), 1, 1);
}
IIOImage iioimage = new IIOImage(bi, null, null);
ImageWriteParam param = w.getDefaultWriteParam();
param.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);
try {
w.write(null, iioimage, param);
} catch (NullPointerException npe) {
throw new RuntimeException("Got NPE during write!");
}
ios.close();
BufferedImage bi2 = ImageIO.read(f);
f.delete();
ImageCompare.compare(bi, bi2);
}
示例6: createTestImage
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static Image createTestImage() throws IOException {
BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });
ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
ImageWriteParam param = writer.getDefaultWriteParam();
writer.setOutput(ios);
writer.prepareWriteSequence(null);
writer.writeToSequence(new IIOImage(frame1, null, null), param);
writer.writeToSequence(new IIOImage(frame2, null, null), param);
writer.endWriteSequence();
writer.reset();
writer.dispose();
ios.flush();
ios.close();
return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
}
示例7: ImageCompressionTypesTest
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public ImageCompressionTypesTest(String format) {
Iterator it = ImageIO.getImageWritersByFormatName(format);
while (it.hasNext()) {
writer = (ImageWriter) it.next();
break;
}
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
System.out.println("Checking compression types for : " + format);
String compTypes[] = param.getCompressionTypes();
if (compTypes.length > 1) {
for (int i = 0; i < compTypes.length; i++) {
for (int j = i + 1; j < compTypes.length; j++) {
if (compTypes[i].equalsIgnoreCase(compTypes[j])) {
throw new RuntimeException("Duplicate compression"
+ " type exists for image format " + format);
}
}
}
}
}
示例8: testEncode
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
@Test
public void testEncode() throws IOException {
ImageWriter writer = Streams.stream(ImageIO.getImageWritersByFormatName("jpeg"))
.filter(TurboJpegImageWriter.class::isInstance)
.findFirst().get();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.85f);
BufferedImage in = ImageIO.read(ClassLoader.getSystemResource("crop_aligned.jpg"));
ByteArrayOutputStream os = new ByteArrayOutputStream();
try (ImageOutputStream ios = ImageIO.createImageOutputStream(os)) {
writer.setOutput(ios);
writer.write(null, new IIOImage(in, null, null), param);
}
os.flush();
assertThat(os.toByteArray()).isNotEmpty();
}
示例9: doTest
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static void doTest(int mode) {
String fileFormat = "bmp";
try {
ImageWriter iw = (ImageWriter)ImageIO.getImageWritersBySuffix(fileFormat).next();
if(iw == null) {
throw new RuntimeException("No available image writer for "
+ fileFormat
+ " Test failed.");
}
File file = new File("image." + fileFormat);
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
iw.setOutput(ios);
BufferedImage bimg = new BufferedImage(100,
100, BufferedImage.TYPE_INT_RGB);
Graphics g = bimg.getGraphics();
g.setColor(Color.green);
g.fillRect(0,0,100,100);
ImageWriteParam param = iw.getDefaultWriteParam();
param.setCompressionMode(mode);
IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bimg),
param);
IIOImage iioImg = new IIOImage(bimg, null, meta);
iw.write(null, iioImg, param);
} catch(Exception e) {
e.printStackTrace();
throw new RuntimeException("Test failed.");
}
}
示例10: apply
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
@Override
public BufferedImage apply(BufferedImage img) {
if(compressionLevel == 0f)
return img;
try {
ImageWriter iw = ImageIO.getImageWritersByFormatName("jpeg").next();
JPEGImageWriteParam iwp = (JPEGImageWriteParam) iw.getDefaultWriteParam();
iwp.setOptimizeHuffmanTables(false);
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
iwp.setCompressionQuality(1f-compressionLevel);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
iw.setOutput(new MemoryCacheImageOutputStream(baos));
IIOImage outputImage = new IIOImage(img, null, null);
iw.write(null, outputImage, iwp);
iw.dispose();
baos.flush();
byte[] returnImage = baos.toByteArray();
baos.close();
BufferedImage img2 = ImageIO.read(new ByteArrayInputStream(returnImage));
if(img2 == null)
throw new Exception();
else
img = img2;
} catch (Exception e) {
e.printStackTrace();
}
return img;
}
示例11: resize
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
/**
* Reads the original image, creates a resized copy of it and returns its input stream. largestDimension is the
* largest dimension of the resized image, the other dimension is scaled accordingly.
*
* @param image
* original image
* @param largestDimension
* the largest dimension of the resized image, the other dimension is scaled accordingly
* @return
* @throws IOException
* @throws UploadImageGalleryFileException
*/
public static InputStream resize(BufferedImage image, int largestDimension) throws IOException {
try {
//resize to 150 pixels max
BufferedImage outImage = Scalr.resize(image, Method.QUALITY, largestDimension);
//set jpeg compression quality explicitly
ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam jpgWriteParam = imageWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(.95f);
// buffer all data in a circular buffer of infinite sizes
CircularByteBuffer outputBuffer = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
ImageOutputStream ios = ImageIO.createImageOutputStream(outputBuffer.getOutputStream());
imageWriter.setOutput(ios);
IIOImage outputImage = new IIOImage(outImage, null, null);
imageWriter.write(null, outputImage, jpgWriteParam);
imageWriter.dispose();
outputBuffer.getOutputStream().close();
return outputBuffer.getInputStream();
} catch (IOException e) {
log.error(e.getStackTrace());
return null;
}
}
示例12: doTest
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public void doTest(int type) throws IOException {
BufferedImage src = getTestImage(type);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
writer.setOutput(ios);
ImageWriteParam wparam = writer.getDefaultWriteParam();
wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
switch(type) {
case TEST_RLE8:
wparam.setCompressionType("BI_RLE8");
break;
case TEST_RLE4:
wparam.setCompressionType("BI_RLE4");
break;
default:
throw new IllegalArgumentException("Wrong test type: " + type);
}
writer.write(null, new IIOImage(src, null, null), wparam);
ios.close();
baos.close();
// read result
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
BufferedImage dst = ImageIO.read(bais);
checkResult(src, dst);
}
示例13: writeWithCompression
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
private static void writeWithCompression(BufferedImage src,
String compression) throws IOException
{
System.out.println("Compression: " + compression);
ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
if (writer == null) {
throw new RuntimeException("Test failed: no bmp writer available");
}
File fout = File.createTempFile(compression + "_", ".bmp",
new File("."));
ImageOutputStream ios = ImageIO.createImageOutputStream(fout);
writer.setOutput(ios);
BMPImageWriteParam param = (BMPImageWriteParam)
writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType(compression);
param.setTopDown(true);
writer.write(null, new IIOImage(src, null, null), param);
writer.dispose();
ios.flush();
ios.close();
BufferedImage dst = ImageIO.read(fout);
verify(dst);
}
示例14: main
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void main(String[] args) {
try {
AnimationTest t = new AnimationTest();
t.initFrame();
ImageWriter w = t.initWriter();
ImageWriteParam p = w.getDefaultWriteParam();
IIOMetadata streamMetadata = w.getDefaultStreamMetadata(p);
w.prepareWriteSequence(streamMetadata);
for (int i = 0; i < 50; i++) {
BufferedImage f = t.createNextFrame();
ImageTypeSpecifier type = new ImageTypeSpecifier(f);
IIOMetadata m = w.getDefaultImageMetadata(type, p);
w.writeToSequence(new IIOImage(f, null, m), p);
}
w.endWriteSequence();
t.checkAnimation();
} catch (Exception e) {
throw new RuntimeException("Test failed.", e);
}
}
示例15: writeTestImage
import javax.imageio.ImageWriter; //导入方法依赖的package包/类
public static void writeTestImage(String fileName) {
BufferedImage bi =
new BufferedImage(TEST_W, TEST_H, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(new Color(testRGB));
g.fillRect(0, 0, TEST_W, TEST_H);
try {
System.err.printf("Writing %s\n", fileName);
if (lowCompression) {
ImageWriter iw = (ImageWriter)ImageIO.getImageWritersBySuffix("jpeg").next();
if(iw == null) {
throw new RuntimeException("No available image writer for "
+ "jpeg "
+ " Test failed.");
}
File file = new File(fileName);
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
iw.setOutput(ios);
ImageWriteParam param = iw.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(1);
IIOImage iioImg = new IIOImage(bi, null, null);
iw.write(null, iioImg, param);
} else {
ImageIO.write(bi, "jpeg", new File(fileName));
}
} catch (IOException e) {
System.err.println("Error " + e +
" when writing file: " + fileName);
throw new RuntimeException(e);
}
}