import {CameraPreview, CameraPreviewOptions,} from '@ionic-native/camera-preview';
import {ToastController} from "ionic-angular";
import {Base64ToGallery} from '@ionic-native/base64-to-gallery';
private cameraPreviewOpts: CameraPreviewOptions = {
x: 0,
y: 0,
width: window.screen.width,
height: window.screen.height,
camera: 'rear',
tapPhoto: true,
previewDrag: true,
toBack: true,
alpha: 1
};
constructor(
public navCtrl: NavController,
public toastCtrl: ToastController,
public navParams: NavParams,
private camera: CameraPreview,
public platform: Platform,
private base64ToGallery: Base64ToGallery
)
{
var browser = this.platform.is('core');
if (!browser) {this.camera.startCamera(this.cameraPreviewOpts);}
}
takePicture() {
this.camera.takePicture({}).then((base64Data) => {
this.base64ToGallery.base64ToGallery(base64Data).then(
res => alert('Saved image to gallery ' + JSON.stringify(res)),
err => alert('Error saving image to gallery ' + JSON.stringify(err))
);
}, (err) => {
alert(JSON.stringify(err));
});
}
。 当我尝试在库中保存保存图像时,我有这样的错误:{“_ _ zone_symbol__currentTask”:{“type”:“microTask”,“state”:“notScheduled”,“source”:“Promise.then”,“zone”: “角”, “cancelFn”:NULL, “runCount”:0}}
答案 0 :(得分:1)
1)从npmjs cordova plugin add cordova-plugin-camera-preview
2)将此代码放在@Component declare let CameraPreview;
3)不要忘记允许存储访问并在应用程序参数中或使用androidPermission写入存储
3)有我的代码可以正常工作!
takePic(){
const options = {
x: 0,
y: 0,
width: window.screen.width,
height: window.screen.height,
camera: CameraPreview.CAMERA_DIRECTION.FRONT, //or BACK
toBack: true,
tapPhoto: false,
tapFocus: false,
previewDrag: false,
disableExifHeaderStripping: true
};
CameraPreview.startCamera(options);
let base64option : Base64ToGalleryOptions = {
prefix: 'img',
mediaScanner: false
};
CameraPreview.takePicture({}, base64PictureData => {
let todecode = atob(base64PictureData);
this.base64ToGallery.base64ToGallery(btoa(todecode), base64option).then(
res => alert('Saved image to gallery '+ JSON.stringify(res)),
err => alert('Error saving image to gallery ' + JSON.stringify(err))
);
}, error =>{
alert(JSON.stringify(error));
});
}
您必须在后台启动相机才能拍照toBack: true
,并用base64PictureData
和atob
对btoa
进行解码和编码
您的图片现在在内部存储器/图片中!
答案 1 :(得分:0)
我想如果你添加
destinationType:2
图像将保存到本地,您将获得本地路径链接。