2016-06-07 67 views
1

我的問題與this previous question有關。我想要實現的是堆疊圖像(它們具有透明度),在頂部寫一個字符串,然後以全分辨率保存照片合成/照片膠印。是否可以在Codename One中保存生成的圖像?

@Override 
protected void beforeMain(Form f) { 


    Image photoBase = fetchResourceFile().getImage("Voiture_4_3.jpg"); 
    Image watermark = fetchResourceFile().getImage("Watermark.png"); 


    f.setLayout(new LayeredLayout()); 
    final Label drawing = new Label(); 
    f.addComponent(drawing); 


    // Image mutable dans laquelle on va dessiner (fond blanc) 
    Image mutableImage = Image.createImage(photoBase.getWidth(), photoBase.getHeight()); 
    drawing.getUnselectedStyle().setBgImage(mutableImage); 
    drawing.getUnselectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT); 

    // Paint all the stuff 
    paints(mutableImage.getGraphics(), photoBase, watermark, photoBase.getWidth(), photoBase.getHeight()); 

    // Save the collage 
    Image screenshot = Image.createImage(photoBase.getWidth(), photoBase.getHeight()); 
    f.revalidate(); 
    f.setVisible(true); 
    drawing.paintComponent(screenshot.getGraphics(), true); 

    String imageFile = FileSystemStorage.getInstance().getAppHomePath() + "screenshot.png"; 
    try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(imageFile)) { 
     ImageIO.getImageIO().save(screenshot, os, ImageIO.FORMAT_PNG, 1); 
    } catch(IOException err) { 
     err.printStackTrace(); 
    } 

} 

public void paints(Graphics g, Image background, Image watermark, int width, int height) { 

    g.drawImage(background, 0, 0); 
    g.drawImage(watermark, 0, 0); 
    g.setColor(0xFF0000); 

    // Upper left corner 
    g.fillRect(0, 0, 10, 10); 

    // Lower right corner 
    g.setColor(0x00FF00); 
    g.fillRect(width - 10, height - 10, 10, 10); 

    g.setColor(0xFF0000); 
    Font f = Font.createTrueTypeFont("Geometos", "Geometos.ttf").derive(220, Font.STYLE_BOLD); 
    g.setFont(f); 
    // Draw a string right below the M from Mercedes on the car windscreen (measured in Gimp) 
    g.drawString("HelloWorld", 
      (int) (848), 
      (int) (610) 
      ); 

} 

這是保存的截圖,我得到,如果我使用Iphone6皮膚Screenshot I get(有效負載圖像是比原來的小,居中)。如果我使用Xoom皮膚,這是我得到的Screenshot I get with Xoom(有效載荷圖像仍然比原始圖像小,但它已經移動到左側)。

所以總結這一切:爲什麼是保存的截圖與Xoom的皮膚從一個我與iPhone的皮膚得到不同?無論如何要直接保存我在塗料方法中繪製的圖形,以便保存的圖像具有原始尺寸?

非常感謝任何人,可以幫助我:-)!

乾杯,

+0

這是一個有點難受,按照前面的問題,因爲它是非常長的。我建議將問題分解爲較小的個人問題以獲得更好的答案。隨意問你喜歡的任何問題,只要他們的措辭很好/研究 –

+0

@Shai上一篇文章太長了。你是否建議編輯它並使其成爲KISS? – HelloWorld

+0

我建議將來寫更多的小問題。 –

回答

1

您可以使用ImageIO類保存在一個代號的圖像。請注意,您可以使用paintComponent(Graphics)方法將容器層次結構繪製爲可變圖像。

您可以在可變或通過佈局做戰平圖像這兩種方法。就我個人而言,我總是喜歡佈局,因爲我喜歡抽象,但我不會說可變圖像的方法是對/錯。

請注意,如果您更改/重繪了很多圖片,那麼可變圖像會比較慢(對於常規代碼或模擬器而言,這些圖像不會顯而易見),因爲它們被迫使用軟件渲染器,並且無法完全使用GPU。

在上一問題看來你放置的圖像與「FIT」的風格,自然吸引了比,容器容納體積更小,然後畫上手動它上面的圖像...這是有問題的。

一種解決方案是手動繪製的一切,但,那麼你將需要做自己繪製的「適合」的方面。如果您使用佈局,則應根據佈局包括繪圖/文本來定位所有內容。

+0

感謝您的指導!我將嘗試繪製可變圖像,以便結果具有與來自相機的原始圖像相同的分辨率。只要我得到的東西,我會發布我使用的代碼。 – HelloWorld

+0

這可能是微不足道的@Shai,但你是什麼意思的「手動」?如果有可能的話,一個例子會很棒! – HelloWorld

+0

當我說手動我的意思是使用drawString,drawImage等,就像你直接在圖形上,而不是paintComponent。 –