1 Django服务端传输图片到鸿蒙APP
1.1 鸿蒙APP
1.1.1 HML
<div id="wrapper">
<button id="button1" type="capsule" onclick="onClick">获取数据</button>
<image id="image2" src="{{ getImgBase64 }}"></image>
</div>
1.1.2 JS
import router from '@system.router';
import prompt from '@system.prompt';
import http from '@ohos.net.http';
export default {
data: {
getImgBase64:''
},
onClick(e){
let httpRequest = http.createHttp();
let url = "http://xxx.xx.xxx.xxx:8000/hssl_django_app/getDtcImg/";
httpRequest.request(url, {
method: 'POST',
}, (err, data)=> {
if (!err) {
var back_2 = data.result;
this.getImgBase64 = "data:image/jpg;base64," + back_2
prompt.showToast({
message: "更新成功",
duration: 5000,
})
} else {
prompt.showToast({
message: 'API请求错误',
duration: 3000,
})
}
})
},
}
1.2 Django
hssl_django_app/views.py
import base64
import cv2
from django.http import HttpResponse
from rest_framework.views import APIView
class GetDtcImg(APIView):
def post(self, request):
try:
img = cv2.imread('photo.png')
img1 = cv2.imencode('.jpg', img)[1]
back_2 = base64.b64encode(img1)
print(back_2)
return HttpResponse(back_2)
except:
return HttpResponse("图片发送失败")
hssl_django_app/urls.py
from django.conf.urls import url
from hssl_django_app.views import GetDtcImg
app_name = 'hssl_django_app'
urlpatterns = [
url('getDtcImg/', GetDtcImg.as_view()),
]
|