加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

图像 – 如何使用Grails使用imgscalr

发布时间:2020-12-14 16:22:59 所属栏目:大数据 来源:网络整理
导读:我最近几天才开始使用Groovy和Grails.我之前没有任何 Java经验,所以你不得不原谅这个(可能)非常基本的问题.我搜索过谷歌和Stack Overflow,但没有找到任何可以帮助我实际安装的东西. 我有一个图像上传工作,我将文件存储在服务器上.我使用了IBM Grails教程来指
我最近几天才开始使用Groovy和Grails.我之前没有任何 Java经验,所以你不得不原谅这个(可能)非常基本的问题.我搜索过谷歌和Stack Overflow,但没有找到任何可以帮助我实际安装的东西.

我有一个图像上传工作,我将文件存储在服务器上.我使用了IBM Grails教程来指导我完成它.这很好.

我还想以大,中,小格式调整文件大小.我想为此使用imgscalr,但我无法让它工作.我已下载4.2版,其中包含各种.jar文件.我是否需要将这些内容放在服务器上并引用它们?我唯一做的就是将这些行添加到buildConfig.groovy

dependencies {
    // specify dependencies here under either 'build','compile','runtime','test' or 'provided' scopes eg.

    // runtime 'mysql:mysql-connector-java:5.1.20'
    compile 'org.imgscalr:imgscalr-lib:4.2'
}

并在我的PhotoController.Groovy中导入org.imgscalr.Scalr.*

这是我将文件保存到服务器的代码,我还想调整大小并保存图像.

def save() {
    def photoInstance = new Photo(params)

    // Handle uploaded file
    def uploadedFile = request.getFile('photoFile')
    if(!uploadedFile.empty) {
        println "Class: ${uploadedFile.class}"
        println "Name: ${uploadedFile.name}"
        println "OriginalFileName: ${uploadedFile.originalFilename}"
        println "Size: ${uploadedFile.size}"
        println "ContentType: ${uploadedFile.contentType}"

        def webRootDir = servletContext.getRealPath("/")

        def originalPhotoDir = new File(webRootDir,"/images/photographs/original")
        originalPhotoDir.mkdirs()
        uploadedFile.transferTo(new File(originalPhotoDir,uploadedFile.originalFilename))

        BufferedImage largeImg = Scalr.resize(uploadedFile,1366);
        def largePhotoDir = new File(webRootDir,"/images/photographs/large")
        largePhotoDir.mkdirs()

        photoInstance.photoFile = uploadedFile.originalFilename
    }

    if (!photoInstance.hasErrors() && photoInstance.save()) {
        flash.message = "Photo ${photoInstance.id} created"
        redirect(action:"list")
    }
    else {
        render(view:"create",model:[photoInstance: photoInstance])
    }
}

我得到的错误是没有这样的属性:类的Scalr:garethlewisweb.PhotoController

我显然做了一件非常错事.任何指导赞赏.

解决方法

这是“如何在grails中使用imgscalr”的第一个谷歌搜索结果,我对Google搜索时缺乏信息和示例感到惊讶.虽然第一个答案很接近,但仍有一些错误需要纠正.

对于像我这样通过谷歌结束的任何人,下面是一个更详细的例子,说明如何正确使用这个漂亮的插件:

首先,在BuildConfig.groovy文件中声明插件:

dependencies {
    // specify dependencies here under either 'build','test' or 'provided' scopes eg.

    // runtime 'mysql:mysql-connector-java:5.1.20'
    compile 'org.imgscalr:imgscalr-lib:4.2'
}

然后,安装完成后,只需将这段代码粘贴到控制器中,即可在接收到已上传图像的多部分表单的操作中粘贴.

def create() {

     def userInstance = new User(params)

     //saving image
     def imgFile = request.getFile('myFile')
     def webRootDir = servletContext.getRealPath("/")
     userInstance.storeImageInFileSystem(imgFile,webRootDir)  

     (...) 
}

在我的域内,我实现了这个storeImageInFileSystem方法,它将调整图像大小并将其存储在文件系统中.但首先,将其导入到文件中:

import org.imgscalr.Scalr
import java.awt.image.BufferedImage
import javax.imageio.ImageIO

然后,实现方法:

def storeImageInFileSystem(imgFile,webRootDir){

    if (!imgFile.empty)
    {
        def defaultPath = "/images/userImages"
        def systemDir = new File(webRootDir,defaultPath)

        if (!systemDir.exists()) {
            systemDir.mkdirs()
        }

        def imgFileDir = new File( systemDir,imgFile.originalFilename)
        imgFile.transferTo( imgFileDir )

        def imageIn = ImageIO.read(imgFileDir);

        BufferedImage scaledImage = Scalr.resize(imageIn,200);   //200 is the size of the image
        ImageIO.write(scaledImage,"jpg",new File( systemDir,imgFile.originalFilename )); //write image in filesystem

       (...)
    }
}

这对我很有用.根据需要更改任何详细信息,例如系统指令或图像大小.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读