问题描述
我有一个BufferedImage图像大小(100mb)像素6720x9239,需要许多像素为60x60的小图像
I had a BufferedImage an image of size (100mb) pixels 6720x9239 and needed many small images with pixels 60x60
首先我使用了网上找到的这个代码
firstly i used this code i found on the net
BufferedImage bi= ImageIO.read(new File(filename));
......
//in paint()
Image b=createImage(new FilteredImageSource(bi.getSource(),
new CropImageFilter(x, y , 60, 60)));
需要等待大约1到5秒的每个小图片非常慢,因为我的应用需要像50这意味着ppl将需要w8从50到5 * 50秒才能重新加载面板,所以我将其转换为
needed to wait around 1 to 5 secs for each small image very slow because i my app needed like 50 images which would mean ppl would have to w8 from 50 to 5*50 sec for panel to reload, so i chan that to
BufferedImage bi= ImageIO.read(new File(filename));
......
//in paint()
BufferedImage a = imageMap.getSubimage(x, y , 60, 60);
Image b = createImage(a.getSource());
感到非常高兴现在不得不让全世界知道这个
feel really happy now had to let the world know this
推荐答案
哦,天哪,你解决了我困扰我5天的问题。我刚刚完成输入问题,即将提交。显然(现在我知道使用图像有效)当你在g2d.drawImage(Image,at,this)中使用bufferedImage时,绘图比使用Image时慢很多。像50倍慢的东西。通过将BufferedImages转换为图像(我不知道你可以做到这一点,减少可以解决问题),如下所示:
loadBullets函数内部:
Oh my god you solved my problem which had stumped me for like 5 days. I had just finished typing out the question and was about to submit it. APPARENTLY (now that I know using Images works) when you use a bufferedImage in g2d.drawImage(Image, at, this) the drawing is MUCH slower than if you use an Image. Something like 50x slower. By converting the BufferedImages to Images (I didn't know you could do that, less that would solve the problem) like this:Inside the loadBullets function:
BufferedImage a;
Image b;
a = spriteSheetPositive.getSubimage(
//going to use the same image 252 times until I get the motherload of data string converted to the format:
//sprites[shotId]=spriteSheetPositive.getSubimage(x, y, width, height);
520, //x 520,1,536,16 (small lime star) id=100
1, //y
16, //width
15 //height
);
b= createImage(a.getSource());
sprites[shotID]=b;
我现在可以使用spritesheet中的图像作为弹丸精灵,屏幕上有多达1,000个没有滞后! Hooray!
I can now use images from a spritesheet as projectile sprites with as many as 1,000 on screen at time with no lag! Hooray!
这是paint函数中的代码:
This is code within the paint function:
for (int i = 0; i < Projectiles.size(); i++) {
Shot02 m = (Shot02) Projectiles.get(i);
//m.getImage();
// g2d.drawImage(m.getImage(), m.getIntX(), m.getIntY(), this);
AffineTransform at = new AffineTransform();
// 4. translate it to the center of the component
at.translate(m.getDrawX(), m.getDrawY());
// 3. do the actual rotation
at.rotate(m.getAngle()); //rotation is Clockwise
g2d.drawImage(m.getImage(), at, this);
}
我正在拍摄平台透视镜头游戏。我从使用简单的imageicon图像切换到从sprite表创建为subImage的bufferedImage。然而,由于该程序滞后于屏幕上只有20个射弹,而之前我可能有大约1000个射程。
I am working on a platformer perspective shooting game. I switched from using a simple imageicon image to a bufferedImage created as a subImage from a sprite sheet. However, as a result of that the program lags with as little as 20 projectiles on screen, whereas previously I could have up to around 1000.
private void loadBullets() {//is done only once, when the window starts
// Get Image
ImageIcon icon = new ImageIcon(this.getClass().getResource("AbsoluteWin\\CustomShotsPositive.png"));
Image image = icon.getImage();
// Create empty BufferedImage, sized to Image
BufferedImage spriteSheetPositive =
new BufferedImage(
image.getWidth(null),
image.getHeight(null),
Transparency.BITMASK);
// Draw Image into BufferedImage
Graphics g = spriteSheetPositive.createGraphics();
g.drawImage(image, 0, 0, null);
int shotID = 1;
System.out.println(shotID);
while (shotID <= length) {//fills the array with the bullets from the sprite sheet spriteSheetPositive
sprites[shotID] = spriteSheetPositive.getSubimage(
//going to use the same image 252 times until I get the coordinates for all the other sub-images
//sprites[shotId]=spriteSheetPositive.getSubimage(x, y, width, height);
520, //x 520,1,536,16 (small lime star) id=100
1, //y
16, //width
15 //height
);
shotID += 1;
}
System.out.println(shotID);
}
Shot02类:
ImageIcon ii =
new ImageIcon(this.getClass().getResource("missile.png"));
image = ii.getImage();
//image=Destination.sprites[100];//is the source
这是Shot02类中的代码,用于控制项目符号使用的图像。同样,如果我取消注释第二个选项并使用BufferedImages,程序会像疯了一样慢下来。
This is the code in the Shot02 Class that controls the what image the bullets use. Again, if I uncomment the second option and use the BufferedImages, the program slows down like crazy.
这篇关于如何快速从大型Bufferedimage获取小图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!