代码中生成的图片的信息与读取到的图片的信息一致,比例未失真,但用office打开Excel时,图片宽高比与代码设置不一致,显示失真。(版本 aspose.cells for java 16.12.0)
原图:
width:576 height:1024 widthCM:15.239999771118164 heightCM:27.093332926432293 widthPt:432.0 heightPt:768.0
生成的图片:
width:85 height:152 widthCM:2.248958299557368 heightCM:4.021666606267293 widthPt:63.75 heightPt:114.0
导入的图片:
width:85 height:152 widthCM:2.248958299557368 heightCM:4.021666606267293 widthPt:63.75 heightPt:114.0
缩放比(导入或生成的图片/原图):
width:0.14757 height:0.14843 widthCM:0.14756 heightCM:0.14844 widthPt:0.14757 heightPt:0.14844
以上比率在计算过程中,均只保留小数点后五位,长宽比差值均控制在百分之一以内,从长宽计算处理角度来看,不存在失真情况,但是在用office打开Excel,图片的显示与预期结果不一致。
image.png (59.6 KB)
原图、excel文件和代码:
上传不了zip文件,有问题或结论请尽快联系 daichao1@huawei.com
代码:
package com.huawei.it.isd.esurvey.tssr.aspose.picture;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import com.aspose.cells.PictureCollection;
import com.aspose.cells.Shape;
import com.aspose.cells.Worksheet;
import com.huawei.it.jalor5.core.log.ILogger;
import com.huawei.it.jalor5.core.log.JalorLoggerFactory;
/**
* Image drawer who draw images into excel
* <p>
* @author dwx356071
* @version V200R002, 2017-1-11
* @see [相关类/方法]
* @since UniSTAR Designer V200R002
*/
public class ImageDraw
{
/**
* logger
*/
private static final ILogger log = JalorLoggerFactory.getLogger(ImageDraw.class);
/**
* image area
*/
private ImageArea imageArea;
/**
* image path
*/
private String filePath;
/**
* which sheet is the images in
*/
private Worksheet sheet;
/**
* <默认构造函数>
*/
public ImageDraw(ImageArea imageArea, String filePath, Worksheet sheet)
{
super();
this.imageArea = imageArea;
this.filePath = filePath;
this.sheet = sheet;
}
/**
* draw images into a sheet
* <p>
* @author dwx356071 2017-1-11
* @see [类、类#方法、类#成员]
*/
public void draw()
{
ByteArrayOutputStream byteArrayOut = null;
try
{
byteArrayOut = new ByteArrayOutputStream();
// the name of picture
String picName = imageArea.getPicName();
PictureCollection shapec = sheet.getPictures();
int index = shapec.add(0, 0, filePath);
Shape shape = shapec.get(index);
int width = shape.getWidth();
int height = shape.getHeight();
shape.setAlternativeText(picName);
double ptRate = shape.getWidthPt() / shape.getWidth();
// the width of excel's area which will be inserted into a image
int shape_x = imageArea.getWidth();
// the height of excel's area which will be inserted into a image
int shape_y = imageArea.getHeight();
BigDecimal r1 = new BigDecimal(shape_x).divide(new BigDecimal(width), 3, BigDecimal.ROUND_DOWN);
BigDecimal r2 = new BigDecimal(shape_y).divide(new BigDecimal(height), 3, BigDecimal.ROUND_DOWN);
if (r1.compareTo(r2) == 1)
{
shape.setWidthPt(r2.multiply(new BigDecimal(width)).doubleValue() * ptRate);
shape.setHeightPt(r2.multiply(new BigDecimal(height)).doubleValue() * ptRate);
}
else
{
shape.setWidthPt(r1.multiply(new BigDecimal(width)).doubleValue() * ptRate);
shape.setHeightPt(r1.multiply(new BigDecimal(height)).doubleValue() * ptRate);
}
float widthRate = shape.getWidth() / (width + 0.0f);
float heightRate = shape.getHeight() / (height + 0.0f);
if (widthRate < 1.0 && heightRate < 1.0 && Math.abs(widthRate - heightRate) >= 0.02)
{
if (widthRate < heightRate)
{
if ((heightRate * width) <= shape_x)
{
String s = String.valueOf(heightRate * width);
shape.setWidth(Integer.valueOf(s.substring(0, s.indexOf('.'))));
}
}
else
{
if ((widthRate * height) <= shape_y)
{
String s = String.valueOf(widthRate * height);
shape.setHeight(Integer.valueOf(s.substring(0, s.indexOf('.'))));
}
}
}
// the practical width of the image
int shape_x_a = shape.getWidth();
// the practical height of the image
int shape_y_a = shape.getHeight();
shape.setX(imageArea.getX() + (shape_x - shape_x_a) / 2);
shape.setY(imageArea.getY() + (shape_y - shape_y_a) / 2);
log.info("ImageDraw draw method info -->" + sheet.getName() + ",picName:" + picName + "width" + width
+ "height" + height + "shapewidth" + shape.getWidth() + "shapeheight" + shape.getHeight()
+ "shape_x:" + shape_x + ",shape_y:" + shape_y + 'X' + imageArea.getX() + 'Y' + imageArea.getY());
log.debug("shape_x:" + shape_x + ",shape_y:" + shape_y);
log.debug("Width:" + shape.getWidth() + ",Height:" + shape.getHeight());
log.debug("X1:" + imageArea.getX() + ",Y1" + imageArea.getY());
log.debug("X2:" + shape.getX() + ",Y2" + shape.getY());
}
catch (IOException e)
{
log.error("ExportTssrReportFile.insertPictureToCellAspose" + e.getMessage());
}
catch (Exception e)
{
log.error("ExportTssrReportFile.insertPictureToCellAspose" + e.getMessage());
}
finally
{
if (null != byteArrayOut)
{
try
{
byteArrayOut.close();
}
catch (IOException e)
{
log.error("ExportTssrReportFile.insertPictureToCellAspose" + e.getMessage());
}
}
}
}
/**
* get image area
* @author dwx356071 2017-1-11
* @return
* @see [类、类#方法、类#成员]
*/
public ImageArea getImageArea()
{
return imageArea;
}
/**
* set image area
* @author dwx356071 2017-1-11
* @return
* @see [类、类#方法、类#成员]
*/
public void setImageArea(ImageArea imageArea)
{
this.imageArea = imageArea;
}
/**
* get file path
* @author dwx356071 2017-1-11
* @return
* @see [类、类#方法、类#成员]
*/
public String getFilePath()
{
return filePath;
}
/**
* set file path
* @author dwx356071 2017-1-11
* @return
* @see [类、类#方法、类#成员]
*/
public void setFilePath(String filePath)
{
this.filePath = filePath;
}
/**
* get sheet
* @author dwx356071 2017-1-11
* @return
* @see [类、类#方法、类#成员]
*/
public Worksheet getSheet()
{
return sheet;
}
/**
* set sheet
* @author dwx356071 2017-1-11
* @return
* @see [类、类#方法、类#成员]
*/
public void setSheet(Worksheet sheet)
{
this.sheet = sheet;
}
}
@daichao1225,
感谢您的详细信息和截图。
请尝试我们的最新版本/修复:Aspose.Cells for Java v18.4.5
如果您仍然使用v18.4.5发现问题,请提供一个简单的Java程序(以便我们可以正确执行它,而且我们可能不会编译您粘贴的代码片段)。 另外,压缩文件(使用一些压缩工具,例如WinRar工具)并将其附加到此处。 这将有助于我们正确评估您的问题并尽快解决问题。
@daichao1225
我们执行你的代码。 这有点复杂,所以你应该为我们提供更简单的代码。 您没有提供给我们20180417100641481.jpg,所以我们使用了一个假的JPG文件。 请参阅以下附件。 它包含输出Excel文件和假jpg文件。
Download Link:
Output Excel File and Input Image.zip (158.7 KB)
请提供我们的20180417100641481.jpg文件和您的实际Excel文件以及您期望的Excel文件,以便我们可以进一步研究此问题。
等比例压缩问题.jpg (60.2 KB)
正如你回复的里的excel,查看里面的图片属性。发现图片不是等比例压缩的。但是我们代码是按代码等比例压缩设置宽高的,但是实际效果是:用office打开后不是等比例。这就是我们的问题。
@daichao1225
很难理解你的问题。 请提供带注释的截图。 请提供您的预期Excel文件。 只需在Microsoft Excel中打开pictureAnamorphose.xlsx,然后使用Microsoft Excel手动纠正并提供给我们。 希望它能帮助我们更好地理解你的问题。 此外,如果您可以提供简化的代码,只需几行代码即可复制您的问题,这对我们有很大帮助。
Post Reference: #7
@daichao1225
我们能够观察到图像尺度的扭曲。 但是,我们需要检查它是由于错误还是因为代码中的某些问题。
此问题已记录为
-
CELLSJAVA-42626 - Scale of Picture Distorts in the Output Excel File
@daichao1225,
我们进一步评估你的问题。
缓存行的高度在文件中不正确。 阅读后,请自动适应线条高度。 请参阅以下示例代码:
例如
示例代码:
LoadOptions options = new LoadOptions();
options.setAutoFitterOptions( new AutoFitterOptions());
options.getAutoFitterOptions().setOnlyAuto(true);
Workbook wb = new Workbook("D:\\Filetemp\\photoAnamorphose-2.xlsx", options);
顺便说一句,如果它们是自动的,不同区域中线条的高度将会不同,因为默认字体将在不同区域/区域中被改变。
我们按照你的建议,修改了代码,运行后的结果还是一样。code.jpg (78.4 KB)
excel结果截图.jpg (46.9 KB)
@daichao1225,
感谢您的截图。
我们很抱歉,建议的代码没有帮助。 我记录了你的问题的截图。 我们会进一步评估你的问题。
一旦我们有任何新的信息共享,我们会通知您
大概什么时间能解决好,这个问题已经影响了用户的使用,用户很关注这个问题,感谢。
另外,我们已经购买了证书了。还有其他高效的技术支持的方式吗,这种发帖的方式时效太差了。
@daichao1225,
我们正在调查您的问题并尽快更新。
如果您希望您的问题被视为一个priroty问题,您可以考虑检查/购买支付支持选项:
@daichao1225,
对不起,我们不通过电话提供技术支持。 请稍候,我们会尽快在线更新您(在此线程中)。