playframework结合QRGen在页面生成QR码
程序员文章站
2022-06-05 15:26:46
...
本文来自: fair-jm.iteye.com 转截请注明出处
QRGen的使用详见:
http://www.importnew.com/15028.html
playframework的安装可以去play的官网:
https://playframework.com/
通过activator建立一个play项目
activator new qr_example
使用eclipse插件生成eclipse项目 并导入
在项目根目录下的build.sbt中增加依赖
"net.glxn.qrgen" % "javase" % "2.0"
完整的如下:
name := """qr_example""" version := "1.0-SNAPSHOT" lazy val root = (project in file(".")).enablePlugins(PlayScala) scalaVersion := "2.11.1" libraryDependencies ++= Seq( jdbc, anorm, cache, ws, "net.glxn.qrgen" % "javase" % "2.0" )
更改Application.scala的代码:
package controllers import net.glxn.qrgen.core.image.ImageType import net.glxn.qrgen.core.vcard.VCard import net.glxn.qrgen.javase.QRCode import play.api.Play.current import play.api.cache.Cached import play.api.mvc._ object Application extends Controller { def index = Action { Ok(views.html.index("Your new application is ready.")) } def qr(word: String) = Cached(word) { Action { val out = QRCode.from(word).to(ImageType.PNG).stream(); Ok(out.toByteArray()).as("image/png") } } def myInfoQr = Cached("myInfo") { Action { val vcard = new VCard("fairjm") .setEmail("fairjmflyer@gmail.com") .setWebsite("fair-jm.iteye.com"); val out = QRCode.from(vcard).to(ImageType.PNG).stream(); Ok(out.toByteArray()).as("image/png") } } }
在routes中配置路由:
# Home page GET / controllers.Application.index GET /qr/myinfo controllers.Application.myInfoQr GET /qr/:wd controllers.Application.qr(wd) # Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.at(path="/public", file)
更改index.scala.html
@(message: String) @main("Welcome to Play") { <p> example: <img src="@routes.Application.qr("http://fair-jm.iteye.com")" /> </p> <p> myinfo: <img src="@routes.Application.myInfoQr" /> </p> }
在命令行使用run 运行 最后效果如下:
这样就完成啦 很简单
本文来自: fair-jm.iteye.com 转截请注明出处
上一篇: playframework结合QRGen在页面生成QR码
下一篇: 工作感悟