0

我用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;
4

1 回答 1

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;
于 2018-12-31T03:22:50.270 回答