Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Commit

Permalink
Rotate image at end (performance optimization)
Browse files Browse the repository at this point in the history
  • Loading branch information
chdanielmueller committed Feb 16, 2018
1 parent a8827e6 commit 6fa084e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/classes/picture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface Picture {
normalizedURL: string;
fileEntry: FileEntry;
base64Data: string;
imageOrientation: number;
}
2 changes: 1 addition & 1 deletion src/pages/camera/camera.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<!-- Picture Display -->
<ion-row nowrap justify-content-start align-items-center>
<ion-col *ngFor="let picture of pictures; let i = index" col-auto>
<img tappable [src]="'data:image/jpeg;base64,' + picture.base64Data" class="thumbnail" (click)="editPicture(picture, i)">
<img tappable [src]="'data:image/jpeg;base64,' + picture.base64Data" [style.transform]="'rotate(' + picture.imageOrientation + 'deg)'" class="thumbnail" (click)="editPicture(picture, i)">
</ion-col>
</ion-row>

Expand Down
64 changes: 49 additions & 15 deletions src/pages/camera/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,8 @@ export class CameraComponent {
}

public takePicture(): void {
const imageOrientation = this.getImageOrientation();
this.cameraPreview.takePicture(this.pictureOptions)
.then(picture => {
return this.rotateImageBasedOnOrientation(picture);
})
.then(picture => {
const fileOptions: IWriteOptions = {
replace: true
Expand All @@ -167,7 +165,8 @@ export class CameraComponent {
this.pictures.push({
fileEntry,
normalizedURL,
base64Data: picture
base64Data: picture,
imageOrientation
});
})
.catch(err => {
Expand All @@ -180,7 +179,46 @@ export class CameraComponent {
}

public finish(): void {
this.exit();
const promises = [];
this.pictures.forEach(picture => {
promises.push(
new Promise((resolve, reject) => {
rotateBase64Image(picture.base64Data, picture.imageOrientation)
.then(imageData => {
picture.imageOrientation = 0;
picture.base64Data = imageData;
picture.fileEntry.createWriter(fileWriter => {
fileWriter.onwriteend = () => {
resolve(picture);
};
fileWriter.onerror = function (e) {
reject(e);
};
const file: Blob = base64toBlob(imageData, 'image/jpeg');
fileWriter.truncate(file.size);
fileWriter.write(file);
}, err => {
reject(err);
});
})
.catch(err => {
reject(err);
});
})
);
});

Promise.all(promises)
.then(results => {
this.callback({
pictures: results
});
this.doesExit = true;
this.navCtrl.pop();
})
.catch(err => {
this.errorHandler(err);
});
}

public editPicture(picture: Picture, index: number): void {
Expand Down Expand Up @@ -251,30 +289,26 @@ export class CameraComponent {
this.cameraPreview.stopCamera();
}

private rotateImageBasedOnOrientation(imageData: string): Promise<string> {
private getImageOrientation(): number {
if (this.deviceOrientation) {
// If landscape
if (Math.abs(this.deviceOrientation.x) > Math.abs(this.deviceOrientation.y)) {
if (this.deviceOrientation.x > 0) {
return rotateBase64Image(imageData, 270);
return 270;
} else {
return rotateBase64Image(imageData, 90);
return 90;
}
} else {
// Portrait upside-down
if (this.deviceOrientation.y < 0) {
return rotateBase64Image(imageData, 180);
return 180;
} else {
// Right-side up
return new Promise((resolve) => {
resolve(imageData);
});
return 0;
}
}
} else {
return new Promise((resolve) => {
resolve(imageData);
});
return 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/edit/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@

<!-- Content -->
<ion-content>
<img [src]="'data:image/jpeg;base64,' + picture.base64Data">
<img [src]="'data:image/jpeg;base64,' + picture.base64Data" [style.transform]="'rotate(' + picture.imageOrientation + 'deg)'">
</ion-content>
21 changes: 4 additions & 17 deletions src/pages/edit/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { NavController, NavParams } from 'ionic-angular';
// Classes
import { Picture } from '../../classes/picture';

// Helpers
import { base64toBlob, rotateBase64Image } from '../../helpers/picture-mutations';

@Component({
selector: 'ionic-multi-edit',
templateUrl: 'edit.html',
Expand Down Expand Up @@ -42,20 +39,10 @@ export class EditComponent {
}

public rotateLeft(): void {
rotateBase64Image(this.picture.base64Data, 270)
.then(imageData => {
this.picture.base64Data = imageData;
this.picture.fileEntry.createWriter(fileWriter => {
const file: Blob = base64toBlob(imageData, 'image/jpeg');
fileWriter.truncate(file.size);
fileWriter.write(file);
}, err => {
console.error(err);
});
})
.catch(err => {
console.error(err);
});
this.picture.imageOrientation += 90;
if (this.picture.imageOrientation >= 360) {
this.picture.imageOrientation - 360;
}
}

}

0 comments on commit 6fa084e

Please sign in to comment.