Example usage for javax.imageio.stream FileImageInputStream FileImageInputStream

List of usage examples for javax.imageio.stream FileImageInputStream FileImageInputStream

Introduction

In this page you can find the example usage for javax.imageio.stream FileImageInputStream FileImageInputStream.

Prototype

public FileImageInputStream(RandomAccessFile raf) 

Source Link

Document

Constructs a FileImageInputStream that will read from a given RandomAccessFile .

Usage

From source file:org.onehippo.forge.gallerymagick.core.command.ScalrProcessorUtils.java

/**
 * Creates and returns an {@link ImageReader} instance from the {@code sourceFile}.
 * @param sourceFile source file/*from w  ww .ja  v a  2s.c  om*/
 * @return an {@link ImageReader} instance
 * @throws IOException if IOException occurs
 */
private static ImageReader getImageReader(File sourceFile) throws IOException {
    ImageReader reader = null;
    String extension = FilenameUtils.getExtension(sourceFile.getName());
    Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix(extension);

    if (it.hasNext()) {
        reader = it.next();
        ImageInputStream input = new FileImageInputStream(sourceFile);
        reader.setInput(input);
    }

    return reader;
}

From source file:org.openmrs.obs.handler.ImageHandler.java

/**
 * Currently supports all views and puts the Image file data into the ComplexData object
 * /*from ww w  .  j av a 2  s.  co m*/
 * @see org.openmrs.obs.ComplexObsHandler#getObs(org.openmrs.Obs, java.lang.String)
 */
public Obs getObs(Obs obs, String view) {
    File file = getComplexDataFile(obs);

    // Raw image
    if (ComplexObsHandler.RAW_VIEW.equals(view)) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(file);
        } catch (IOException e) {
            log.error("Trying to read file: " + file.getAbsolutePath(), e);
        }

        ComplexData complexData = new ComplexData(file.getName(), img);

        // Image MIME type
        try {
            FileImageInputStream imgStream = new FileImageInputStream(file);
            Iterator<ImageReader> imgReader = ImageIO.getImageReaders(imgStream);
            imgStream.close();
            if (imgReader.hasNext()) {
                complexData.setMimeType("image/" + imgReader.next().getFormatName().toLowerCase());
            } else {
                log.warn("MIME type of " + file.getAbsolutePath() + " is not known");
            }
        } catch (FileNotFoundException e) {
            log.error("Image " + file.getAbsolutePath() + " was not found", e);
        } catch (IOException e) {
            log.error("Trying to determine MIME type of " + file.getAbsolutePath(), e);
        }

        obs.setComplexData(complexData);
    } else {
        // No other view supported
        // NOTE: if adding support for another view, don't forget to update supportedViews list above
        return null;
    }

    return obs;
}

From source file:org.psystems.dicomweb.Dcm2Dcm.java

/**
 * Recodes the images from the source transfer syntax, as read from the src
 * file, to the specified destination syntax.
 *//*w w w.j  av a2  s  . co  m*/
public void recodeImages(File src, File dest) throws IOException {

    ImageReader reader = new DicomImageReaderSpi().createReaderInstance();
    ImageWriter writer = new DicomImageWriterSpi().createWriterInstance();
    FileImageInputStream input = new FileImageInputStream(src);
    reader.setInput(input);
    if (dest.exists())
        dest.delete();
    FileImageOutputStream output = new FileImageOutputStream(dest);
    writer.setOutput(output);
    DicomStreamMetaData streamMeta = (DicomStreamMetaData) reader.getStreamMetadata();
    DicomObject ds = streamMeta.getDicomObject();
    DicomStreamMetaData writeMeta = (DicomStreamMetaData) writer.getDefaultStreamMetadata(null);
    DicomObject newDs = new BasicDicomObject();
    ds.copyTo(newDs);
    writeMeta.setDicomObject(newDs);
    int frames = ds.getInt(Tag.NumberOfFrames, 1);
    LookupTable lut = prepareBitStrip(writeMeta, reader);
    newDs.putString(Tag.TransferSyntaxUID, VR.UI, destinationSyntax.uid());
    if (overwriteObject != null) {
        overwriteObject.copyTo(newDs);
    }
    writer.prepareWriteSequence(writeMeta);
    for (int i = 0; i < frames; i++) {
        WritableRaster r = (WritableRaster) reader.readRaster(i, null);
        ColorModel cm = ColorModelFactory.createColorModel(ds);
        BufferedImage bi = new BufferedImage(cm, r, false, null);
        if (lut != null) {
            lut.lookup(bi.getRaster(), bi.getRaster());
        }
        IIOImage iioimage = new IIOImage(bi, null, null);
        writer.writeToSequence(iioimage, null);
    }
    writer.endWriteSequence();
    output.close();
    input.close();
}

From source file:org.psystems.dicomweb.Dcm2DcmCopy.java

/**
 * Recodes the images from the source transfer syntax, as read from the src
 * file, to the specified destination syntax.
 *//*  ww  w.j  a  v  a 2  s . c  om*/
public void recodeImages(File src, File dest) throws IOException {
    ImageReader reader = new DicomImageReaderSpi().createReaderInstance();
    ImageWriter writer = new DicomImageWriterSpi().createWriterInstance();
    FileImageInputStream input = new FileImageInputStream(src);
    reader.setInput(input);
    if (dest.exists())
        dest.delete();
    FileImageOutputStream output = new FileImageOutputStream(dest);
    writer.setOutput(output);
    DicomStreamMetaData streamMeta = (DicomStreamMetaData) reader.getStreamMetadata();
    DicomObject ds = streamMeta.getDicomObject();
    DicomStreamMetaData writeMeta = (DicomStreamMetaData) writer.getDefaultStreamMetadata(null);
    DicomObject newDs = new BasicDicomObject();
    ds.copyTo(newDs);
    writeMeta.setDicomObject(newDs);
    int frames = ds.getInt(Tag.NumberOfFrames, 1);
    LookupTable lut = prepareBitStrip(writeMeta, reader);
    newDs.putString(Tag.TransferSyntaxUID, VR.UI, destinationSyntax.uid());
    if (overwriteObject != null) {
        overwriteObject.copyTo(newDs);
    }
    writer.prepareWriteSequence(writeMeta);
    for (int i = 0; i < frames; i++) {
        WritableRaster r = (WritableRaster) reader.readRaster(i, null);
        ColorModel cm = ColorModelFactory.createColorModel(ds);
        BufferedImage bi = new BufferedImage(cm, r, false, null);
        if (lut != null) {
            lut.lookup(bi.getRaster(), bi.getRaster());
        }
        IIOImage iioimage = new IIOImage(bi, null, null);
        writer.writeToSequence(iioimage, null);
    }
    writer.endWriteSequence();
    output.close();
    input.close();
}