从SocketR连接在浏览器中显示base64编码的图像
我正在r中构建一个API,它从解析的url向浏览器返回特定的图像 当前,浏览器显示base64编码的字符串,而不是渲染图像 这是一个可重复的示例:从SocketR连接在浏览器中显示base64编码的图像,r,sockets,base64,R,Sockets,Base64,我正在r中构建一个API,它从解析的url向浏览器返回特定的图像 当前,浏览器显示base64编码的字符串,而不是渲染图像 这是一个可重复的示例: #### Reproducable Example for Stack-Overflow library("plyr") library("Cairo") require("png") library("base64enc") # Always 'listening' server for returning image of mtc
#### Reproducable Example for Stack-Overflow
library("plyr")
library("Cairo")
require("png")
library("base64enc")
# Always 'listening' server for returning image of mtcars dataset
Test_server <- function(){
while(TRUE){
writeLines("Listening...")
CON <- socketConnection(host="localhost", port = 6011, blocking=TRUE,
server=TRUE, open="r+")
New_Request <- readLines(CON, 3)
print(New_Request)
Search <- strsplit(New_Request[1], split="/")[[1]][2]
print(Search)
IMG_Name <- paste0(Search, ".png")
CairoPNG(IMG_Name, width=640, height=450)
plot(mtcars[Search])
IMG_TEST <- Cairo.capture()
# dev.off()
IMG_data <- dataURI(writePNG(IMG_TEST), mime="image/png")
### I have tried the following commands to print the IMAGE not the base64 to the browser:
# SEND_img <- paste0("<img width='640' height='450' src='", IMG_data, "'></img>")
# SEND_img <- paste0('<img src="', IMG_data, '"/>')
SEND_img <- cat(sprintf("<img src=\"%s\" />", IMG_data), file=CON)
# writeLines(SEND_img, CON)
#Remember to close the connection!
close(CON)
}
}
# Now run the server
Test_server()
或
浏览器将在预标记中显示base64的内部。
如何让所有(现代)浏览器显示此API中的图像
谢谢
编辑:新的工作代码如下
# Always 'listening' server for returning image of mtcars dataset
Test_server <- function(){
while(TRUE){
writeLines("Listening...")
CON <- socketConnection(host="localhost", port = 6011, blocking=TRUE,
server=TRUE, open="a+b")
New_Request <- readLines(CON, 3)
print(New_Request)
Search <- strsplit(New_Request[1], split="/")[[1]][2]
print(Search)
if (Search != "favicon.ico HTTP") {
IMG_Name <- paste0(Search, ".png")
CairoPNG(IMG_Name, width=640, height=450)
plot(mtcars[Search])
IMG_TEST <- Cairo.capture()
dev.off()
IMG_data <- dataURI(writePNG(IMG_TEST), mime="image/png")
SEND_img <- cat(sprintf("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<img src=\"%s\" />", IMG_data), file=CON)
}
#Remember to close the connection!
close(CON)
}
}
# Now run the server
Test_server()
#始终“侦听”服务器以返回mtcars数据集的图像
Test_server您需要HTTP头,包括将内容类型设置为text/html的头,否则浏览器不知道如何处理响应。事实上,任何浏览器都会在没有标题的HTTP响应中显示任何内容,这有点奇怪
将SEND_img行替换为以下内容:
没人知道base64的事?
http:http://127.0.0.1:6011/disp/
# Always 'listening' server for returning image of mtcars dataset
Test_server <- function(){
while(TRUE){
writeLines("Listening...")
CON <- socketConnection(host="localhost", port = 6011, blocking=TRUE,
server=TRUE, open="a+b")
New_Request <- readLines(CON, 3)
print(New_Request)
Search <- strsplit(New_Request[1], split="/")[[1]][2]
print(Search)
if (Search != "favicon.ico HTTP") {
IMG_Name <- paste0(Search, ".png")
CairoPNG(IMG_Name, width=640, height=450)
plot(mtcars[Search])
IMG_TEST <- Cairo.capture()
dev.off()
IMG_data <- dataURI(writePNG(IMG_TEST), mime="image/png")
SEND_img <- cat(sprintf("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<img src=\"%s\" />", IMG_data), file=CON)
}
#Remember to close the connection!
close(CON)
}
}
# Now run the server
Test_server()