ionic2实战ionic2

ionic2实战-添加拍照功能cordova-plugin-ca

2017-01-04  本文已影响8766人  昵称已被使用_

效果演示

源代码已上传到github

由于ionic版本更新较快,有些写法可能改变来不及更新简书,请以github代码为准
ionic2.0插件的使用方式和ionic3.0已不一样

ionic2实战-添加拍照功能cordova-plugin-camera.gif

安装插件

封装拍照插件

这里是ionic2.0插件的使用方式.ionic3.0使用方式看这里

    /**
     * Created by yanxiaojun617@163.com on 01-03.
     */
    import {Injectable} from '@angular/core';
    import {ToastController, LoadingController} from 'ionic-angular';
    import {Camera} from 'ionic-native';

    @Injectable()
    export class NativeService {
      private toast;
      private loading;

      constructor(private toastCtrl: ToastController, private loadingCtrl: LoadingController) {
      }

      /**
       * 统一调用此方法显示提示信息
       * @param message 信息内容
       * @param duration 显示时长
       */
      showToast = (message: string = '操作完成', duration: number = 2500) => {
        this.toast = this.toastCtrl.create({
          message: message,
          duration: duration,
          position: 'top',
          showCloseButton: true,
          closeButtonText: '关闭'
        });
        this.toast.present();
      };

      /**
       * 关闭信息提示框
       */
      hideToast = () => {
        this.toast.dismissAll()
      };

      /**
       * 统一调用此方法显示loading
       * @param content 显示的内容
       */
      showLoading = (content: string = '') => {
        this.loading = this.loadingCtrl.create({
          content: content
        });
        this.loading.present();
        setTimeout(() => {//最长显示20秒
          this.loading.dismiss();
        }, 20000);
      };

      /**
       * 关闭loading
       */
      hideLoading = () => {
        this.loading.dismissAll()
      };

      /**
       * 使用cordova-plugin-camera获取照片的base64
       * @param options
       * @return {Promise<T>}
       */
      getPicture = (options) => {
        return new Promise((resolve, reject) => {
          Camera.getPicture(Object.assign({
            sourceType: Camera.PictureSourceType.CAMERA,//图片来源,CAMERA:拍照,PHOTOLIBRARY:相册
            destinationType: Camera.DestinationType.DATA_URL,//返回值格式,DATA_URL:base64,FILE_URI:图片路径
            quality: 90,//保存的图像质量,范围为0 - 100
            allowEdit: true,//选择图片前是否允许编辑
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 800,//缩放图像的宽度(像素)
            targetHeight: 800,//缩放图像的高度(像素)
            saveToPhotoAlbum: false,//是否保存到相册
            correctOrientation: true//设置摄像机拍摄的图像是否为正确的方向
          }, options)).then((imageData) => {
            resolve(imageData);
          }, (err) => {
            console.log(err);
            err == 20 ? this.showToast('没有权限,请在设置中开启权限') : reject(err);
          });
        });
      };

      /**
       * 通过图库获取照片
       * @param options
       * @return {Promise<T>}
       */
      getPictureByPhotoLibrary = (options = {}) => {
        return new Promise((resolve) => {
          this.getPicture(Object.assign({
            sourceType: Camera.PictureSourceType.PHOTOLIBRARY
          }, options)).then(imageBase64 => {
            resolve(imageBase64);
          }).catch(err => {
            String(err).indexOf('cancel') != -1 ? this.showToast('取消选择图片', 1500) : this.showToast('获取        照片失败');
          });
        });
      };

      /**
       * 通过拍照获取照片
       * @param options
       * @return {Promise<T>}
       */
      getPictureByCamera = (options = {}) => {
        return new Promise((resolve) => {
          this.getPicture(Object.assign({
            sourceType: Camera.PictureSourceType.CAMERA
          }, options)).then(imageBase64 => {
            resolve(imageBase64);
          }).catch(err => {
            String(err).indexOf('cancel') != -1 ? this.showToast('取消拍照', 1500) : this.showToast('获取照片失败');
          });
        });
      };

    }

使用

我的html页面代码

    <ion-header>
      <ion-toolbar>
        <ion-title>
          设置个人头像
        </ion-title>
        <ion-buttons start>
          <button ion-button (click)="dismiss()">
            <span showWhen="ios">关闭</span>
            <ion-icon name="md-close" showWhen="android,windows,landscape"></ion-icon>
          </button>
        </ion-buttons>
      </ion-toolbar>
    </ion-header>

    <ion-content padding text-center>
      <img [src]="avatarPath" width="100%">
      <div padding-top>
        <button ion-button block color="light" (click)="getPicture(0)">从相册选一张</button>
      </div>
      <div padding-top>
        <button ion-button block color="light" (click)="getPicture(1)">拍一张照片</button>
      </div>
      <div padding-top>
        <button type="button" ion-button block (click)="saveAvatar()">保 存</button>
      </div>
    </ion-content>

我的.ts文件代码

    import {Component} from '@angular/core';
    import {ViewController} from 'ionic-angular';
    import {NativeService} from "../../providers/NativeService";


    @Component({
      selector: 'page-page2',
      templateUrl: 'page2.html'
    })
    export class Page2Page {
      isChange: boolean = false;//头像是否改变标识
      avatarPath: string = './assets/img/qr_code.png';//用户默认头像
      imageBase64: string;//保存头像base64,用于上传

      constructor(private viewCtrl: ViewController,
                  private nativeService: NativeService) {
      }

      getPicture(type) {//1拍照,0从图库选择
        let options = {
          targetWidth: 400,
         targetHeight: 400
        };
        if (type == 1) {
          this.nativeService.getPictureByCamera(options).then(imageBase64 => {
            this.getPictureSuccess(imageBase64);
          });
       } else {
          this.nativeService.getPictureByPhotoLibrary(options).then(imageBase64 => {
            this.getPictureSuccess(imageBase64);
          });
        }
      }

      private getPictureSuccess(imageBase64) {
        this.isChange = true;
        this.imageBase64 = <string>imageBase64;
        this.avatarPath = 'data:image/jpeg;base64,' + imageBase64;
      }

      saveAvatar() {
        if (this.isChange) {
          console.log(this.imageBase64);//这是头像数据.
          this.nativeService.showLoading('正在上传....');
          this.viewCtrl.dismiss({avatarPath: this.avatarPath});//这里可以把头像传出去.
        } else {
          this.dismiss();
        }
      }

      dismiss() {
        this.viewCtrl.dismiss();
      }
    }

最后

最后的最后

 /**
   * 根据图片绝对路径转化为base64字符串
   * @param url 绝对路径
   * @param callback 回调函数
   */
  convertImgToBase64(url, callback) {
    this.getFileContentAsBase64(url, function (base64Image) {
      callback.call(this, base64Image.substring(base64Image.indexOf(';base64,') + 8));
    })
  }

  private getFileContentAsBase64(path, callback) {
    function fail(err) {
      console.log('Cannot found requested file' + err);
    }

    function gotFile(fileEntry) {
      fileEntry.file(function (file) {
        let reader = new FileReader();
        reader.onloadend = function (e) {
          let content = this.result;
          callback(content);
        };
        reader.readAsDataURL(file);
      });
    }

    this.file.resolveLocalFilesystemUrl(path).then(fileEnter => gotFile(fileEnter)).catch(err => fail(err));
    // window['resolveLocalFileSystemURL'](path, gotFile, fail);
  }
 /**
     * base64字节生成图片
     *
     * @param base64字符串
     * @param imgFilePath  生成的图片绝对路径+文件名
     * @return
     */
    public static boolean makePicture(String base64, String imgFilePath) {
        if (base64 == null) {
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(base64);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

上一篇 下一篇

猜你喜欢

热点阅读