While working on an Ionic project, the client asked for a timestamp to be added at the bottom of a taken photo.
After some head scratching, I ended up with the solution below.
onTakePhoto(): void { const options: CameraOptions = { quality: 30, destinationType: this.camera.DestinationType.DATA_URL, encodingType: this.camera.EncodingType.JPEG, mediaType: this.camera.MediaType.PICTURE, targetWidth: 2304, targetHeight: 1728 }; this.camera.getPicture(options).then( (imageData) => { this.generateImageWithTimestamp("data:image/jpeg;base64," + imageData); }, (error) => { // not important now } ); } generateImageWithTimestamp(dataUrl: string): void { let image = new Image(); image.onload = () => { let canvas = document.createElement("canvas"); let context = canvas.getContext("2d"); canvas.height = image.height; canvas.width = image.width; context.drawImage(image, 0, 0); //adding some styling context.font = "80px Arial bold"; context.textAlign = "center"; context.fillStyle = "orange"; context.fillText( this.getTimeStamp(), image.width / 2, //center image.height * 0.97 //close to the bottom ); const finalImage = canvas.toDataURL("image/jpeg", 1); }; image.src = dataUrl; } getTimeStamp(): string { const date = new Date(); return `${date.getDate()}. ${date.getMonth() +1}. ${date.getFullYear()}, ${date.getHours()}:${date.getMinutes()}`; }
We simply take a photo, redraw it on a canvas where we also add the timestamp.
One thing to keep in mind here is that order is important.
In my first attempt I first added the text and then the image which, of course, has overwritten the text. It looks obvious now but it took me a little to figure it out.
Hope this will help you out.
Until next time,
Happy coding.