Tag: bufferedimage

Java将Image转换成BufferedImage

在StackOverflow上已经有这样的问题了,接受的答案是“cast”: Image image = ImageIO.read(new File(file)); BufferedImage buffered = (BufferedImage) image; 在我的程序中,我尝试: final float FACTOR = 4f; BufferedImage img = ImageIO.read(new File(“graphic.png”)); int scaleX = (int) (img.getWidth() * FACTOR); int scaleY = (int) (img.getHeight() * FACTOR); Image image = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH); BufferedImage buffered = (BufferedImage) image; 不幸的是我得到运行时错误: sun.awt.image.ToolkitImage不能转换为java.awt.image.BufferedImage 显然铸造不起作用。 问题是:什么是(或有)正确的方式转换图像到BufferedImage?

不可接受的性能阅读透明.png像素由像素

我正在创建一个工具来检测精灵表中的精灵并将每个找到的精灵转换成一个新的BufferedImage。 这个过程是有效的,但是对于某些图像格式(主要是透明的图像)来说速度过于缓慢,比如这个: ( 肯尼的游戏资产 – 动物包 ) 我已经对我的代码进行了剖析,并确定绝大多数应用程序的时间超过99%都是由于getRGB()调用而花费在这个方法中的。 private fun findContiguousSprite(image: BufferedImage, startingPoint: Point, backgroundColor: Color): List<Point> { val unvisited = LinkedList<Point>() val visited = arrayListOf(startingPoint) unvisited.addAll(neighbors(startingPoint, image).filter { Color(image.getRGB(it.x, it.y)) != backgroundColor }) while (unvisited.isNotEmpty()) { val currentPoint = unvisited.pop() val currentColor = Color(image.getRGB(currentPoint.x, currentPoint.y)) if (currentColor != backgroundColor) { unvisited.addAll(neighbors(currentPoint, image).filter { !visited.contains(it) […]