只是想貢獻這個古老的問題。我得到這個工作通過添加下列行BuildConfig.groovy
:
在plugins
部分:
plugins {
// other plugins here
// ...
compile ":barcode4j:0.3"
compile ":rendering:1.0.0"
}
在dependencies
部分:
dependencies {
// other dependencies here
// ...
compile 'avalon-framework:avalon-framework:4.1.5'
}
然後我說的控制器基於代碼的例子,我在網上找到。我特別需要一個DataMatrix生成器,但添加其他應該很容易,只需向控制器添加方法即可。很抱歉的品質不好的代碼(我是一個Groovy新手):
package myproject
import org.krysalis.barcode4j.impl.datamatrix.DataMatrix
import java.awt.Dimension
class BarcodeController {
// a valid PNG image, base64 encoded
static invalidBarcodeImgBase64 = """iVBORw0KGgoAA...=="""
// Needs index.gsp for testing
def index() {
['uuid': UUID.randomUUID(), 'fecha': new Date()]
}
def dataMatrix(String value) {
if ((null == value) || (value.length() < 1) || (value.length() > 2000)) {
def img = invalidBarcodeImgBase64.decodeBase64()
response.setHeader('Content-length', new Integer(img.length).toString())
response.contentType = 'image/png'
response.outputStream << img
response.outputStream.flush()
} else {
def generator = new DataMatrix()
generator.dataMatrixBean.setMinSize(new Dimension(16, 16))
renderBarcodePng(generator, value, [antiAlias: false])
}
}
def datamatrix(String value) {
dataMatrix(value)
}
}
最後這裏的index.gsp
在測試條形碼觀點:
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>DataMatrix</title>
</head>
<body>
<g:img dir="barcode" file="dataMatrix?value=${uuid}"/>
<br />
<br />
<g:img dir="barcode" file="dataMatrix?value=${fecha}"/>
<br />
<br />
<g:img dir="barcode" file="dataMatrix?value=Nothing to see here"/>
</body>
</html>
不能使用Base64編碼也? – 2011-01-20 21:06:08
Ach,沒關係,只注意到IE不支持它? – Igor
我對grails並不熟悉,但是在PHP中這樣做的方法是將src屬性設置爲腳本。所以,''並讓該PHP腳本輸出原始圖像數據。你能做那樣的事嗎?不要忘記適當地設置您的內容類型標題。 – Brad