nativescript-camera-plus使用屏幕分辨率,而不是本机照相机的宽度和高度

时间:2018-12-30 01:01:34

标签: nativescript angular2-nativescript nativescript-angular nativescript-plugin

我使用nativescript-camera-plus NativeScript插件覆盖了一些UI元素,效果很好,但是在拍摄照片时,jpg的分辨率与屏幕相同,因此我需要使用相机的分辨率。 我的相机的分辨率为4160x3120,但生成的jpg为1280x720。 我想以相机传感器的最大分辨率保存jpg。

我使用以下代码:

<Page class="page" navigatingTo="onNavigatingTo" loaded="onLoaded" 
    xmlns="http://schemas.nativescript.org/tns.xsd" 
    xmlns:Cam="@nstudio/nativescript-camera-plus" actionBarHidden="true">

    <!-- <Button icon="res://menu" tap="onDrawerButtonTap"></Button> -->

    <GridLayout class="page-content">
        <Cam:CameraPlus id="camera" width="3000" height="2000" confirmPhotos="false" saveToGallery="true" showCaptureIcon="false" showGalleryIcon="false" showToggleIcon="false" showFlashIcon="false" loaded="camLoaded" debug="false">
        </Cam:CameraPlus>
        <Image id="testImagePickResult" src="{{ testImage }}" stretch="aspectFit" />
        <Image src="res://frame1" textAlignment="center" stretch="aspectFit"></Image>
        <Image src="res://camera" textAlignment="center" verticalAlignment="bottom" height="50" tap="takePicFromCam"></Image>
    </GridLayout>
</Page>

const app = require("tns-core-modules/application");
const CameraPlus = require('@nstudio/nativescript-camera-plus').CameraPlus;
const observableModule = require("tns-core-modules/data/observable");
const ImageAsset = require('tns-core-modules/image-asset');
const imageSourceModule = require("tns-core-modules/image-source");
//const screen = require('tns-core-modules/platform');
const topmost = require('tns-core-modules/ui/frame');
const Image = require('tns-core-modules/ui/image');
const Page = require('tns-core-modules/ui/page');
const platformModule = require("tns-core-modules/platform");
const statusBar = require("nativescript-status-bar");
const frameModule = require("tns-core-modules/ui/frame");
const fileSystemModule = require("tns-core-modules/file-system");
var page;

const viewModel = observableModule.fromObject({
    cameraHeight: 200,
    testImage: null
});

function onNavigatingTo(args) {
    console.log(`onNavigatingTo`);
    page = args.object;

    statusBar.hide();

    //console.log("onNavigatingTo " + platformModule.screen.mainScreen.heightDIPs);
    viewModel.cameraHeight = platformModule.screen.mainScreen.heightDIPs;

    page.bindingContext = viewModel;
}
exports.onNavigatingTo = onNavigatingTo;

function onLoaded(args) {
    console.log(`onLoaded`);

    let cam = page.getViewById("camera");

    cam.on(CameraPlus.photoCapturedEvent, (event) => {
        photoCapturedEvent(event);
    });
}
exports.onLoaded = onLoaded /*  */ ;

function camLoaded(args) {
    console.log(`cam loaded event`);
    const cam = args.object;

    try {
        const flashMode = args.object.getFlashMode();
        console.log(`flashMode in loaded event = ${flashMode}`);
    }
    catch (e) {
        console.log(e);
    }
}
exports.camLoaded = camLoaded;

function takePicFromCam(eventData) {
    let page = frameModule.topmost().currentPage;
    let cam = page.getViewById("camera");

    cam.takePicture({ saveToGallery: true, confirm: false, keepAspectRatio: false, width: 4160, height: 3120 });
};
exports.takePicFromCam = takePicFromCam;

function photoCapturedEvent(e) {
    console.log('photo captured !');

    console.log(e.data.android); // the path of the captured file, eg. "/storage/emulated/0/Android/data/COM.YOURAPP.NAME/files/IMG_1535045496885.jpg"

    try {
        const imageFromLocalFile = imageSourceModule.fromFile(e.data.android);
        console.log(imageFromLocalFile);

        let filePhoto = fileSystemModule.File.fromPath(e.data.android);
    }
    catch (err) {
        console.log("Error: " + err);
    }
};
exports.photoCapturedEvent = photoCapturedEvent;

1 个答案:

答案 0 :(得分:0)

就像@Manoj一样,此问题的解决方法是:

function takePicFromCam(eventData) {
    let page = frameModule.topmost().currentPage;
    let cam = page.getViewById("camera");

    // Android only
    const params = cam.camera.getParameters();
    params.setPictureSize(1920, 1440);
    cam.camera.setParameters(params);

    cam.takePicture({ saveToGallery: true, confirm: false, keepAspectRatio: true, width: 1920, height: 1440 });
};

exports.takePicFromCam = takePicFromCam;