有誰知道如何用Java打印BufferedImage?用Java打印BufferedImage
回答
打印就像在屏幕上繪圖一樣,所以最終你會得到一個Graphics對象,並且你只需將Image繪製到它中。
thx,現在它工作正常 – 2011-03-12 17:30:33
@FelixKnorr你可以請代碼spinet打印緩衝的圖像作爲我也有同樣的需求?謝謝 – Jony 2012-06-11 11:12:52
我不確定你打印的意思。在打印機上打印?打印到標準?寫入文件?
你可以看看這個教程從太陽。如果你想寫一個文件。用你喜歡的圖像工具打開並從那裏打印。
http://download.oracle.com/javase/tutorial/2d/images/saveimage.html
如果你正在構建一個圖形應用程序,並使用AWT一樣東西,你可以使用java打印API這可能需要一些踢踏舞。
爲什麼要放兩次相同的網址? – 2011-03-12 16:55:19
我的意思是在打印機上打印 – 2011-03-12 17:23:55
@chuck證據不足? – nsfyn55 2011-03-12 17:26:23
我試圖做到這一點通過擴展的BufferedImage實現打印接口像這樣(代碼拼貼我從網上發現身邊):
package components;
import java.awt.image.PixelGrabber;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.applet.Applet;
import javax.imageio.*;
import java.awt.Image.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.net.*;
import java.lang.*;
import java.io.*;
import java.io.File;
import java.lang.Integer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.RenderingHints;
import java.lang.String;
import java.io.File;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.lang.ClassLoader.*;
import java.lang.Class;
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.attribute.standard.MediaSize.*;
import javax.print.event.*;
import java.awt.MediaTracker;
import java.awt.Color;
import java.awt.*;
import java.awt.print.*;
import javax.print.attribute.*;
import javax.swing.UIManager;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.util.Hashtable;
import java.awt.image.IndexColorModel;
import java.awt.print.Printable.*;
import java.awt.Rectangle;
import java.awt.Graphics2D;
public class printableBufferedImage extends BufferedImage implements Printable
{
public printableBufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable properties)
{
super(cm, raster, isRasterPremultiplied, properties);
}
public printableBufferedImage(int width, int height, int imageType)
{
super(width, height, imageType);
}
public printableBufferedImage(int width, int height, int imageType, IndexColorModel cm)
{
super(width, height, imageType, cm);
}
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
Graphics2D g2d = (Graphics2D) g;
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
try
{
g2d.setClip(0,0,2100,3300);
g2d.drawImage(this, 225, 0,null);
g2d.drawImage(this,225, 1550,null);
}
catch(Exception exc)
{
}
/* tell the caller that this page is part of the printed document */
//return NO_SUCH_PAGE;
return PAGE_EXISTS;
}
public BufferedImage resizeOurImageFromImage(BufferedImage OurImage, int targetWidth, int targetHeight)
throws Exception
{
double tempWidth, tempHeight;
int w,h,type;
Boolean OurImageHasAlpha;
OurImageHasAlpha = OurImage.getColorModel().hasAlpha();
if(OurImageHasAlpha)
{
type = BufferedImage.TYPE_INT_ARGB;
}
else
{
type = BufferedImage.TYPE_INT_RGB;
}
w = OurImage.getWidth();
h = OurImage.getHeight();
if((targetWidth == 0) && (targetHeight != 0))
{
targetWidth = (targetHeight * w)/h;
}
else
{
if((targetHeight == 0) && (targetWidth != 0))
{
targetHeight = (targetWidth * h)/w;
}
}
if((targetHeight == 0) || (targetWidth == 0))
{
throw(new Exception("In the Resize Image module with one dimension still zero after trying proportion"));
}
do
{
if(w > targetWidth)
{
tempWidth = ((double) w)/1.2;
if (tempWidth < (double) targetWidth)
{
w = targetWidth;
}
else
{
w = (int) java.lang.Math.round(tempWidth + 0.49);
}
}
else
{
w = targetWidth;
}
if(h > targetHeight)
{
tempHeight = ((double) h)/1.2;
if (tempHeight < (double) targetHeight)
{
h = targetHeight;
}
else
{
h = (int) java.lang.Math.round(tempHeight + 0.49);
}
}
else
{
h = targetHeight;
}
BufferedImage tmp = new BufferedImage(w, h, type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.drawImage(OurImage, 0, 0, w, h, null);
g2.dispose();
OurImage = tmp;
} while ((targetHeight != h) || (targetWidth != w));
return OurImage;
}
}
- 1. 打印bufferedimage到打印機
- 2. 如何使用Java打印BufferedImage?
- 3. 在JPanel上打印BufferedImage
- 4. 使用Java打印BufferedImage時的空白頁面
- 5. 打印特定尺寸的BufferedImage對象
- 6. 打印bufferedImage的邊距爲3英寸
- 7. 打印一個連續的BufferedImage
- 8. 在Java中打印BufferedImage的正確方法
- 9. 用Java打印
- 10. 用java打印PDF
- 11. Java BufferedImage padding
- 12. BufferedImage字節Java
- 13. Java - 打印類
- 14. 在Java中使用BufferedImage
- 15. 用java打印multipage tiff
- 16. 打印文本使用Java
- 17. Java:使用Apache POI打印?
- 18. java用括號打印BST
- 19. 用字節打印[] Java
- 20. Java的swing打印()使用
- 21. 用Java打印數據
- 22. 用java在linux中打印
- 23. 使用java打印數組
- 24. 無法打印用java
- 25. 在Java中打印雙面打印
- 26. 打印的熱敏打印機的Java
- 27. java打印api - 在300dpi打印JComponent
- 28. Java - 打印到收據打印機
- 29. 如何使用打印機API在JAVA中打印用於打印的文本?
- 30. Java ImageWriter BufferedImage to GIF
打印出來,其中,帆布或其他somwhere? – 2011-03-12 16:46:30
首先我試圖從jpanel打印所有組件,但是當我打印它時,頁面上只有一個按鈕。然後我發現如何製作組件的圖片,所以我認爲打印圖像更容易。但我還沒有找到關於在互聯網上打印bufferedimage的任何內容 – 2011-03-12 16:49:59
@Chuck - 或者在紙上打印? – Ishtar 2011-03-12 16:51:17