本文整理汇总了Java中javax.imageio.stream.MemoryCacheImageOutputStream类的典型用法代码示例。如果您正苦于以下问题:Java MemoryCacheImageOutputStream类的具体用法?Java MemoryCacheImageOutputStream怎么用?Java MemoryCacheImageOutputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MemoryCacheImageOutputStream类属于javax.imageio.stream包,在下文中一共展示了MemoryCacheImageOutputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertToPNG
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
private static byte[] convertToPNG(Image image) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int width = image.getWidth(null);
int height = image.getHeight(null);
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.setComposite(AlphaComposite.Src);
g.drawImage(image, 0, 0, null);
g.dispose();
ImageOutputStream ios = new MemoryCacheImageOutputStream(out);
try {
if (!ImageIO.write(bi, "PNG", ios)) {
throw new IOException("ImageIO.write failed");
}
ios.close();
} catch (IOException ex) {
throw new RuntimeException("saveImage: " + ex.getMessage());
}
return out.toByteArray();
}
示例2: toJPG
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
public static byte[] toJPG(BufferedImage originalImage) throws IOException {
BufferedImage image = removeAlpha(originalImage);
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(0.9f);
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos)) {
jpgWriter.setOutput(mcios);
IIOImage outputImage = new IIOImage(image, null, null);
jpgWriter.write(null, outputImage, jpgWriteParam);
jpgWriter.dispose();
bos.flush();
return bos.toByteArray();
}
}
示例3: toPng
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
private byte[] toPng(BufferedImage img, Float compressionQuality) throws IOException {
ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
if (compressionQuality != null) {
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionQuality(compressionQuality);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream stream = new MemoryCacheImageOutputStream(baos);
try {
writer.setOutput(stream);
writer.write(null, new IIOImage(img, null, null), writeParam);
} finally {
stream.close();
writer.dispose();
}
return baos.toByteArray();
}
示例4: writeJPG
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
public static void writeJPG(
BufferedImage bufferedImage,
OutputStream outputStream,
float quality) throws IOException {
Iterator<ImageWriter> iterator
= ImageIO.getImageWritersByFormatName("jpg");
ImageWriter imageWriter = iterator.next();
ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriteParam.setCompressionQuality(quality);
ImageOutputStream imageOutputStream
= new MemoryCacheImageOutputStream(outputStream);
imageWriter.setOutput(imageOutputStream);
IIOImage iioimage = new IIOImage(bufferedImage, null, null);
imageWriter.write(null, iioimage, imageWriteParam);
imageOutputStream.flush();
}
示例5: writeToFile
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
public static void writeToFile(BufferedImage img, String filename) {
try {
FileOutputStream out = new FileOutputStream(new File(filename));
String extension = null;
int i = filename.lastIndexOf('.');
if (i > 0) {
extension = filename.substring(i + 1);
}
if (extension != null) {
ImageIO.write(img, extension, new MemoryCacheImageOutputStream(out));
}
} catch (Exception e) {
Logging.logError(e);
}
}
示例6: getBytes
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
public byte[] getBytes() {
if (bytes != null) {
return bytes;
}
if (buffer != null) {
bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return bytes;
}
if (image != null) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", new MemoryCacheImageOutputStream(bos));
bytes = bos.toByteArray();
return bytes;
} catch (Exception e) {
Logging.logError(e);
}
}
// TODO image --to--> bytes
return null;
}
示例7: getJPGBytes
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
private byte[] getJPGBytes(BufferedImage image) {
ByteArrayOutputStream outputStream = null;
try {
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(1.0f);
outputStream = new ByteArrayOutputStream();
jpgWriter.setOutput(new MemoryCacheImageOutputStream((outputStream)));
IIOImage outputImage = new IIOImage(image, null, null);
jpgWriter.write(null, outputImage, jpgWriteParam);
jpgWriter.dispose();
outputStream.flush();
return outputStream.toByteArray();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
closeOutputStream(outputStream);
}
}
示例8: applySimulation
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
@Override
public MBFImage applySimulation(MBFImage input) {
try {
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
float compressionFactor = this.randomFloatInRange(minCompression, maxCompression);
iwp.setCompressionQuality(compressionFactor);
ByteArrayOutputStream output = new ByteArrayOutputStream();
writer.setOutput(new MemoryCacheImageOutputStream(output));
IIOImage image = new IIOImage(ImageUtilities.createBufferedImage(input), null, null);
writer.write(null, image, iwp);
return ImageUtilities.readMBF(new ByteArrayInputStream(output.toByteArray()));
} catch (IOException e) {
throw new Error(e);
}
}
示例9: bufferedImageToJPEG
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
private static byte[] bufferedImageToJPEG(BufferedImage bufferedImage, float quality)
throws Exception
{
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
ColorModel colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
ImageTypeSpecifier imageTypeSpecifier =
new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(1, 1)/*ignored*/);
writeParam.setDestinationType(imageTypeSpecifier);
writeParam.setSourceBands(new int[] {0, 1, 2});
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionQuality(quality);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
writer.setOutput(new MemoryCacheImageOutputStream(buffer));
IIOImage ioImage = new IIOImage(bufferedImage, null, null);
writer.write(null, ioImage, writeParam);
writer.dispose();
return buffer.toByteArray();
}
示例10: writeTo
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
/**
* Can change this to choose a better compression level as the default
*
* @param image
* @param scaledImage
* @return
*/
private static boolean writeTo(BufferedImage image, OutputStream scaledImage, Size scaledSize, String outputFormat) {
try {
if (!StringHelper.containsNonWhitespace(outputFormat)) {
outputFormat = OUTPUT_FORMAT;
}
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat);
if (writers.hasNext()) {
ImageWriter writer = writers.next();
ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize);
IIOImage iiOImage = new IIOImage(image, null, null);
ImageOutputStream iOut = new MemoryCacheImageOutputStream(scaledImage);
writer.setOutput(iOut);
writer.write(null, iiOImage, iwp);
writer.dispose();
iOut.flush();
iOut.close();
return true;
} else {
return ImageIO.write(image, outputFormat, scaledImage);
}
} catch (IOException e) {
return false;
}
}
示例11: writeToFile
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
public static void writeToFile(BufferedImage img, String filename) {
try {
FileOutputStream out = new FileOutputStream(new File(filename));
String extension = null;
int i = filename.lastIndexOf('.');
if (i > 0) {
extension = filename.substring(i + 1);
}
if (extension != null) {
ImageIO.write(img, extension, new MemoryCacheImageOutputStream(out));
}
} catch (Exception e) {
Logging.logError(e);
}
}
示例12: getBytes
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
public byte[] getBytes() {
if (bytes != null) {
return bytes;
}
if (buffer != null) {
bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return bytes;
}
if (image != null) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", new MemoryCacheImageOutputStream(bos));
bytes = bos.toByteArray();
return bytes;
} catch (Exception e) {
Logging.logError(e);
}
}
// TODO image --to--> bytes
return null;
}
示例13: generateMipmap
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
/**
* Generates a mipmap and writes it to the specified output stream.
*
* @param sourceUrl URL string for the source image to be down-sampled.
*
* @param mipmapLevelDelta difference between the mipmap level of the source image and
* the mipmap level for the generated image (should be positive).
*
* @param format format of the down-sampled image (e.g. {@link Utils#JPEG_FORMAT})
*
* @param jpegQuality JPEG quality (0.0 <= x <= 1.0, only relevant for JPEG images).
*
* @param outputStream output stream for the down-sampled image.
*
* @throws IllegalArgumentException
* if the source URL cannot be loaded or an invalid delta value is specified.
*
* @throws IOException
* if the down-sampled image cannot be written.
*/
public static void generateMipmap(final String sourceUrl,
final int mipmapLevelDelta,
final String format,
final Float jpegQuality,
final OutputStream outputStream)
throws IllegalArgumentException, IOException {
final ImagePlus sourceImagePlus = Utils.openImagePlusUrl(sourceUrl);
if (sourceImagePlus == null) {
throw new IllegalArgumentException("failed to load '" + sourceUrl + "' for scaling");
}
if (mipmapLevelDelta < 1) {
throw new IllegalArgumentException("mipmap level delta value (" + mipmapLevelDelta +
") must be greater than 0");
}
final ImageProcessor downSampledProcessor =
Downsampler.downsampleImageProcessor(sourceImagePlus.getProcessor(),
mipmapLevelDelta);
final BufferedImage downSampledImage = downSampledProcessor.getBufferedImage();
final ImageOutputStream imageOutputStream = new MemoryCacheImageOutputStream(outputStream);
Utils.writeImage(downSampledImage, format, false, jpegQuality, imageOutputStream);
}
示例14: write
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
@Override
public void write(final OutputStream outputStream)
throws IOException, WebApplicationException {
LOG.info("write: entry");
if (Utils.PNG_FORMAT.equals(format)) {
writePngImage(targetImage, 6, FilterType.FILTER_PAETH, outputStream);
} else if (Utils.TIFF_FORMAT.equals(format)) {
Utils.writeTiffImage(targetImage, outputStream);
} else {
final ImageOutputStream imageOutputStream = new MemoryCacheImageOutputStream(outputStream);
Utils.writeImage(targetImage, format, convertToGray, quality, imageOutputStream);
}
LOG.info("write: exit");
}
示例15: convert
import javax.imageio.stream.MemoryCacheImageOutputStream; //导入依赖的package包/类
public void convert(Node node, LayoutContext ctx, OutputStream out) throws IOException
{
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
JEuclidView view = new JEuclidView(node, ctx, graphics);
int ascentHeight = (int) Math.ceil((double) view.getAscentHeight());
int descentHeight = (int) Math.ceil((double) view.getDescentHeight());
int height = ascentHeight + descentHeight;
int width = (int) Math.ceil((double) view.getWidth());
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
graphics = image.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
view.draw(graphics, 0.0F, (float) ascentHeight);
ImageOutputStream imageOutputStream = new MemoryCacheImageOutputStream(out);
ImageWriter writer = getImageWriter(MimeType.PNG);
writer.setOutput(imageOutputStream);
writer.write(image);
imageOutputStream.close();
}