我有一个简单的Java应用程序,它接收不同大小的图像并将其输出为PDF。它使用类com.lowagie.text.Image
(与iText< = 2.1.7一起使用的类),在导入图像时,像素尺寸和dpi是已知的。
如果图像不违反8.5“x11”纸的限制,我想显示图像。如果它太宽,我需要它来确定宽度减少百分比,以计算在计算PDF上图像的最终宽度和高度时高度的变化。如果图像太高,如果按高度约束则相反。最后,如果它太宽而且太高,我需要确定哪个是更大的百分比减少,并减少该值的高度和宽度。
我不想升级8.5x11限制范围内的图像(例如8.5x11 - > 11x14),而只是缩小尺寸太大的图像。
以下是我的已知值:
Original Width
Original Height
DPI
以下是我尝试过的许多尝试,但都没有奏效。我很感激任何帮助!
PlainW
是图片的原始宽度,而PlainH
是原始高度。
if (PlainW >= 612) { //x is over or equal
xDiff = PlainW - 612;
xScale = PlainW / 612;
if (PlainH < 792) { //y is under
yDiff = 792 - PlainH;
yScale = PlainH / 792;
} else if (PlainH == 792) { //y is equal
yDiff = 792 - PlainH;
yScale = PlainH / 792;
} else if (PlainH > 792) { //y is over
yDiff = PlainH - 792;
yScale = PlainH / 792;
}
} else if (PlainW < 612) { //x is under
xDiff = 612 - PlainW;
xScale = PlainW / 162;
if (PlainH < 792) { //y is under
yDiff = 792 - PlainH;
yScale = PlainH / 792;
} else if (PlainH == 792) { //y is equal
yDiff = 792 - PlainH;
yScale = PlainH / 792;
} else if (PlainH > 792) { //y is over
yDiff = PlainH - 792;
yScale = PlainH / 792;
}
}
答案 0 :(得分:2)
您的代码真的很难理解!我将告诉你如何使它变得更简单。
这是你的代码:
if (PlainW >= 612) { //x is over or equal
xDiff = PlainW - 612;
xScale = PlainW / 612;
if (PlainH < 792) { //y is under
yDiff = 792 - PlainH;
yScale = PlainH / 792;
} else if (PlainH == 792) { //y is equal
yDiff = 792 - PlainH;
yScale = PlainH / 792;
} else if (PlainH > 792) { //y is over
yDiff = PlainH - 792;
yScale = PlainH / 792;
}
} else if (PlainW < 612) { //x is under
xDiff = 612 - PlainW;
xScale = PlainW / 162;
if (PlainH < 792) { //y is under
yDiff = 792 - PlainH;
yScale = PlainH / 792;
} else if (PlainH == 792) { //y is equal
yDiff = 792 - PlainH;
yScale = PlainH / 792;
} else if (PlainH > 792) { //y is over
yDiff = PlainH - 792;
yScale = PlainH / 792;
}
}
在每个条件中,你有yScale = PlainH / 792;
,只需把它放在外面,就像这样(另外我摆脱了无用的评论):
if (PlainW >= 612) {
xDiff = PlainW - 612;
xScale = PlainW / 612;
if (PlainH < 792) {
yDiff = 792 - PlainH;
} else if (PlainH == 792) {
yDiff = 792 - PlainH;
} else if (PlainH > 792) {
yDiff = PlainH - 792;
}
} else if (PlainW < 612) {
xDiff = 612 - PlainW;
xScale = PlainW / 162;
if (PlainH < 792) {
yDiff = 792 - PlainH;
} else if (PlainH == 792) {
yDiff = 792 - PlainH;
} else if (PlainH > 792) {
yDiff = PlainH - 792;
}
}
yScale = PlainH / 792;
然后你有一些完全相同的条件yDiff = 792 - PlainH;
,只需将它们组合起来:
if (PlainW >= 612) {
xDiff = PlainW - 612;
xScale = PlainW / 612;
if (PlainH <= 792) {
yDiff = 792 - PlainH;
} else if (PlainH > 792) {
yDiff = PlainH - 792;
}
} else if (PlainW < 612) {
xDiff = 612 - PlainW;
xScale = PlainW / 162;
if (PlainH <= 792) {
yDiff = 792 - PlainH;
} else if (PlainH > 792) {
yDiff = PlainH - 792;
}
}
yScale = PlainH / 792;
但是等等!比较两个整数然后从最大值中减去最小值?为什么不采取差异的绝对值?
if (PlainW >= 612) {
xDiff = PlainW - 612;
xScale = PlainW / 612;
yDiff = Math.abs(PlainH - 792);
} else if (PlainW < 612) {
xDiff = 612 - PlainW;
xScale = PlainW / 162;
yDiff = Math.abs(PlainH - 792);
}
yScale = PlainH / 792;
同样,我在条件中看到相同的代码。把它移到外面!
if (PlainW >= 612) {
xDiff = PlainW - 612;
} else if (PlainW < 612) {
xDiff = 612 - PlainW;
}
xScale = PlainW / 612;
yDiff = Math.abs(PlainH - 792);
yScale = PlainH / 792;
同样,比较2个整数并从最大值中减去最小值可以用Math.abs
替换。
xDiff = Math.abs(PlainW - 612);
xScale = PlainW / 612;
yDiff = Math.abs(PlainH - 792);
yScale = PlainH / 792;
看!您的代码从27行减少到只有4行,它包含 ZERO 条件,并且更容易理解!