https://bz.apache.org/bugzilla/show_bug.cgi?id=64213
Bug ID: 64213
Summary: Picture.resize(double scale) scales width wrong for
small pictures and when dx1 is set.
Product: POI
Version: unspecified
Hardware: PC
OS: All
Status: NEW
Severity: normal
Priority: P2
Component: SS Common
Assignee: [email protected]
Reporter: [email protected]
Target Milestone: ---
Created attachment 37088
--> https://bz.apache.org/bugzilla/attachment.cgi?id=37088&action=edit
Images used
For small pictures and if there is dx1 set in anchor, Picture.resize(double
scale) scales width wrong.
Example to reproduce:
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
class PictureResizeBug {
public static void main(String[] args) throws Exception {
//Workbook wb = new HSSFWorkbook(); String resultName =
"PictureResizeBug.xls";
Workbook wb = new XSSFWorkbook(); String resultName =
"PictureResizeBug.xlsx";
Sheet sheet = wb.createSheet("Sheet1");
String picturePath = "./candle200x200.png";
//String picturePath = "./candle400x400.png";
int pictureType = Workbook.PICTURE_TYPE_PNG;
int col1 = 2;
int dx1 = 100;
int row1 = 2;
float scale = 0.5f;
sheet.setColumnWidth(col1, 50 * 256);
//load the picture
InputStream inputStream = new FileInputStream(picturePath);
byte[] bytes = IOUtils.toByteArray(inputStream);
int pictureIdx = wb.addPicture(bytes, pictureType);
inputStream.close();
//create an anchor with upper left cell column/startRow, only one cell anchor
since bottom right depends on resizing
CreationHelper helper = wb.getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(col1);
if (wb instanceof XSSFWorkbook) {
anchor.setDx1(dx1 * Units.EMU_PER_PIXEL);
} else if (wb instanceof HSSFWorkbook) {
anchor.setDx1(dx1);
}
anchor.setRow1(row1);
//create a picture anchored to Col1 and Row1
Drawing drawing = sheet.createDrawingPatriarch();
Picture pict = drawing.createPicture(anchor, pictureIdx);
//resize the picture to it's native size
pict.resize();
//resize the picture scaled proportional
pict.resize(scale);
FileOutputStream fileOut = new FileOutputStream(resultName);
wb.write(fileOut);
fileOut.close();
wb.close();
}
}
Here for candle200x200.png, a 200px + 200px PNG image, the result of this code
is a picture in Excel which is correctla scaled 0.25 in heigth but 0.90 in
width for HSSF and 1.37 in width for XSSF. If candle400x400.png is used, the
scaliing works properly in height and width.
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]