UE4上传图片
先记录下:
1 void UploadImageFromFile(FString FileName) 2 { 3 // load image 4 TArray<uint8> ImageData; 5 FFileHelper::LoadFileToArray(ImageData, *FileName); 6 7 TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest(); 8 FString Boundary = "---------------------------" + FString::FromInt(FDateTime::Now().GetTicks()); 9 HttpRequest->SetHeader(TEXT("Content-Type"), FString(TEXT("multipart/form-data; boundary=")) + Boundary); 10 HttpRequest->SetURL(TEXT("http://127.0.0.1/image/upload")); 11 HttpRequest->SetVerb(TEXT("POST")); 12 13 FString InFileFormat(TEXT(".jpg")); 14 FString FileContents = FBase64::Encode(ImageData); 15 FString HashStr = FMD5::HashAnsiString(*FileName); 16 17 FString PrefixBoundry = "\r\n--" + Boundary + "\r\n"; 20 21 FString FileHeader = "Content-Disposition: form-data; name=\"file\"; filename=\"" + FileName 22 + "\"\r\nContent-Type: " + InFileFormat 23 + "\"\r\nmd5Sum: " + HashStr + "\r\n\r\n"; 24 25 FString SuffixBoundary = "\r\n--" + Boundary + "--\r\n"; 26 27 /*FString ContentsString = PrefixBoundry + FileHeader + FileContents + SuffixBoundary; 28 HttpRequest->SetContentAsString(ContentsString);*/ 29 30 TArray<uint8> ContentArray; 31 FString ContentsString = PrefixBoundry + FileHeader; 32 ContentArray.Append((uint8*)TCHAR_TO_ANSI(*ContentsString), ContentsString.Len()); 33 ContentArray.Append(ImageData); 34 ContentArray.Append((uint8*)TCHAR_TO_ANSI(*SuffixBoundary), SuffixBoundary.Len()); 35 HttpRequest->SetContent(ContentArray); 36 37 //绑定回调 38 HttpRequest->OnProcessRequestComplete().BindRaw(this, &HandleHttpRequestComplete, EHttpReqType::PostImage); 39 40 //发送请求 41 HttpRequest->ProcessRequest(); 42 }
在获取图片后需要将返回的数据流转化为图片信息:
1 // ... 2 TArray<uint8> OutImageData = HttpResponse->GetContent(); 3 UE_LOG(LogTemp, Warning, TEXT("Image Size = %d "), OutImageData.Num()); 4 if (OutImageData.Num() <= 0) 5 return; 6 7 EImageFormat TmepFormat = GetTargetImageFormat(Extension); 8 9 IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked <IImageWrapperModule>(FName("ImageWrapper")); 10 TSharedPtr<IImageWrapper> SourceImageWrapper = ImageWrapperModule.CreateImageWrapper(TmepFormat); 11 12 if (SourceImageWrapper.IsValid() 13 && SourceImageWrapper->SetCompressed(OutImageData.GetData(), OutImageData.GetAllocatedSize())) 14 { 15 TArray <uint8> UncompressedBGRA; 16 if (SourceImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA)) 17 { 18 int32 Height = SourceImageWrapper->GetHeight(); 19 int32 Width = SourceImageWrapper->GetWidth(); 20 UTexture2D* InTexture = UTexture2D::CreateTransient(Width, Height, PF_B8G8R8A8); 21 22 if (InTexture) 23 { 24 void* TextureData = InTexture->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE); 25 FMemory::Memcpy(TextureData, UncompressedBGRA.GetData(), UncompressedBGRA.Num()); 26 InTexture->GetPlatformData()->Mips[0].BulkData.Unlock(); 27 InTexture->UpdateResource(); 28 29 // 30 TargetTexture = InTexture; 31 OnGetTargetImage.ExecuteIfBound(TargetTexture); 32 } 33 } 34 }