Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {HttpClient} from '@angular/common/http';
  2. import {Injectable} from '@angular/core';
  3. import {EXIF} from 'exif-js';
  4. import {File, FileEntry} from '@ionic-native/file';
  5.  
  6. /*
  7.   Generated class for the EditorProvider provider.
  8.  
  9.   See https://angular.io/guide/dependency-injection for more info on providers
  10.   and Angular DI.
  11. */
  12. @Injectable()
  13. export class EditorProvider {
  14.   canvas: any;
  15.   context: any;
  16.   image: any;
  17.   imageData: any;
  18.   pixels: any;
  19.   numPixels: number;
  20.   functions: any = {
  21.     brightContrast: this.brightContrast,
  22.     colorFilter: this.colorFilter,
  23.   };
  24.  
  25.   // editor variables
  26.   contrast: string = '10';
  27.   brightness: string = '0';
  28.  
  29.   red: string = '128';
  30.   green: string = '128';
  31.   blue: string = '128';
  32.   intensity: string = '50';
  33.  
  34.   constructor(public http: HttpClient, private file: File) {
  35.     console.log('Hello EditorProvider Provider');
  36.   }
  37.  
  38.   setElements(c, i) {
  39.     this.canvas = c;
  40.     this.context = this.canvas.getContext('2d');
  41.     this.image = i;
  42.     // console.log(this.image);
  43.   };
  44.  
  45.   setFile(imData): void {
  46.     this.file.resolveLocalFilesystemUrl(imData).
  47.         then(entry => (<FileEntry>entry).file(file => this.filetoCanvas(file))).
  48.         catch(err => console.log(err));
  49.   }
  50.  
  51.   filetoCanvas(file) {
  52.     const reader: FileReader = new FileReader();
  53.  
  54.     reader.onload = () => {
  55.       // console.log(reader.result);
  56.       this.image.src = reader.result;
  57.       this.image.onload = () => this.resetImage();
  58.     };
  59.  
  60.     reader.readAsDataURL(file);
  61.   };
  62.  
  63.   resetImage() {
  64.     // console.log(this.image);
  65.     this.canvas.height = this.image.height;
  66.     this.canvas.width = this.image.width;
  67.  
  68.     this.context.drawImage(this.image, 0, 0, this.image.width,
  69.         this.image.height);
  70.     this.imageData = this.context.getImageData(0, 0, this.canvas.width,
  71.         this.canvas.height);
  72.     this.pixels = this.imageData.data;
  73.     this.numPixels = this.imageData.width * this.imageData.height;
  74.   };
  75.  
  76.   applyFilters() {
  77.     this.resetImage();
  78.  
  79.     for (let i in this.functions) {
  80.       if (this.functions.hasOwnProperty(i)) {
  81.         this.functions[i](this);
  82.       }
  83.     }
  84.  
  85.   }
  86.  
  87.   // filters
  88.  
  89.   brightContrast(thisClass) {
  90.     console.log(thisClass);
  91.     // console.log(thisClass.brightness);
  92.     // console.log(thisClass.contrast);
  93.     let contrast = parseFloat(thisClass.contrast)/10;
  94.     let brightness = parseInt(thisClass.brightness);
  95.     for (let i = 0; i < thisClass.numPixels; i++) {
  96.       thisClass.pixels[i * 4] = (thisClass.pixels[i * 4] - 128) * contrast + 128 +
  97.           brightness; // Red
  98.       thisClass.pixels[i * 4 + 1] = (thisClass.pixels[i * 4 + 1] - 128) * contrast +
  99.           128 + brightness; // Green
  100.       thisClass.pixels[i * 4 + 2] = (thisClass.pixels[i * 4 + 2] - 128) * contrast +
  101.           128 + brightness; // Blue
  102.     }
  103.  
  104.     thisClass.context.clearRect(0, 0, thisClass.canvas.width, thisClass.canvas.height);
  105.     thisClass.context.putImageData(thisClass.imageData, 0, 0);
  106.  
  107.   }
  108.  
  109.   colorFilter(thisClass) {
  110.     console.log(thisClass);
  111.     // console.log(thisClass.brightness);
  112.     // console.log(thisClass.contrast);
  113.     let red = parseInt(thisClass.redColor);
  114.     let green = parseInt(thisClass.greenColor);
  115.     let blue = parseInt(thisClass.blueColor);
  116.     let strength = parseInt(thisClass.intensity);
  117.     for (let i = 0; i < thisClass.numPixels; i++) {
  118.       //Red
  119.       thisClass.pixels[i * 4] =
  120.        (thisClass.pixels[i * 4]) + red / (strength / 100);
  121.        //Green
  122.       thisClass.pixels[i * 4 + 1] =
  123.        (thisClass.pixels[i * 4 + 1]) + green / (strength / 100);
  124.        //Blue
  125.       thisClass.pixels[i * 4 + 2] =
  126.        (thisClass.pixels[i * 4 + 2]) + blue / (strength / 100);
  127.  
  128.     }
  129.  
  130.     thisClass.context.clearRect(0, 0, thisClass.canvas.width, thisClass.canvas.height);
  131.     thisClass.context.putImageData(thisClass.imageData, 0, 0);
  132.  
  133.   }
  134.  
  135.   getExif(img) {
  136.     let latLon: any;
  137.     try {
  138.       EXIF.getData(img, () => {
  139.         console.log(EXIF.getAllTags(img));
  140.         /* console.log(EXIF.getTag(evt.target, 'GPSLatitude'));
  141.         console.log(EXIF.getTag(evt.target, 'GPSLongitude')); */
  142.         try {
  143.           if (EXIF.getTag(img, 'GPSLatitude')) {
  144.  
  145.             latLon.lat = this.degreesToDecimals(
  146.                 EXIF.getTag(img, 'GPSLatitude'));
  147.             latLon.lon = this.degreesToDecimals(
  148.                 EXIF.getTag(img, 'GPSLongitude'));
  149.             return latLon;
  150.           } else {
  151.             return false;
  152.           }
  153.         } catch (e) {
  154.           console.log(e);
  155.         }
  156.       });
  157.     } catch (e) {
  158.       console.log(e);
  159.     }
  160.   }
  161.  
  162.   degreesToDecimals(deg: Array<number>): number {
  163.     /*deg.forEach(d => {
  164.       console.log(d['numerator']);
  165.     });*/
  166.     return deg[0]['numerator'] + (deg[1]['numerator'] / 60) +
  167.         (deg[2]['numerator'] / 100 / 3600);
  168.   }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement