Created
April 19, 2019 10:50
-
-
Save NeelBhatt/6249e48977c43f4829d87f072ff77bdb to your computer and use it in GitHub Desktop.
PiCameraService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PiCameraService | |
{ | |
public MMALCamera MMALCamera; | |
private readonly string picStoragePath = "/home/pi/images/"; | |
private readonly string picExtension = "jpg"; | |
public PiCameraService() | |
{ | |
MMALCamera = MMALCamera.Instance; | |
//Setting the Average resolution for reducing the file size | |
MMALCameraConfig.StillResolution = new Resolution(640, 480); | |
} | |
public async Task<byte[]> CapturePictureAsByteArray() | |
{ | |
var fileName = await CapturePictureAndGetFileName(); | |
string filePath = Path.Join(picStoragePath, $"{fileName}.{picExtension}"); | |
byte[] resultData = await File.ReadAllBytesAsync(filePath); | |
//Delete the captured picture from PI storage | |
File.Delete(filePath); | |
return resultData; | |
} | |
public async Task<Stream> CapturePictureAsStream() | |
{ | |
return new MemoryStream(await CapturePictureAsByteArray()); | |
} | |
private async Task<string> CapturePictureAndGetFileName() | |
{ | |
string fileName = null; | |
using (var imgCaptureHandler = new ImageStreamCaptureHandler(picStoragePath, picExtension)) | |
{ | |
await MMALCamera.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420); | |
fileName = imgCaptureHandler.GetFilename(); | |
} | |
return fileName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment