Java Code Examples for javafx.embed.swing.SwingFXUtils#fromFXImage()

The following examples show how to use javafx.embed.swing.SwingFXUtils#fromFXImage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EpidemicReportsChartController.java    From MyBox with Apache License 2.0 6 votes vote down vote up
protected File snapWidth(SnapshotParameters snapPara, double scale, int width, Region chart) {
    try {
        Bounds bounds = chart.getLayoutBounds();
        int imageWidth = (int) Math.round(bounds.getWidth() * scale);
        int imageHeight = (int) Math.round(bounds.getHeight() * scale);
        Image snap = chart.snapshot(snapPara, new WritableImage(imageWidth, imageHeight));
        BufferedImage bufferedImage = SwingFXUtils.fromFXImage(snap, null);
        if (bufferedImage.getWidth() > width) {
            bufferedImage = ImageManufacture.scaleImageWidthKeep(bufferedImage, snapWidth);
        }
        File tmpfile = FileTools.getTempFile(".png");
        ImageFileWriters.writeImageFile(bufferedImage, "png", tmpfile.getAbsolutePath());
        return tmpfile;
    } catch (Exception e) {
        logger.debug(e.toString());
        return null;
    }
}
 
Example 2
Source File: MainController.java    From gitPic with MIT License 6 votes vote down vote up
/**
 * 从剪贴板复制
 */
@FXML
protected void copyByClipboard() {
    Clipboard clipboard = Clipboard.getSystemClipboard();
    if (clipboard.hasImage()) {
        Image image = clipboard.getImage();
        BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
        try {
            Path tempDirectory = Files.createTempDirectory(Constants.GITPIC_PREFIX);
            String tempFile = tempDirectory.toString() + File.separator + Constants.GITPIC_PREFIX + System.currentTimeMillis() + ".png";
            File file = new File(tempFile);
            ImageIO.write(bImage, "png", file);
            uploadImgFilePath = file.getAbsolutePath();
            copyAndGenerate();
            file.delete();// 删除临时图片
            Files.delete(tempDirectory);//删除临时目录
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            showErrorMessage("从剪切板拷贝图片异常", e);
        }
    } else {
        showErrorMessage("剪切板中没有图片");
    }
}
 
Example 3
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static Image blendImages(Image foreImage, Image backImage,
        ImagesRelativeLocation location, int x, int y,
        boolean intersectOnly, ImagesBlendMode blendMode, float opacity) {
    if (foreImage == null || backImage == null || blendMode == null) {
        return null;
    }
    BufferedImage source1 = SwingFXUtils.fromFXImage(foreImage, null);
    BufferedImage source2 = SwingFXUtils.fromFXImage(backImage, null);
    BufferedImage target = ImageBlend.blendImages(source1, source2,
            location, x, y, intersectOnly, blendMode, opacity);
    if (target == null) {
        target = source1;
    }
    Image newImage = SwingFXUtils.toFXImage(target, null);
    return newImage;
}
 
Example 4
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image addShadowAlpha(Image image, int shadow, Color color) {
    if (image == null || shadow <= 0) {
        return image;
    }
    BufferedImage source = SwingFXUtils.fromFXImage(image, null);
    BufferedImage target = mara.mybox.image.ImageManufacture.addShadowAlpha(source, shadow, FxmlImageManufacture.toAwtColor(color));
    Image newImage = SwingFXUtils.toFXImage(target, null);
    return newImage;
}
 
Example 5
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image scaleImage(Image image, int width, int height) {
    if (width == image.getWidth() && height == image.getHeight()) {
        return image;
    }
    BufferedImage source = SwingFXUtils.fromFXImage(image, null);
    BufferedImage target = mara.mybox.image.ImageManufacture.scaleImage(source, width, height);
    Image newImage = SwingFXUtils.toFXImage(target, null);
    return newImage;
}
 
Example 6
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image scaleImage(Image image, int width, int height,
        boolean keepRatio, int keepType) {
    BufferedImage source = SwingFXUtils.fromFXImage(image, null);
    BufferedImage target = mara.mybox.image.ImageManufacture.scaleImage(source, width, height, keepRatio, keepType);
    Image newImage = SwingFXUtils.toFXImage(target, null);
    return newImage;
}
 
Example 7
Source File: Data.java    From houdoku with MIT License 5 votes vote down vote up
/**
 * Save a JavaFX Image to the filesystem.
 *
 * @param image the Image to save
 * @param path  the Path to save the image
 * @throws IOException an IOException occurred when writing to the file
 */
private static void saveImage(Image image, Path path) throws IOException {
    // ensure path to file exists
    Files.createDirectories(path.getParent());

    BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null);
    // convert to BufferedImage.TYPE_INT_RGB
    BufferedImage convertedImg = new BufferedImage(bufferedImage.getWidth(),
            bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
    convertedImg.getGraphics().drawImage(bufferedImage, 0, 0, null);
    ImageIO.write(convertedImg, "jpg", path.toFile());
}
 
Example 8
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image addShadowNoAlpha(Image image, int shadow, Color color) {
    if (image == null || shadow <= 0) {
        return image;
    }
    BufferedImage source = SwingFXUtils.fromFXImage(image, null);
    BufferedImage target = mara.mybox.image.ImageManufacture.addShadowNoAlpha(source, shadow, FxmlImageManufacture.toAwtColor(color));
    Image newImage = SwingFXUtils.toFXImage(target, null);
    return newImage;
}
 
Example 9
Source File: ImageOCRController.java    From MyBox with Apache License 2.0 5 votes vote down vote up
@FXML
protected void scale() {
    if (isSettingValues || imageView.getImage() == null || scale <= 0) {
        return;
    }
    synchronized (this) {
        if (task != null) {
            return;
        }
        task = new SingletonTask<Void>() {
            private Image ocrImage;

            @Override
            protected boolean handle() {
                try {
                    BufferedImage bufferedImage = SwingFXUtils.fromFXImage(imageView.getImage(), null);
                    bufferedImage = ImageManufacture.scaleImage(bufferedImage, scale);
                    ocrImage = SwingFXUtils.toFXImage(bufferedImage, null);
                    return ocrImage != null;
                } catch (Exception e) {
                    error = e.toString();
                    return false;
                }
            }

            @Override
            protected void whenSucceeded() {
                setPreprocessImage(ocrImage);

            }

        };
        openHandlingStage(task, Modality.WINDOW_MODAL);
        Thread thread = new Thread(task);
        thread.setDaemon(true);
        thread.start();
    }
}
 
Example 10
Source File: ImageOCRController.java    From MyBox with Apache License 2.0 5 votes vote down vote up
@FXML
protected void binary() {
    if (isSettingValues || imageView.getImage() == null || threshold <= 0) {
        return;
    }
    synchronized (this) {
        if (task != null) {
            return;
        }
        task = new SingletonTask<Void>() {
            private Image ocrImage;

            @Override
            protected boolean handle() {
                try {
                    BufferedImage bufferedImage = SwingFXUtils.fromFXImage(imageView.getImage(), null);
                    ImageBinary bin = new ImageBinary(bufferedImage, threshold);
                    bufferedImage = bin.operateImage();
                    ocrImage = SwingFXUtils.toFXImage(bufferedImage, null);
                    return ocrImage != null;
                } catch (Exception e) {
                    error = e.toString();
                    return false;
                }
            }

            @Override
            protected void whenSucceeded() {
                setPreprocessImage(ocrImage);

            }

        };
        openHandlingStage(task, Modality.WINDOW_MODAL);
        Thread thread = new Thread(task);
        thread.setDaemon(true);
        thread.start();
    }
}
 
Example 11
Source File: ImageBinary.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image binary(Image image) {
    try {
        BufferedImage bm = SwingFXUtils.fromFXImage(image, null);
        bm = byteBinary(bm);
        return SwingFXUtils.toFXImage(bm, null);
    } catch (Exception e) {
        logger.error(e.toString());
        return image;
    }
}
 
Example 12
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image indicateCircle(Image image,
        Color color, int lineWidth, DoubleCircle circle) {
    BufferedImage source = SwingFXUtils.fromFXImage(image, null);
    BufferedImage target = mara.mybox.image.ImageScope.indicateCircle(source,
            FxmlImageManufacture.toAwtColor(color), lineWidth, circle);
    Image newImage = SwingFXUtils.toFXImage(target, null);
    return newImage;
}
 
Example 13
Source File: ImageContrast.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public static Image grayHistogramEqualization(Image grayImage) {
    BufferedImage image = SwingFXUtils.fromFXImage(grayImage, null);
    image = mara.mybox.image.ImageContrast.grayHistogramEqualization(image);
    return SwingFXUtils.toFXImage(image, null);
}
 
Example 14
Source File: DesktopSetup.java    From mzmine3 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void run() {

  logger.finest("Configuring desktop settings");

  // Set basic desktop handlers
  final Desktop awtDesktop = Desktop.getDesktop();
  if (awtDesktop != null) {

    // Setup About handler
    if (awtDesktop.isSupported(Desktop.Action.APP_ABOUT)) {
      awtDesktop.setAboutHandler(e -> {
        MZmineGUI.showAboutWindow();
      });
    }

    // Setup Quit handler
    if (awtDesktop.isSupported(Desktop.Action.APP_QUIT_HANDLER)) {
      awtDesktop.setQuitHandler((e, response) -> {
        ExitCode exitCode = MZmineCore.getDesktop().exitMZmine();
        if (exitCode == ExitCode.OK)
          response.performQuit();
        else
          response.cancelQuit();
      });
    }
  }

  if (Taskbar.isTaskbarSupported()) {

    final Taskbar taskBar = Taskbar.getTaskbar();

    // Set the main app icon
    if ((mzMineIcon != null) && taskBar.isSupported(Taskbar.Feature.ICON_IMAGE)) {
      final java.awt.Image mzMineIconAWT = SwingFXUtils.fromFXImage(mzMineIcon, null);
      taskBar.setIconImage(mzMineIconAWT);
    }

    // Add a task controller listener to show task progress
    MZmineCore.getTaskController().addTaskControlListener((numOfWaitingTasks, percentDone) -> {
      if (numOfWaitingTasks > 0) {
        if (taskBar.isSupported(Taskbar.Feature.ICON_BADGE_NUMBER)) {
          String badge = String.valueOf(numOfWaitingTasks);
          taskBar.setIconBadge(badge);
        }

        if (taskBar.isSupported(Taskbar.Feature.PROGRESS_VALUE))
          taskBar.setProgressValue(percentDone);

      } else {

        if (taskBar.isSupported(Taskbar.Feature.ICON_BADGE_NUMBER))
          taskBar.setIconBadge(null);
        /*
         * if (taskBar.isSupported( Taskbar.Feature.PROGRESS_STATE_WINDOW))
         * taskBar.setWindowProgressState( MZmineCore.getDesktop().getMainWindow(),
         * Taskbar.State.OFF);
         */
        if (taskBar.isSupported(Taskbar.Feature.PROGRESS_VALUE))
          taskBar.setProgressValue(-1);
        /*
         * if (taskBar.isSupported( Taskbar.Feature.PROGRESS_VALUE_WINDOW))
         * taskBar.setWindowProgressValue( MZmineCore.getDesktop().getMainWindow(), -1);
         */
      }
    });

  }

  // Let the OS decide the location of new windows. Otherwise, all windows
  // would appear at the top left corner by default.
  // TODO: investigate if this applies to JavaFX windows
  System.setProperty("java.awt.Window.locationByPlatform", "true");

}
 
Example 15
Source File: PdfViewController.java    From MyBox with Apache License 2.0 4 votes vote down vote up
@FXML
public void startOCR() {
    checkLanguages();
    if (imageView.getImage() == null
            || selectedLanguages == null || selectedLanguages.isEmpty()) {
        return;
    }
    synchronized (this) {
        if (task != null) {
            return;
        }
        task = new SingletonTask<Void>() {

            private String result;

            @Override
            protected boolean handle() {
                try {
                    ITesseract instance = new Tesseract();
                    instance.setTessVariable("user_defined_dpi", "96");
                    instance.setTessVariable("debug_file", "/dev/null");
                    String path = AppVariables.getUserConfigValue("TessDataPath", null);
                    if (path != null) {
                        instance.setDatapath(path);
                    }
                    if (selectedLanguages != null) {
                        instance.setLanguage(selectedLanguages);
                    }

                    Image selected = cropImage();
                    if (selected == null) {
                        selected = imageView.getImage();
                    }
                    BufferedImage bufferedImage = SwingFXUtils.fromFXImage(selected, null);
                    if (task == null || isCancelled()) {
                        return false;
                    }
                    result = instance.doOCR(bufferedImage);
                    return result != null;
                } catch (Exception e) {
                    error = e.toString();
                    return false;
                }
            }

            @Override
            protected void whenSucceeded() {
                if (result.length() == 0) {
                    popText(message("OCRMissComments"), 5000, "white", "1.1em", null);
                }
                ocrArea.setText(result);
                resultLabel.setText(MessageFormat.format(message("OCRresults"),
                        result.length(), DateTools.showTime(cost)));

                orcPage = currentPage;
            }

        };
        openHandlingStage(task, Modality.WINDOW_MODAL);
        Thread thread = new Thread(task);
        thread.setDaemon(true);
        thread.start();
    }

}
 
Example 16
Source File: ImageOCRController.java    From MyBox with Apache License 2.0 4 votes vote down vote up
@FXML
public void savePreprocessAction() {
    if (imageView.getImage() == null) {
        return;
    }
    synchronized (this) {
        if (task != null) {
            return;
        }

        String name = null;
        if (sourceFile != null) {
            name = FileTools.getFilePrefix(sourceFile.getName()) + "_preprocessed";
        }
        final File file = chooseSaveFile(AppVariables.getUserConfigPath("ImageFilePath"),
                name, CommonFxValues.ImageExtensionFilter, true);
        if (file == null) {
            return;
        }
        recordFileWritten(file);

        task = new SingletonTask<Void>() {

            @Override
            protected boolean handle() {
                String format = FileTools.getFileSuffix(file.getName());
                BufferedImage bufferedImage = SwingFXUtils.fromFXImage(imageView.getImage(), null);
                return ImageFileWriters.writeImageFile(bufferedImage, format, file.getAbsolutePath());
            }

            @Override
            protected void whenSucceeded() {
                if (LoadCheck.isSelected()) {
                    sourceFileChanged(file);
                }
            }

        };
        openHandlingStage(task, Modality.WINDOW_MODAL);
        Thread thread = new Thread(task);
        thread.setDaemon(true);
        thread.start();
    }
}
 
Example 17
Source File: RCHandler.java    From Quelea with GNU General Public License v3.0 4 votes vote down vote up
public static byte[] getPresentationSlides(HttpExchange he) {
    String targetPath = he.getRequestURI().getPath().replace("/slides", "");
    Displayable d = QueleaApp.get().getMainWindow().getMainPanel().getLivePanel().getDisplayable();
    if (d instanceof PresentationDisplayable || d instanceof PdfDisplayable || d instanceof ImageGroupDisplayable) {
        if (targetPath.contains("/")) {
            try {
                BufferedImage image;
                int slide = Integer.parseInt(targetPath.replace("/slide", "").replace(".png", ""));
                if (d instanceof PresentationDisplayable) {
                    image = SwingFXUtils.fromFXImage(((PresentationDisplayable) d).getPresentation().getSlide(slide - 1).getImage(), null);
                } else if (d instanceof PdfDisplayable) {
                    image = SwingFXUtils.fromFXImage(((PdfDisplayable) d).getPresentation().getSlide(slide - 1).getImage(), null);
                } else {
                    image = SwingFXUtils.fromFXImage(((ImageGroupDisplayable) d).getPresentation().getSlide(slide - 1).getImage(), null);
                }
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                ImageIO.write(image, "png", output);
                return output.toByteArray();
            } catch (IOException ex) {
                LOGGER.log(Level.WARNING, "Error getting PowerPoint slides", ex);
            }
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append("\n<html>");
            int numberOfFiles;
            if (d instanceof PresentationDisplayable)
                numberOfFiles = ((PresentationDisplayable) d).getPresentation().getSlides().length;
            else if (d instanceof PdfDisplayable)
                numberOfFiles = ((PdfDisplayable) d).getPresentation().getSlides().length;
            else
                numberOfFiles = ((ImageGroupDisplayable) d).getPresentation().getSlides().length;
            for (int i = 0; i < numberOfFiles; i++) {
                if (currentLyricSection() == i) {
                    sb.append("<div class=\"inner current\">");
                }
                sb.append("<p class=\"empty\" onclick=\"section(").append(i).append(");\">");
                sb.append("<a href='").append("/").append("slides/slide").append(i).append("'>").append("</a>");
                sb.append("</p>");
            }
            sb.append("\n</html>");
            return sb.toString().getBytes();
        }
    }
    return "".getBytes();
}
 
Example 18
Source File: ImageGray.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public ImageGray(Image image) {
    this.image = SwingFXUtils.fromFXImage(image, null);
    this.operationType = OperationType.Gray;
}
 
Example 19
Source File: ImageContrast.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public ImageContrast(Image image, ContrastAlgorithm algorithm) {
    this.image = SwingFXUtils.fromFXImage(image, null);
    this.operationType = OperationType.Contrast;
    this.algorithm = algorithm;
}
 
Example 20
Source File: ImageGray.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public ImageGray(Image image, ImageScope scope) {
    this.image = SwingFXUtils.fromFXImage(image, null);
    this.operationType = OperationType.Gray;
    this.scope = scope;
}