Example usage for javax.imageio.stream FileImageOutputStream flush

List of usage examples for javax.imageio.stream FileImageOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Usage

From source file:com.bc.util.io.FileUtils.java

public static void copy(File source, File target) throws IOException {
    final byte[] buffer = new byte[ONE_KB * ONE_KB];
    int bytesRead;

    FileImageInputStream sourceStream = null;
    FileImageOutputStream targetStream = null;

    try {/* ww  w.  jav a 2s.  c  o  m*/
        final File targetDir = target.getParentFile();
        if (!targetDir.isDirectory()) {
            if (!targetDir.mkdirs()) {
                throw new IOException("failed to create target directory: " + targetDir.getAbsolutePath());
            }
        }
        target.createNewFile();

        sourceStream = new FileImageInputStream(source);
        targetStream = new FileImageOutputStream(target);
        while ((bytesRead = sourceStream.read(buffer)) >= 0) {
            targetStream.write(buffer, 0, bytesRead);
        }
    } finally {
        if (sourceStream != null) {
            sourceStream.close();
        }
        if (targetStream != null) {
            targetStream.flush();
            targetStream.close();
        }
    }
}

From source file:edu.ku.brc.specify.extras.FishBaseInfoGetter.java

/**
 * Performs a "generic" HTTP request and fill member variable with results use
 * "getDigirResultsetStr" to get the results as a String
 *
 *///from   ww w  .ja  v  a2 s.c  o m
public Image getImage(final String fileName, final String url) {
    imageURL = url;

    String fullPath = tmpDir + File.separator + fileName;
    ////System.out.println(fullPath);
    File file = new File(fullPath);
    if (file.exists()) {
        try {
            return new ImageIcon(fullPath).getImage();

        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FishBaseInfoGetter.class, ex);
            ex.printStackTrace();
            status = ErrorCode.Error;
        }

    } else {
        byte[] bytes = super.doHTTPRequest(url);

        try {
            FileImageOutputStream fos = new FileImageOutputStream(file);
            fos.write(bytes);
            fos.flush();
            fos.close();

            return new ImageIcon(bytes).getImage();

        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FishBaseInfoGetter.class, ex);
            ex.printStackTrace();
            status = ErrorCode.Error;
        }
    }
    return null;
}

From source file:com.lynk.hrm.ui.dialog.InfoEmployee.java

private void updateData() {
    uiId.setText(employee.getId());/*  w w w.j av a2 s.c  o  m*/
    uiName.setText(employee.getName());
    uiNamePy.setText(employee.getNamePy());
    if (StringUtils.isNotEmpty(employee.getState())) {
        uiState.setSelectedItem(employee.getState());
        switch (employee.getState()) {
        case Employee.STATE_LEAVE:
        case Employee.STATE_RETIRE:
        case Employee.STATE_DELETE:
            uiInfoTab.setSelectedIndex(1);
            break;
        case Employee.STATE_SUSPEND:
            uiInfoTab.setSelectedIndex(2);
            break;
        case Employee.STATE_PROBATION:
        case Employee.STATE_CONTRACT:
        default:
            break;
        }
    }
    uiTimeCardLeaveCertify.setText(employee.getTimeCard());
    uiIdCard.setText(employee.getIdCard());
    uiBirthday.setText(employee.getBirthday());
    uiAge.setText(employee.getAge().toString());
    uiGender.setText(employee.getGender());
    if (StringUtils.isNotEmpty(employee.getMaritalStatus())) {
        uiMarital.setSelectedItem(employee.getMaritalStatus());
    }
    uiContact.setText(employee.getContact());
    if (StringUtils.isNotEmpty(employee.getCensus())) {
        uiCensus.setSelectedItem(employee.getCensus());
    }
    uiCensusAddress.setText(employee.getCensusAddress());

    // ?
    if (employee.getEmployeePhoto() != null && employee.getEmployeePhoto().getPhoto() != null
            && employee.getEmployeePhoto().getPhoto().length > 0) {
        EmployeePhoto photo = employee.getEmployeePhoto();
        try {
            File imageFile = new File(System.getProperty("user.dir") + "/temp/" + employee.getId() + ".png");
            FileImageOutputStream fios = new FileImageOutputStream(imageFile);
            fios.write(photo.getPhoto());
            fios.flush();
            fios.close();
            uiPhoto.putClientProperty("path", imageFile.getPath());
            uiPhoto.setImage(imageFile.getPath());
        } catch (Exception e) {
            showErrorMsg(e);
        }
    } else {
        uiPhoto.setImage("resource/image/default_emp_photo.png", true);
    }

    uiAddress.setText(employee.getAddress());
    if (StringUtils.isNotEmpty(employee.getCensusProvince())) {
        uiResidencePermit.setSelectedItem(employee.getCensusProvince());
    }
    if (StringUtils.isNotEmpty(employee.getDept())) {
        uiDept.setText(employee.getDept());
    }
    if (StringUtils.isNotEmpty(employee.getJob())) {
        uiJob.setText(employee.getJob());
    }
    if (StringUtils.isNotEmpty(employee.getEntryDate())) {
        uiEntryDate.setDate(Utils.dateFromStr(employee.getEntryDate()));
    }
    uiWorkAge.setText(UtilsClient.convertWorkAgeToStr(employee.getWorkAge()));
    if (StringUtils.isNotEmpty(employee.getProbationDate())) {
        uiProbation.setDate(Utils.dateFromStr(employee.getProbationDate()));
    }
    if (StringUtils.isNotEmpty(employee.getExpirationDate())) {
        uiExpiration.setText(employee.getExpirationDate());
    }
    if (StringUtils.isNotEmpty(employee.getDegree())) {
        uiDegree.setSelectedItem(employee.getDegree());
    }
    uiSchool.setText(employee.getSchool());
    uiMajor.setText(employee.getMajor());
    //      uiSuspendEnd.setText(employee.getSuspendEnd());
    uiWorkExperience.setText(employee.getWorkExperience());
    uiSocialCard.setText(employee.getSocialCard());
    uiHousingCard.setText(employee.getHousingCard());
    uiBankCard.setText(employee.getBankCard());
    uiNote.setText(employee.getNote());
    uiPerformance.setText(employee.getCensusCity());
    if (StringUtils.isNotEmpty(employee.getLeaveDate())) {
        uiLeaveDate.setDate(Utils.dateFromStr(employee.getLeaveDate()));
    }

    if (StringUtils.isNotEmpty(employee.getSocialEndDate())) {
        uiSocialEndDate.setText(employee.getSocialEndDate());
    }
    if (StringUtils.isNotEmpty(employee.getHousingEndDate())) {
        uiHousingEndDate.setText(employee.getHousingEndDate());
    }
    uiLeaveType.setSelectedItem(employee.getLeaveType());
    uiLeaveReason.setText(employee.getLeaveReason());
    uiFactory.setSelectedItem(employee.getSuspendNote());
    uiDdg.setSelectedItem(employee.getSuspendStart());
}

From source file:com.lynk.hrm.ui.dialog.InfoEmployee.java

private void initData(final String id) {
    showWaitPanel("?......");
    new SwingWorker<Employee, Void>() {

        @Override//w w w.  j  av a  2 s.  c om
        protected Employee doInBackground() throws Exception {
            if (StringUtils.isEmpty(id)) {
                return Employee.createNewEmployee();
            } else {
                return HrmBiz.getInstance().getEmployee(id);
            }
        }

        @Override
        protected void done() {
            try {
                employee = get();
                uiEntryDate.setCalendar(Calendar.getInstance());
                uiId.setText(employee.getId());
                uiName.setText(employee.getName());
                uiNamePy.setText(employee.getNamePy());
                if (StringUtils.isNotEmpty(employee.getState())) {
                    uiState.setSelectedItem(employee.getState());
                    switch (employee.getState()) {
                    case Employee.STATE_LEAVE:
                    case Employee.STATE_RETIRE:
                    case Employee.STATE_DELETE:
                        uiInfoTab.setSelectedIndex(1);
                        break;
                    case Employee.STATE_SUSPEND:
                        uiInfoTab.setSelectedIndex(2);
                        break;
                    case Employee.STATE_PROBATION:
                    case Employee.STATE_CONTRACT:
                    default:
                        break;
                    }
                }
                uiTimeCardLeaveCertify.setText(employee.getTimeCard());
                uiIdCard.setText(employee.getIdCard());
                uiBirthday.setText(employee.getBirthday());
                uiAge.setText(employee.getAge().toString());
                uiGender.setText(employee.getGender());
                if (StringUtils.isNotEmpty(employee.getMaritalStatus())) {
                    uiMarital.setSelectedItem(employee.getMaritalStatus());
                }
                uiContact.setText(employee.getContact());
                if (StringUtils.isNotEmpty(employee.getCensus())) {
                    uiCensus.setSelectedItem(employee.getCensus());
                }
                uiCensusAddress.setText(employee.getCensusAddress());

                // ?
                if (employee.getEmployeePhoto() != null && employee.getEmployeePhoto().getPhoto() != null
                        && employee.getEmployeePhoto().getPhoto().length > 0) {
                    EmployeePhoto photo = employee.getEmployeePhoto();
                    uiPhoto.putClientProperty("photo", employee.getEmployeePhoto().getPhoto());
                    try {
                        File imageFile = new File(
                                System.getProperty("user.dir") + "/temp/" + employee.getId() + ".png");
                        FileImageOutputStream fios = new FileImageOutputStream(imageFile);
                        fios.write(photo.getPhoto());
                        fios.flush();
                        fios.close();
                        uiPhoto.putClientProperty("path", imageFile.getPath());
                        uiPhoto.setImage(imageFile.getPath());
                    } catch (Exception e) {
                        showErrorMsg(e);
                    }
                } else {
                    uiPhoto.setImage("resource/image/default_emp_photo.png", true);
                }

                uiAddress.setText(employee.getAddress());
                if (StringUtils.isNotEmpty(employee.getCensusProvince())) {
                    uiResidencePermit.setSelectedItem(employee.getCensusProvince());
                }
                if (StringUtils.isNotEmpty(employee.getSuspendNote())) {
                    uiFactory.setSelectedItem(employee.getSuspendNote());
                }
                if (StringUtils.isNotEmpty(employee.getDept())) {
                    uiDept.setText(employee.getDept());
                }
                if (StringUtils.isNotEmpty(employee.getJob())) {
                    uiJob.setText(employee.getJob());
                }
                if (StringUtils.isNotEmpty(employee.getSuspendStart())) {
                    uiDdg.setSelectedItem(employee.getSuspendStart());
                }
                if (StringUtils.isNotEmpty(employee.getEntryDate())) {
                    uiEntryDate.setDate(Utils.dateFromStr(employee.getEntryDate()));
                }
                uiWorkAge.setText(UtilsClient.convertWorkAgeToStr(employee.getWorkAge()));
                if (StringUtils.isNotEmpty(employee.getProbationDate())) {
                    uiProbation.setDate(Utils.dateFromStr(employee.getProbationDate()));
                }
                if (StringUtils.isNotEmpty(employee.getExpirationDate())) {
                    uiExpiration.setText(employee.getExpirationDate());
                }
                if (StringUtils.isNotEmpty(employee.getDegree())) {
                    uiDegree.setSelectedItem(employee.getDegree());
                }
                uiSchool.setText(employee.getSchool());
                uiMajor.setText(employee.getMajor());
                uiWorkExperience.setText(employee.getWorkExperience());
                //               uiSuspendEnd.setText(employee.getSuspendEnd());
                uiSocialCard.setText(employee.getSocialCard());
                uiHousingCard.setText(employee.getHousingCard());
                uiBankCard.setText(employee.getBankCard());
                uiNote.setText(employee.getNote());
                uiPerformance.setText(employee.getCensusCity());
                uiPhoneShort.setText(employee.getPhoneShort());
                if (StringUtils.isNotEmpty(employee.getLeaveDate())) {
                    uiLeaveDate.setDate(Utils.dateFromStr(employee.getLeaveDate()));
                }

                if (StringUtils.isNotEmpty(employee.getSocialEndDate())) {
                    uiSocialEndDate.setText(employee.getSocialEndDate());
                }
                if (StringUtils.isNotEmpty(employee.getHousingEndDate())) {
                    uiHousingEndDate.setText(employee.getHousingEndDate());
                }
                uiLeaveType.setSelectedItem(employee.getLeaveType());
                uiLeaveReason.setText(employee.getLeaveReason());
                if (StringUtils.isNotEmpty(employee.getJobTitle())) {
                    uiGuide.setText(employee.getJobTitle());
                }
                hideWaitPanel();
            } catch (Exception e) {
                hideWaitPanel();
                showErrorMsg(e);
            }

        }

    }.execute();
}

From source file:org.meveo.admin.util.ModuleUtil.java

public static void cropPicture(String filename, CroppedImage croppedImage) throws Exception {
    FileImageOutputStream imageOutput = new FileImageOutputStream(new File(filename));
    imageOutput.write(croppedImage.getBytes(), 0, croppedImage.getBytes().length);
    imageOutput.flush();
    imageOutput.close();/*www.j a  va 2s . c om*/
}

From source file:org.tinymediamanager.core.ImageCache.java

/**
 * Cache image.//from www .ja v  a2s .  c  o  m
 * 
 * @param mf
 *          the media file
 * @return the file the cached file
 * @throws Exception
 */
public static Path cacheImage(Path originalFile) throws Exception {
    MediaFile mf = new MediaFile(originalFile);
    Path cachedFile = ImageCache.getCacheDir()
            .resolve(getMD5(originalFile.toString()) + "." + Utils.getExtension(originalFile));
    if (!Files.exists(cachedFile)) {
        // check if the original file exists && size > 0
        if (!Files.exists(originalFile)) {
            throw new FileNotFoundException("unable to cache file: " + originalFile + "; file does not exist");
        }
        if (Files.size(originalFile) == 0) {
            throw new EmptyFileException(originalFile);
        }

        // recreate cache dir if needed
        // rescale & cache
        BufferedImage originalImage = null;
        try {
            originalImage = createImage(originalFile);
        } catch (Exception e) {
            throw new Exception("cannot create image - file seems not to be valid? " + originalFile);
        }

        // calculate width based on MF type
        int desiredWidth = originalImage.getWidth(); // initialize with fallback
        switch (mf.getType()) {
        case FANART:
            if (originalImage.getWidth() > 1000) {
                desiredWidth = 1000;
            }
            break;

        case POSTER:
            if (originalImage.getHeight() > 500) {
                desiredWidth = 350;
            }
            break;

        case EXTRAFANART:
        case THUMB:
        case BANNER:
        case GRAPHIC:
            desiredWidth = 300;
            break;

        default:
            break;
        }

        // special handling for movieset-fanart or movieset-poster
        if (mf.getFilename().startsWith("movieset-fanart") || mf.getFilename().startsWith("movieset-poster")) {
            if (originalImage.getWidth() > 1000) {
                desiredWidth = 1000;
            }
        }

        Point size = calculateSize(desiredWidth, (int) (originalImage.getHeight() / 1.5),
                originalImage.getWidth(), originalImage.getHeight(), true);
        BufferedImage scaledImage = null;

        if (Globals.settings.getImageCacheType() == CacheType.FAST) {
            // scale fast
            scaledImage = Scalr.resize(originalImage, Scalr.Method.BALANCED, Scalr.Mode.FIT_EXACT, size.x,
                    size.y);
        } else {
            // scale with good quality
            scaledImage = Scalr.resize(originalImage, Scalr.Method.QUALITY, Scalr.Mode.FIT_EXACT, size.x,
                    size.y);
        }
        originalImage = null;

        ImageWriter imgWrtr = null;
        ImageWriteParam imgWrtrPrm = null;

        // here we have two different ways to create our thumb
        // a) a scaled down jpg/png (without transparency) which we have to modify since OpenJDK cannot call native jpg encoders
        // b) a scaled down png (with transparency) which we can store without any more modifying as png
        if (hasTransparentPixels(scaledImage)) {
            // transparent image -> png
            imgWrtr = ImageIO.getImageWritersByFormatName("png").next();
            imgWrtrPrm = imgWrtr.getDefaultWriteParam();

        } else {
            // non transparent image -> jpg
            // convert to rgb
            BufferedImage rgb = new BufferedImage(scaledImage.getWidth(), scaledImage.getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            ColorConvertOp xformOp = new ColorConvertOp(null);
            xformOp.filter(scaledImage, rgb);
            imgWrtr = ImageIO.getImageWritersByFormatName("jpg").next();
            imgWrtrPrm = imgWrtr.getDefaultWriteParam();
            imgWrtrPrm.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
            imgWrtrPrm.setCompressionQuality(0.80f);

            scaledImage = rgb;
        }

        FileImageOutputStream output = new FileImageOutputStream(cachedFile.toFile());
        imgWrtr.setOutput(output);
        IIOImage image = new IIOImage(scaledImage, null, null);
        imgWrtr.write(null, image, imgWrtrPrm);
        imgWrtr.dispose();
        output.flush();
        output.close();
        scaledImage = null;
    }

    if (!Files.exists(cachedFile)) {
        throw new Exception("unable to cache file: " + originalFile);
    }

    return cachedFile;
}