Java Applet - 图像导出性能问题
我有一个 Java Applet,我正在对其进行一些编辑,但遇到了性能问题。 更具体地说,该小程序生成一个图像,我需要将其导出到客户端的计算机。
这确实处于概念验证阶段,所以请耐心等待。 目前,图像将导出到客户端计算机上的预定义位置(将来将被保存对话框或其他内容替换)。 然而,对于 32kb 的文件,该过程需要近 15 秒。
我做了一些“按部就班”的分析,在整个相关方法中以逻辑间隔将消息打印到控制台。 令我惊讶的是,我发现瓶颈似乎在于实际的数据流写入过程,而不是 jpeg 编码。
请记住,我只有 JAVA 及其方法的基本知识
所以慢慢来:p - 我主要是在寻找解决问题的建议,而不是解决方案本身。
下面是神奇发生的代码块:
ByteArrayOutputStream jpegOutput = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(jpegOutput);
encoder.encode(biFullView);
byte[] imageData = jpegOutput.toByteArray();
String myFile="C:" + File.separator + "tmpfile.jpg";
File f = new File(myFile);
try {
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myFile),512));
dos.writeBytes(byteToString(imageData));
dos.flush();
dos.close();
}
catch (SecurityException ee) {
System.out.println("writeFile: caught security exception");
}
catch (IOException ioe) {
System.out.println("writeFile: caught i/o exception");
}
就像我提到的,使用 system.out.println() 我已经将性能瓶颈缩小到了 DataOutputStream 块。 使用具有不同硬件统计数据的各种机器似乎对整体性能影响不大。
任何指示/建议/方向将不胜感激。
编辑: 根据要求,byteToString():
public String byteToString(byte[] data){
String text = new String();
for ( int i = 0; i < data.length; i++ ){
text += (char) ( data[i] & 0x00FF );
}
return text;
}
I have a Java Applet that I'm making some edits to and am running into performance issues. More specifically, the applet generates an image which I need to export to the client's machine.
This is really at the proof-of-concept stage so bear with me. For right now, the image is exported to the clients machine at a pre-defined location (This will be replaced with a save-dialog or something in the future). However, the process takes nearly 15 seconds for a 32kb file.
I've done some 'shoot-by-the-hip' profiling where I have printed messages to the console at logical intervals throughout the method in question. I've found, to my surprise, that the bottleneck appears to be with the actual data stream writing process, not the jpeg encoding.
KEEP IN MIND THAT I ONLY HAVE A BASIC KNOWLEDGE OF JAVA AND ITS METHODS
So go slow :p - I'm mainly looking for suggestions to solve the problem rather the solution itself.
Here is the block of code where the magic happens:
ByteArrayOutputStream jpegOutput = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(jpegOutput);
encoder.encode(biFullView);
byte[] imageData = jpegOutput.toByteArray();
String myFile="C:" + File.separator + "tmpfile.jpg";
File f = new File(myFile);
try {
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myFile),512));
dos.writeBytes(byteToString(imageData));
dos.flush();
dos.close();
}
catch (SecurityException ee) {
System.out.println("writeFile: caught security exception");
}
catch (IOException ioe) {
System.out.println("writeFile: caught i/o exception");
}
Like I mentioned, using system.out.println() I've narrowed the performance bottleneck to the DataOutputStream block. Using a variety of machines with varying hardware stats seems to have little effect on the overall performance.
Any pointers/suggestions/direction would be much appreciated.
EDIT:
As requested, byteToString():
public String byteToString(byte[] data){
String text = new String();
for ( int i = 0; i < data.length; i++ ){
text += (char) ( data[i] & 0x00FF );
}
return text;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想看看
ImageIO
。我认为性能问题的原因是 byteToString 中的循环。 您永远不想在循环中进行串联。 您可以改用
String(byte[])
构造函数,但实际上您并不需要将字节转换为字符串。You might want to take a look at
ImageIO
.And I think the reason for the performance problem is the looping in
byteToString
. You never want to do a concatenation in a loop. You could use theString(byte[])
constructor instead, but you don't really need to be turning the bytes into a string anyway.如果不需要图像数据字节数组,可以直接编码到文件中:
如果需要字节数组来执行其他操作,最好将其直接写入 FileOutputStream:
If you don't need the image data byte array you can encode directly to the file:
If you need the byte array to perform other operations it's better to write it directly to the FileOutputStream:
您还可以使用标准 ImageIO API (com.sun.image.codec.jpeg 包中的类不是核心 Java API 的一部分)。
You could also use the standard ImageIO API (classes in the com.sun.image.codec.jpeg package are not part of the core Java APIs).