数据库base64格式图片查询返回给前端

一、问题描述

​ 目前我们在开发的时候,图片大部分都存在文件服务器(例如minio),数据库中只存储对应的访问地址,前端在访问的时候只需用文件服务器的地址拼接上对应的图片地址即可,但在有些情况下需要把图片以base64的形式存储在数据库中。此时如果直接返回base64数据给前端的话,会造成返回数据量过大,被浏览器给组织掉,从而无法正常加载数据。
在这里插入图片描述

二、问题解决

1、提供相关接口可以查询对应的base64数据并转成在线图片,即通过ip:port拼上URL即可访问图片

/**
* 根据id和列名返回对应的图片
* @param response
* @param id
* @param column
* @return
*/
public void getImageByColumn(HttpServletResponse response, Integer id, String column) {
        String carDatas = this.carDataMapper.getCarDataByColumn(id, column);
        base64ToImage(response, carDatas);
}

/**
* 根据id和列名查询对应字段值
* @param id
* @param column
* @return
*/
@Select("SELECT  " +
        "${column} " +
        "FROM car_data WHERE id = #{id}")
String getCarDataByColumn(Integer id, String column);

/**
 * base64图片转图片返回给前端
 * @param response
 * @param base64
 */
public static void base64ToImage(HttpServletResponse response, String base64) {
    try {
        //设置输出文件格式
        response.setContentType("image/png");
        //将Base64格式的图片解码成字节
        byte[] bytes = Base64.getDecoder().decode(base64);
        InputStream inStream = new ByteArrayInputStream(bytes);
        OutputStream outputStream = null;
        outputStream = new BufferedOutputStream(response.getOutputStream());

        //创建存放文件内容的数组
        byte[] buff = new byte[1024];
        //所读取的内容使用n来接收
        int n;
        //当没有读取完时,继续读取,循环
        while ((n = inStream.read(buff)) != -1) {
            //将字节数组的数据全部写入到输出流中
            outputStream.write(buff, 0, n);
        }
        //强制将缓存区的数据进行输出
        outputStream.flush();
        //关流
        outputStream.close();
        inStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

在这里插入图片描述

2、在分页查询接口中返回对应数据的url

public PageInfo<FaceData> getFaceDatas(int page, int count, String communityId, Integer passType, String personName, String idNumber, Integer type, Long start, Long end) {
    PageHelper.startPage(page, count);
    List<FaceData> faceDatas = this.faceDataMapper.getFaceDatas(communityId, passType, personName, idNumber, type, start, end);
    faceDatas.stream().forEach(faceData -> {
        faceData.setPhotoBase1(faceData.getPhotoBase1() == null ? null : "/faceData/getImageByColumn?id=" + faceData.getId() + "&column=photoBase1");
        faceData.setPhotoBase2(faceData.getPhotoBase2() == null ? null : "/faceData/getImageByColumn?id=" + faceData.getId() + "&column=photoBase2");
        faceData.setPhotoBase3(faceData.getPhotoBase3() == null ? null : "/faceData/getImageByColumn?id=" + faceData.getId() + "&column=photoBase3");
        faceData.setBigImageBase64(faceData.getBigImageBase64() == null ? null : "/faceData/getImageByColumn?id=" + faceData.getId() + "&column=bigImageBase64");
    });
    return new PageInfo<>(faceDatas);
}

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用uniapp内置的base64图片方法,具体步骤如下: 1. 在template中使用image标签,设置src属性为base64字符串。 2. 在script中使用uni.base64ToArrayBuffer方法将base64字符串转为ArrayBuffer类型。 3. 使用uni.getImageInfo方法获取图片信息,包括宽度和高度。 4. 使用uni.canvasToTempFilePath方法将ArrayBuffer类型的图片转为临时文件路径。 5. 将临时文件路径赋值给image标签的src属性,即可显示图片。 示例代码如下: <template> <view> <image :src="imgSrc"></image> </view> </template> <script> export default { data() { return { base64Str: 'base64字符串', imgSrc: '' } }, methods: { async base64ToImg() { const arrayBuffer = await uni.base64ToArrayBuffer(this.base64Str) const { width, height } = await uni.getImageInfo({ src: this.base64Str }) const tempFilePath = await uni.canvasToTempFilePath({ x: , y: , width, height, destWidth: width, destHeight: height, canvasId: 'canvas', fileType: 'jpg', quality: 1, data: arrayBuffer }) this.imgSrc = tempFilePath.tempFilePath } }, mounted() { this.base64ToImg() } } </script> ### 回答2: 在uniapp中,我们可以将一张图片编码为base64格式的字符串,然后将其在页面中进行显示。同时,我们也可以将一个base64格式的字符串转换为一张图片。 通过使用uniapp内置的Base64模块,我们可以方便地将图片数据编码为base64格式的字符串。具体的步骤如下: 1. 获取图片数据。 我们可以通过uniapp提供的API获取本地文件系统中的图片数据。例如,以下代码可以获取并读取图片: ``` uni.chooseImage({ success: function (res) { var tempFilePath = res.tempFilePaths[0]; uni.getFileSystemManager().readFile({ filePath: tempFilePath, encoding: 'base64', success: function (res) { var base64 = 'data:image/jpeg;base64,' + res.data; } }); } }); ``` 2. 将图片数据编码为base64格式的字符串。 在获取到图片数据之后,我们可以借助Base64模块将其编码为base64格式的字符串。具体的代码如下: ``` var base64 = uni.base64ToArrayBuffer(res.data); var binary = ''; var bytes = new Uint8Array(base64); var len = bytes.byteLength; for (var i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } var base64String = btoa(binary); ``` 3. 将base64格式的字符串转换为图片。 我们可以将一个base64格式的字符串转换为一张图片,并将其展示在页面中。具体的代码如下: ``` <img :src="base64String"/> ``` 通过以上步骤,我们可以在uniapp中实现将base64格式图片字符串转换为图片并显示到页面上的功能。同时,我们也可以将一张图片编码为base64格式的字符串并进行传输和处理。这种方法通常用于在移动端进行图片显示和上传等操作。 ### 回答3: Uniapp是一个跨平台的开发框架,它可以让开发者使用一套代码编写出同时适用于iOS、Android和H5的应用程序。在Uniapp中,我们可以通过将图片采用base64编码的方式存储在后台,然后直接将base64字符串传递到前端,最后通过js的方式将base64字符串转换为可被浏览器渲染并显示的图片。 具体操作流程如下: 1. 后台将图片转换为base64编码,将其保存在数据库中或者直接传递到前端。在后台中可以使用Python等语言的base64库将图片转换为base64编码。 2. 前端接收到base64字符串后,使用以下JS代码将其转换为图片并显示: ``` // 将base64字符串转换为ArrayBuffer var arr = btoa(base64String); var binary_string = window.atob(arr); var len = binary_string.length; var bytes = new Uint8Array(len); for (var i = 0; i < len; i++) { bytes[i] = binary_string.charCodeAt(i); } var blob = new Blob([bytes.buffer], { type: "image/png" }); // 在html中添加img标签并显示图片 var objUrl = URL.createObjectURL(blob); var img = document.createElement("img"); img.src = objUrl; // 将img标签添加到需要显示的dom节点中 ``` 以上代码将base64字符串转换为ArrayBuffer、Blob和ObjectURL等格式,并最终显示在浏览器中。 需要注意一点的是,在使用base64编码的图片时,会占用更多的内存,因此在使用时应注意优化图片尺寸和压缩比例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值