2016-06-15 24 views
1

javaで画像を処理している経験はありません。私の目標はいくつかの画像を結合することです。より詳細には、テンプレート画像と他の画像があります。それらの画像を特定の場所のテンプレート画像に入れたいと思います。java、複数の画像から画像を作成

テンプレート画像

enter image description here

特定の画像

enter image description here

だから、私は猫の画像の場所への犬の画像を配置するなどのために 作成した画像を保存することができます。

最も簡単な方法を教えてください。これを行うにはどうすればよいですか?

+0

あなたはそれらの画像がどこにあるのか教えていただけますか?例えばインターネットから?あなたのローカルディレクトリから? –

+0

@YuZhang:画像はローカルディレクトリに保存されます。 –

+0

'最も簡単な方法は何ですか?'あなたが試したことを投稿してください。 – Frakcool

答えて

5

ファビアンは私の提案は、あなたがテンプレートを制御し、オプションとしてユーザーに提供する場合は、あなたがそれらを自分で実装することができ、代替

可能となりので、パターンを識別することは、期待される結果が得られない場合があります指摘したように、プレースホルダーノードに画像を取り込みます。マージされた画像は、私は簡単な例を含めました全体のスナップショット

を取ってから来るが、それは完全には実装されていないだということに注意してください(レイアウトなど)ので、コンセプトの証拠として多くのことを考えるでしょう。あなたが提供した画像の例をもっと近くに表現するために、同時に異なる画像を表示することも可能です。

これはという最も簡単な方法ですが、楽しく学べます。

enter image description here enter image description here

保存されたスナップショット:

あなたはJavaで


public class ImageTemplateNode extends Region{ 
    private SimpleObjectProperty<Image> displayedImageProperty; 
    private ObservableList<Node> children = FXCollections.observableArrayList(); 
    private Random random = new Random(); 
    private int rows, columns; 
    private final int maximumRotation = 15; 

    public ImageTemplateNode(int rows, int cols, Image imageToDisplay){ 
     this.rows = rows; 
     this.columns = cols; 
     this.displayedImageProperty = new SimpleObjectProperty<>(imageToDisplay); 
     createDisplayNodes(); 
     setPadding(new Insets(10)); 
     Bindings.bindContentBidirectional(getChildren(), children); 
    } 

    public ImageTemplateNode(int rows, int cols, Image imageToDisplay, Image backgroundImage){ 
     this(rows, cols, imageToDisplay); 
     setBackgroundImage(backgroundImage); 
    } 

    private void createDisplayNodes(){ 
     for(int count = 0; count < (rows * columns); count++){ 
      StackPane container = new StackPane(); 
      container.setRotate(getRandomRotationValue()); 
      container.setBackground(
        new Background(new BackgroundFill(getRandomColour(), new CornerRadii(5), new Insets(5)))); 
      container.maxWidthProperty().bind(displayedImageProperty.get().widthProperty().add(25)); 
      container.maxHeightProperty().bind(displayedImageProperty.get().heightProperty().add(25)); 

      ImageView displayNode = new ImageView(); 
      displayNode.imageProperty().bind(displayedImageProperty); 
      displayNode.fitWidthProperty().bind(container.widthProperty().subtract(25)); 
      displayNode.fitHeightProperty().bind(container.heightProperty().subtract(25)); 

      container.getChildren().setAll(displayNode); 
      children.add(container); 
     } 
    } 

    private int getRandomRotationValue(){ 
     int randomValue = random.nextInt(maximumRotation); 
     //Rotate clockwise if even, anti-clockwise if odd 
     return randomValue % 2 == 0 ? randomValue : 360 - randomValue; 
    } 

    private Color getRandomColour(){ 
     int red = random.nextInt(256); 
     int green = random.nextInt(256); 
     int blue = random.nextInt(256); 
     return Color.rgb(red, green, blue); 
    } 

    @Override 
    protected void layoutChildren() { 
     //Calculate the dimensions for the children so that they do not breach the padding and allow for rotation 
     double cellWidth = (widthProperty().doubleValue() 
       - getPadding().getLeft() - getPadding().getRight() - maximumRotation)/columns; 
     double cellHeight = (heightProperty().doubleValue() 
       - getPadding().getTop() - getPadding().getBottom() - maximumRotation)/rows; 

     for (int i = 0; i < (rows); i++) { 
      for (int j = 0; j < (columns); j++) { 
       if (children.size() <= ((i * (columns)) + j)) { 
        break; 
       } 
       Node childNode = children.get((i * (columns)) + j); 
       layoutInArea(childNode, 
         (j * cellWidth) + getPadding().getLeft(), 
         (i * cellHeight) + getPadding().getTop(), cellWidth, cellHeight, 
         0.0d, HPos.CENTER, VPos.CENTER); 
      } 
     } 
    } 

    public void setBackgroundImage(Image backgroundImage){ 
     setBackground(new Background(
       new BackgroundImage(backgroundImage, 
         BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.CENTER, 
         BackgroundSize.DEFAULT))); 
    } 

    public void changeDisplayImage(Image newImageToDisplay){ 
     displayedImageProperty.set(newImageToDisplay); 
    } 

    public void captureAndSaveDisplay(){ 
     FileChooser fileChooser = new FileChooser(); 

     //Set extension filter 
     fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("png files (*.png)", "*.png")); 

     //Prompt user to select a file 
     File file = fileChooser.showSaveDialog(null); 

     if(file != null){ 
      try { 
       //Pad the capture area 
       WritableImage writableImage = new WritableImage((int)getWidth() + 20, 
         (int)getHeight() + 20); 
       snapshot(null, writableImage); 
       RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null); 
       //Write the snapshot to the chosen file 
       ImageIO.write(renderedImage, "png", file); 
      } catch (IOException ex) { ex.printStackTrace(); } 
     } 
    } 
} 

スクリーンショットを画像処理の経験を持っていないので、これはまた、実行可能な選択肢である可能性がありenter image description here enter image description here

関連する問題