【发布时间】:2020-01-13 13:59:29
【问题描述】:
我编写了一个闪亮的应用程序,允许用户在图像顶部绘制矩形(下面的最小可重现示例)。
我当前方法的问题是每次添加一个矩形时,都会创建一个新图像,将其写入磁盘并渲染(发送到用户的浏览器)。这需要相当长的时间,并且在互联网连接缓慢时变得非常烦人。
有什么方法可以在图像顶部显示矩形直接在浏览器中,而不需要在服务器端修改图像?我唯一需要确保的是浏览器将绘图上的矩形坐标发送回服务器。
我正在寻找的一个很好的例子(在 JavaScript 中):https://kyamagu.github.io/bbox-annotator/demo.html 我知道 JavaScript 可以通过小部件嵌入到 Shiny 应用中,如果没有人提出更简单的解决方案,我会这样做。
library(shiny)
library(png)
library(RCurl)
myurl = 'https://raw.githubusercontent.com/Tixierae/deep_learning_NLP/master/CNN_IMDB/cnn_illustration.png'
my_img = readPNG(getURLContent(myurl))
img_height = dim(my_img)[1]
img_width = dim(my_img)[2]
server = function(input, output) {
observe({
outfile = tempfile(tmpdir='./', fileext='.png')
png(filename=outfile,width=img_width,height=img_height)
par(mar=c(0,0,0,0),xaxs='i', yaxs='i')
plot(NA,xlim=c(0,img_width),ylim=c(0,img_height))
rasterImage(my_img,0,0,img_width,img_height)
if (!is.null(input$image_brush)){
b_in = lapply(input$image_brush,as.numeric)
if (!is.null(b_in$xmin)){
rect(b_in$xmin,img_height-b_in$ymax,b_in$xmax,img_height-b_in$ymin,border='green',lwd=5)
}
}
dev.off()
output$my_image = renderImage({
list(
src = outfile,
contentType = 'image/png',
width = img_width,
height = img_height,
alt = ''
)
},deleteFile=TRUE)
output$image = renderUI({
imageOutput('my_image',
height = img_height,
width = img_width,
click = 'image_click',
dblclick = dblclickOpts(
id = 'image_dblclick'
),
hover = hoverOpts(
id = 'image_hover'
),
brush = brushOpts(
id = 'image_brush',resetOnNew=TRUE,delayType='debounce',delay=100000
)
)
})
})
}
ui = bootstrapPage(
uiOutput('image')
)
shinyApp(ui=ui, server=server)
【问题讨论】:
标签: javascript image browser shiny draw