Java 如何上传BuffereImage而不将其存储在user';s系统?
我是java的初学者,我正在尝试编写一个简单的屏幕捕获程序。我编写了一个简单的Java 如何上传BuffereImage而不将其存储在user';s系统?,java,php,swing,Java,Php,Swing,我是java的初学者,我正在尝试编写一个简单的屏幕捕获程序。我编写了一个简单的SWING桌面应用程序,其中包含一个按钮和一个文本字段,我试图做的是,当用户单击该按钮时,该应用程序使用awt.Robot拍摄屏幕快照,并将该图像和文本发送到我服务器上的PHP脚本 到目前为止,我的快照功能是: private void takeSnapShot(){ try { Robot robot = new Robot(); Rectangle a
SWING
桌面应用程序,其中包含一个按钮和一个文本字段,我试图做的是,当用户单击该按钮时,该应用程序使用awt.Robot
拍摄屏幕快照,并将该图像和文本发送到我服务器上的PHP脚本
到目前为止,我的快照功能是:
private void takeSnapShot(){
try {
Robot robot = new Robot();
Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage bufferedImage = robot.createScreenCapture(area);
//Try to save the captured image
try {
File file = new File("screenshot_full.png");
ImageIO.write(bufferedImage, "png", file);
} catch (IOException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (AWTException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
}
}
正如您所看到的,到目前为止它相当简单,但是我不确定如何将该图像发送到我的PHP脚本,而不实际将该图像存储在用户的PC上
哦,我正在使用
apachehttpclient
库与web服务器通信。对于文本,我想我可以将其作为get查询传递到URL中,但我不确定如何处理该图像。为什么不将其作为Web服务,让PHP使用它?您可以使用某种Base64编码器通过WebService发送二进制数据
您可以这样做以获取BuffereImage的字节:
byte[] binaryData = ((DataBufferByte) bufferedImage.getData().getDataBuffer()).getData();
要稍微修改现有的方法,也许可以使用一个临时文件,然后在完成后将其删除。也许它看起来像:
private void takeSnapShot(){
try {
Robot robot = new Robot();
Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage bufferedImage = robot.createScreenCapture(area);
//Try to save the captured image
try {
File file = File.createTempFile(Long.toString(System.currentTimeMillis()), ".png");
ImageIO.write(bufferedImage, "png", file);
//send image
file.delete();
} catch (IOException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (AWTException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
}
}
另一种选择是从buffereImage
构建一个int[][]
,它将保存图像每个像素的RGB值:
public int[][] getColors(final BufferedImage image){
assert image != null;
final int[][] colors = new int[image.getWidth()][image.getHeight()];
for(int x = 0; x < colors.length; x++)
for(int y = 0; y < colors[x].length; y++)
colors[x][x] = image.getRGB(x, y);
return colors;
}
public int[]getColors(最终缓冲区图像){
断言图像!=null;
final int[][]colors=new int[image.getWidth()][image.getHeight()];
对于(int x=0;x
我有点不确定你希望实现什么;您打算如何处理图像?
ImageIO。将
can写入您选择的输出流
因此,如果您不想将图像写入文件
,只需将其写入另一个流即可
例如
OutputStream os = null;
try {
Robot robot = new Robot();
Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage bufferedImage = robot.createScreenCapture(area);
//Try to save the captured image
try {
os = ...;
ImageIO.write(bufferedImage, "png", os);
} catch (IOException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
os.close();
} catch (Exception exp) {
}
}
} catch (AWTException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
}
当然,我不知道您将数据发送到哪里,因此您需要自己定义OutputStream
如果您有内存,您可以通过tearrayoutputstream将其写入中,然后将其写入将来需要的任何输出流中