我在 C# 中按如下方式执行此操作。我们编写的代码从帧中捕获字节数组,创建一个 Image 类型,从下面的函数转换为 Bitmap 类型,然后保存为压缩的 jpeg 以供以后保存在文件系统上:
using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
{
if (depthFrame == null) return;
byte[] pixels = GenerateColoredBytes(depthFrame, first);
int stride = depthFrame.Width * 4;
depthImage.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
Bitmap btmap = GetBitmap((BitmapSource)depthImage.Source);
// Encoder parameter for image quality
long quality = 100000;
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
// Jpeg image codec
ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
var stream = new MemoryStream(); // Create MemoryStream
btmap.Save(stream, jpegCodec, encoderParams); //(stream, ImageFormat.Jpeg); // Save Bitmap as .jpeg in MemoryStream
depthPath = "_UserTrialImages/" + UsernameInput.username + "/Date-" + DateTime.Now.ToString("MM.dd.yyyy");
gamePath = depthPath + "/Game-" + gameNumber;
trajectoryPath = gamePath + "/Trajectory-" + trajectoryNumber; // one trajectory per super bubble appearance
string depthFile = "image" + ind[5] + ind[4] + ind[3] + ind[2] + ind[1] + ind[0] + ".jpg";
string pathFile = trajectoryPath + '/' + depthFile;
imageNumberCounter();
newImg.pathFile = pathFile;
newImg.stream = stream;
// saves depth images in list
listOfImages.Add(newImg);
}
调用此函数:
Bitmap GetBitmap(BitmapSource source)
{
Bitmap bmp = new Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
BitmapData data = bmp.LockBits(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size),
ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
source.CopyPixels(
Int32Rect.Empty,
data.Scan0,
data.Height * data.Stride,
data.Stride);
bmp.UnlockBits(data);
return bmp;
}
我在 C++ 中没有此功能,但如果您在 .NET 4.0 中编码,则应该有与上述 C++ 中相同的功能。希望这可以帮助。