1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
public List<BufferedImage> mergeImageFiles(List<File> files, int width, int height, int row, int column, int pageCount) throws IOException { if (width <= 0 || height <= 0) { throw new IllegalArgumentException("wight/height is zero or negative"); } if (column <= 0 || row <= 0) { throw new IllegalArgumentException("column/row is zero or negative"); } if (files == null) { throw new IllegalArgumentException("image files is null or empty"); } if (files.isEmpty()) { return Lists.newArrayList(new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)); }
List<BufferedImage> imageItemList = new ArrayList<>(); for (File file : files) { BufferedImage image = ImageIO.read(file); imageItemList.add(image); }
return mergeImages(imageItemList, width, height, row, column, pageCount); }
public List<BufferedImage> mergeImages(List<BufferedImage> images, int width, int height, int row, int column, int pageCount) throws IOException { if (width <= 0 || height <= 0) { throw new IllegalArgumentException("wight/height is zero or negative"); } if (column <= 0 || row <= 0) { throw new IllegalArgumentException("column/row is zero or negative"); } if (images == null) { throw new IllegalArgumentException("image files is null or empty"); } if (images.isEmpty()) { return Lists.newArrayList(new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)); }
int pageImageCount = column * row; int fullPageCount = (int) Math.ceil(images.size() / (double) pageImageCount); if (pageCount <= 0 || pageCount > fullPageCount) { pageCount = fullPageCount; } List<BufferedImage> pageList = new ArrayList<>(pageCount);
int partWidth = width / column; int partHeight = height / row;
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) { BufferedImage resultImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = resultImage.createGraphics();
for (int rowIndex = 0; rowIndex < row; rowIndex++) { for (int columnIndex = 0; columnIndex < column; columnIndex++) { int fileIndex = (pageIndex * pageImageCount) + (rowIndex * column) + columnIndex; if (fileIndex >= images.size()) break;
BufferedImage partImage = images.get(fileIndex);
int currentPartWidth = partWidth; int currentPartHeight = partHeight; int currentDeltaX = 0; int currentDeltaY = 0; double widthScale = partImage.getWidth() / (double) partWidth; double heightScale = partImage.getHeight() / (double) partHeight; if (widthScale > heightScale) { currentPartHeight = (int) (partImage.getHeight() / widthScale); currentDeltaY = (partHeight - currentPartHeight) / 2; } else { currentPartWidth = (int) (partImage.getWidth() / heightScale); currentDeltaX = (partWidth - currentPartWidth) / 2; }
int currentX = columnIndex * partWidth + currentDeltaX; int currentY = rowIndex * partHeight + currentDeltaY; graphics.drawImage(partImage, currentX, currentY, currentX + currentPartWidth, currentY + currentPartHeight, 0, 0, partImage.getWidth(), partImage.getHeight(), null); } }
pageList.add(resultImage); }
return pageList; }
|