java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/yujianping_123/article/details/100163320

pdf (多页)生成jpg的时候,报错

java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!

代码片段:

	        for (int i = 0; i < picNum; i++) {
				buffer = piclist.get(i);
				heightArray[i] = _height = buffer.getHeight();// 图片高度
				if (i == 0) {
					width = buffer.getWidth();// 图片宽度
				}
				height += _height; // 获取总高度
				_imgRGB = new int[width * _height];// 从图片中读取RGB
				_imgRGB = buffer.getRGB(0, 0, width, _height, _imgRGB, 0, width);
				imgRGB.add(_imgRGB);
			}
			_height = 0; // 设置偏移高度为0
			// 生成新图片
			BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

			for (int i = 0; i < picNum; i++) {
				__height = heightArray[i];
				if (i != 0)
					_height += __height; // 计算偏移高度
				imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width); // 写入流中
			}

这行代码报错: imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width);

查看doc,仔细看第2个参数startY(_height),是起始页高

所以发现了是代码bug:

    __height = heightArray[i];
                if (i != 0)
                    _height += __height;   -》  _height += heightArray[i-1]; // 上页高度总和

猜你喜欢

转载自blog.csdn.net/yujianping_123/article/details/100163320
今日推荐