void UploadImageFromFile(FString FileName) { TArray<uint8> ImageData; FFileHelper::LoadFileToArray(ImageData, *FileName); TSharedRef<IHttpRequest> HttpRequest = FHttpModule::Get().CreateRequest(); FString Boundary = "---------------------------" + FString::FromInt(FDateTime::Now().GetTicks()); HttpRequest->SetHeader(TEXT("Content-Type"), FString(TEXT("multipart/form-data; boundary=")) + Boundary); HttpRequest->SetURL(TEXT("http://127.0.0.1/image/upload")); HttpRequest->SetVerb(TEXT("POST")); FString InFileFormat(TEXT(".jpg")); FString FileContents = FBase64::Encode(ImageData); FString HashStr = FMD5::HashAnsiString(*FileName); FString PrefixBoundry = "\r\n--" + Boundary + "\r\n"; FString FileHeader = "Content-Disposition: form-data; name=\"file\"; filename=\"" + FileName + "\"\r\nContent-Type: " + InFileFormat + "\"\r\nmd5Sum: " + HashStr + "\r\n\r\n"; FString SuffixBoundary = "\r\n--" + Boundary + "--\r\n";
TArray<uint8> ContentArray; FString ContentsString = PrefixBoundry + FileHeader; ContentArray.Append((uint8*)TCHAR_TO_ANSI(*ContentsString), ContentsString.Len()); ContentArray.Append(ImageData); ContentArray.Append((uint8*)TCHAR_TO_ANSI(*SuffixBoundary), SuffixBoundary.Len()); HttpRequest->SetContent(ContentArray); HttpRequest->OnProcessRequestComplete().BindRaw(this, &HandleHttpRequestComplete, EHttpReqType::PostImage); HttpRequest->ProcessRequest(); }
TArray<uint8> OutImageData = HttpResponse->GetContent(); UE_LOG(LogTemp, Warning, TEXT("Image Size = %d "), OutImageData.Num()); if (OutImageData.Num() <= 0) return;
EImageFormat TmepFormat = GetTargetImageFormat(Extension);
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked <IImageWrapperModule>(FName("ImageWrapper")); TSharedPtr<IImageWrapper> SourceImageWrapper = ImageWrapperModule.CreateImageWrapper(TmepFormat);
if (SourceImageWrapper.IsValid() && SourceImageWrapper->SetCompressed(OutImageData.GetData(), OutImageData.GetAllocatedSize())) { TArray <uint8> UncompressedBGRA; if (SourceImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA)) { int32 Height = SourceImageWrapper->GetHeight(); int32 Width = SourceImageWrapper->GetWidth(); UTexture2D* InTexture = UTexture2D::CreateTransient(Width, Height, PF_B8G8R8A8);
if (InTexture) { void* TextureData = InTexture->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE); FMemory::Memcpy(TextureData, UncompressedBGRA.GetData(), UncompressedBGRA.Num()); InTexture->GetPlatformData()->Mips[0].BulkData.Unlock(); InTexture->UpdateResource();
TargetTexture = InTexture; OnGetTargetImage.ExecuteIfBound(TargetTexture); } } }
|