简体   繁体   English

保存相机拍摄的图像JPG

[英]Save image JPG captured by the camera

I'm using the code below to open the camera for an Android device. 我正在使用下面的代码打开Android设备的相机。 How do I save a image JPG, captured by this camera? 如何保存本相机拍摄的JPG图片? What code/Button should I add to my programming AS3/AIR? 我应该在AS3 / AIR编程中添加什么代码/按钮?

public class Test_Cam extends Sprite {
private var cam: Camera;
public function Test_Cam() {
cam = Camera.getCamera();
connectCamera();
function connectCamera(): void {
var vid: Video = new Video(cam.width, cam.height);
vid.x = 100;
vid.y = 200;
vid.attachCamera(cam);
addChild(vid);
}
}
}

:( I could not implement my code: Saving and masking webcam still AS3 AIR IOS :(我无法实现我的代码: 保存和掩盖摄像头仍为AS3 AIR IOS

Just apply it as your root class and give required permissions 只需将其用作您的根类并授予所需的权限

package {
    import flash.desktop.NativeApplication;
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.MediaEvent;
    import flash.media.CameraUI;
    import flash.media.MediaPromise;
    import flash.media.MediaType;
    import flash.display.DisplayObject;
    import flash.filesystem.*;
    import flash.events.Event;
    import flash.utils.ByteArray;
    import com.adobe.images.PNGEncoder;


    public class CameraUISaveImage extends MovieClip {

        private var deviceCameraApp: CameraUI = new CameraUI();
        private var imageLoader: Loader;
        private var docsDir: File = File.documentsDirectory;

        public function CameraUISaveImage() {
            this.stage.align = StageAlign.TOP_LEFT;
            this.stage.scaleMode = StageScaleMode.NO_SCALE;

            if (CameraUI.isSupported) {
                trace("Initializing camera...");

                deviceCameraApp.addEventListener(MediaEvent.COMPLETE, imageCaptured);
                deviceCameraApp.addEventListener(Event.CANCEL, captureCanceled);
                deviceCameraApp.addEventListener(ErrorEvent.ERROR, cameraError);
                deviceCameraApp.launch(MediaType.IMAGE);
            } else {
                trace("Camera interface is not supported.");
            }
        }

        private function imageCaptured(event: MediaEvent): void {
            trace("Media captured...");

            var imagePromise: MediaPromise = event.data;

            if (imagePromise.isAsync) {
                trace("Asynchronous media promise.");
                imageLoader = new Loader();
                imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, asyncImageLoaded);
                imageLoader.addEventListener(IOErrorEvent.IO_ERROR, cameraError);

                imageLoader.loadFilePromise(imagePromise);
            } else {
                trace("Synchronous media promise.");
                imageLoader.loadFilePromise(imagePromise);
                saveMedia(imageLoader);
            }
        }

        private function captureCanceled(event: Event): void {
            trace("Media capture canceled.");
            NativeApplication.nativeApplication.exit();
        }

        private function asyncImageLoaded(event: Event): void {
            trace("Media loaded in memory.");
            saveMedia(imageLoader);
        }

        private function saveMedia(loader: Loader): void {
            try {
                docsDir = File.applicationDirectory
                docsDir.browseForSave("Save As");
                docsDir.addEventListener(Event.SELECT, saveData);
            } catch (error: Error) {
                trace("Failed:", error.message);
            }
        }


        function saveData(event: Event): void {
            var newFile: File = event.target as File;
            if (!newFile.exists) // remove this 'if' if overwrite is OK.
            {
                var stream: FileStream = new FileStream();
                stream.open(newFile, FileMode.WRITE);
                stream.writeBytes(imageLoader.loaderInfo.bytes);
                stream.close();
            } else trace('Selected path already exists.');
        }

        private function cameraError(error: ErrorEvent): void {
            trace("Error:" + error.text);
            NativeApplication.nativeApplication.exit();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM