2017-04-21 51 views
7

有沒有一種方法來調整xy座標以適應對蝦PDF中的邊界框,如果它們大於框的高度?蝦PDF線適合邊界框

我使用寶石「簽名墊軌」捕捉簽名然後存儲以下內容:

[{"lx":98,"ly":23,"mx":98,"my":22},{"lx":98,"ly":21,"mx":98,"my":23},{"lx":98,"ly":18,"mx":98,"my":21}, ... {"lx":405,"ly":68,"mx":403,"my":67},{"lx":406,"ly":69,"mx":405,"my":68}] 

我有如下,以顯示我的PDF簽名:

bounding_box([0, cursor], width: 540, height: 100) do 
     stroke_bounds 
     @witness_signature.each do |e| 
     stroke { line [e["lx"], 100 - e["ly"]], 
         [e["mx"], 100 - e["my"] ] } 
     end 
    end 

但是簽名在某些情況下會跑離頁面,並不是中心,只是一般地運行不佳。

+0

你能告訴你的電流輸出的一個例子(圖像)和另一個與所需的輸出? – Gerry

回答

1

你的問題很模糊,所以我猜你的意思。

重新縮放座標(x[i], y[i]), i = 1..n的序列,以適應尺寸(width, height)的給定邊界框與原籍(0,0)如後記,首先決定是否以保持原始圖像的高寬比。適合一個盒子通常不會那樣做。既然你可能不想扭曲簽名,說答案是「是的」。

當將圖像縮放爲保留長寬比的盒子時,x軸或y軸確定比例因子,除非該盒恰好具有圖像的方面。接下來要決定如何處理替代軸上的「額外空間」。例如。如果圖像比邊界框高而薄,多餘的空間將在x軸上;如果短而胖,那就是y軸。

讓我們假設將圖像置於額外的空間中;這似乎適合簽名。

那麼這裏就是僞重規模的點,以適應框:

x_min = y_min = +infty, x_max = y_max = -infty 
for i in 1 to n 
    if x[i] < x_min, x_min = x[i] 
    if x[i] > x_max, x_max = x[i] 
    if y[i] < y_min, y_min = y[i] 
    if y[i] > y_max, y_max = y[i] 
end for 
dx = x_max - x_min 
dy = y_max - y_min 
x_scale = width/dx 
y_scale = height/dy 
if x_scale < y_scale then 
    // extra space is on the y-dimension 
    scale = x_scale 
    x_org = 0 
    y_org = 0.5 * (height - dy * scale) // equal top and bottom extra space 
else 
    // extra space is on the x_dimension 
    scale = y_scale 
    x_org = 0.5 * (width - dx * scale) // equal left and right extra space 
    y_org = 0 
end 
for i in 1 to n 
    x[i] = x_org + scale * (x[i] - x_min) 
    y[i] = y_org + scale * (y[i] - y_min) 
end