IONIC拍照及图片上传(自定义图片名称和图片保存到指定位置)

详情请参考官网:https://ionicframework.com/docs/native/camera/

1.添加插件

$ ionic cordova plugin add cordova-plugin-camera
$ npm install --save @ionic-native/camera

 2.拍照和从相册获取图片只有一个参数的区别

 strFileName: string = ""; 

 //拍照或者从图库获取图片
  getPictureWay(numPhotoType) {
    const options: CameraOptions = {
      quality: 50,
      saveToPhotoAlbum: false,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      allowEdit: false,
      correctOrientation: false
    }

    //判断是拍照还是从图库中获取
    if (numPhotoType === 1) {
      //为 1 才从图库获取
      options.sourceType = this.camera.PictureSourceType.PHOTOLIBRARY
    } else {
      //其它情况默认从相机获取
      options.sourceType = this.camera.PictureSourceType.CAMERA
    }

    let loading = this.loadingCtrl.create({
      content: this.translate.instant('pdfloading')
    });
    loading.present();

    this.camera.getPicture(options).then((imageData) => {

      //自定义图片名称

      this.showAlert();

      //imageData为拍照或从图库选择的图片的文件流,格式为Base64
      //
      //      连接服务的方法(连接服务方法:略)
      //       
      //     参数:strBatchID 
      //     说明:上传进度
      //     参数:strFileName 
      //     说明:图片名称
      //     参数:imageData
      //     说明:将这个读取到的文件流当参数上传到服务即可
      
     loading.dismiss();          
    }, (err) => {
      loading.dismiss();   
    });
  }

 //提示框提示信息
  showAlert() {
    let alert = this.alertCtrl.create({
      title: "提示",
      input:this.strFileName ,
      buttons: [{
        text: this.translate.instant('ok'),
        handler: () => {
          
        }
      },{
        text: this.translate.instant('cancel'),
        handler: () => {
          
        }
      }]
    });
    alert.present()
  }

由于之前的疏忽,忘记加上服务代码了,下面补上

      由于Ionic拍照得到是一个Base64位的文件流,所以上传到服务后需要在服务代码内进行一些处理(下面服务方法是使用VB编写的)

Public Function UploadByBytes(strBatchID As String, strFileName As String, strFileBytes As String) As Boolean   
           
            Dim bolUploadResult As Boolean = False       		
            Try
			  
                Dim image As System.Drawing.Image = Nothing
                Dim strFileDirectory As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "/../Web", UPLOAD_FILE_PATH)

                If Not Directory.Exists(strFileDirectory) Then
                    Directory.CreateDirectory(strFileDirectory)
                End If
                        
                '需删除字符串中"data:image/png;base64,"或"data:image/jpeg;base64,",否则在使用Base64ToByteArray时,会返回nothing				
                strFileBytes = strFileBytes.Replace("data:image/png;base64,", String.Empty)
                strFileBytes = strFileBytes.Replace("data:image/jpeg;base64,", String.Empty)
				
                '图片转为64位编码时,"+"变成了" ",需还原
                strFileBytes = strFileBytes.Replace(" ", "+")
				
                ' base64编码的字符串的长度始终是4的倍数。如果它不是4的倍数,=则会附加字符直到它为止
                Dim mod4 As Integer = strFileBytes.Length Mod 4
                If (mod4 > 0) Then
                    For i = 1 To (4 - mod4)
                        strFileBytes += "="
                    Next
                End If

                Dim objBytes As Byte() = Common.Utility.Util.Base64ToByteArray(strFileBytes.Trim)
                Dim objStream As Stream = New MemoryStream(objBytes)

                'Read Stream
                Using objMemoryStream As New MemoryStream
                    Dim objBuffer(BUFFER_LENGTH) As Byte

                    '进度设为0
                    UpdateProgress(strBatchID, 0)

                    '将流读到一个MemoryStream 中
                    Dim intReadLength As Integer = 0

                    '读取图片
                    While ReadStream(intReadLength, objBuffer, BUFFER_LENGTH, objStream) > 0
                        objMemoryStream.Write(objBuffer, 0, intReadLength)
                        Thread.Sleep(1)
                        UpdateProgress(strBatchID, objMemoryStream.Length)
                    End While

                    '保存图片到数据库
                    bolUploadResult = SaveFileToDatabase(objMemoryStream, strFileName, strBatchID)

                    '读取成图片
                    image = System.Drawing.Image.FromStream(objMemoryStream, True)
                    '将图片保存到指定路径
                    image.Save(strFileDirectory + "/" + strFileName.ToString + ".jpg")
                End Using
              
            Catch ex As Exception
                Throw ex
            End Try
         
	    return bolUploadResult
        End Function

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值