我是编码新手,尤其是 ActionScript 3.0 和 Adobe Animate。
我正在尝试为 android 智能手机开发一个简单的相机应用程序来拍摄和保存照片。
主要问题是手机屏幕上“照片容器”的正确横向-纵向方向。因此,在旋转智能手机(纵向-横向)时,“照片容器”会从智能手机屏幕上消失。显然 x 和 y 属性不在屏幕上。我尝试了很多不同的方法,但我错过了一些东西:(
是否有一个明确的解决方案可以让我研究以了解我做错了什么?例如,用于“逆向工程”目的的示例代码可以帮助我了解相机方向的工作原理?
(另一个大问题是“权限”,但无论如何,在我了解方向如何工作之前,我不敢从这个开始......)
我非常沮丧和失望,我不知道该怎么办,因为网络上似乎没有很多关于 ActionScript 3.0 的资源,当我发现某些东西时,它真的已经过时了!
非常感谢您提供的各种帮助或指导。
PS:看起来在 AIR 中本来就有一个错误,因为图像的默认方向在屏幕上,而在纵向/侧向上位置时逆时针旋转 90 度(!)
到目前为止,这是我的代码:
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.StageAspectRatio;
import flash.system.Capabilities;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.MovieClip;
import flash.events.StageOrientationEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.events.GestureEvent;
import flash.events.MouseEvent;
import flash.media.Camera;
import flash.events.TouchEvent;
import flash.display.BitmapData;
import flash.media.CameraRoll;
import flash.media.Video;
import flash.sensors.Geolocation;
import flash.events.StatusEvent;
import flash.net.URLRequest;
//basic UI
var headerr:MovieClip;
var footerr:MovieClip;
var backgroundd:MovieClip;
var consolee:TextField;
var consoleFormat:TextFormat = new TextFormat;
var photoContainer:MovieClip;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//////////////////// START //////////////////////////
var camera:Camera;
var video:Video;
var cameraBitmapData:BitmapData;
var cameraBitmap:Bitmap;
function setCamera():void {
camera = Camera.getCamera();
camera.setMode(800, 600, 24); // background.width, background.height
video = new Video(800, 600); // background.width, background.height
cameraBitmapData = new BitmapData(800, 600, false, 0xFFFFFF);
cameraBitmap = new Bitmap(cameraBitmapData);
video.attachCamera(camera);
photoContainer.addChild(cameraBitmap);
stage.removeEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(Event.ENTER_FRAME, loop);
}
function loop(e:Event):void {
if (video && cameraBitmapData) {
cameraBitmapData.draw(video, null, null, null, null, true)};
}
//-----------------------------------------------------
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
//tap anywhere on STAGE to shoot & save an image
stage.addEventListener(TouchEvent.TOUCH_TAP, saveImage);
function saveImage(event:TouchEvent):void {
var capture = new BitmapData(background.width, background.height);
capture.draw(video);
var cameraRoll = new CameraRoll();
cameraRoll.addBitmapData(capture);
}
///////////////////// END///////////////////////////////////////
this.stage.addEventListener(Event.RESIZE, adjustAppLayout);
function adjustAppLayout(e:Event):void {
header.width = stage.stageWidth;
header.height = stage.stageHeight*0.10;
footer.width = stage.stageWidth;
footer.height = stage.stageHeight*0.02;
footer.y = stage.stageHeight-footer.height;
background.width = stage.stageWidth;
background.height = stage.stageHeight-footer.height-header.height;
background.y = header.height;
consoleFormat.size = stage.stageHeight*0.03;
console.defaultTextFormat = consoleFormat;
console.width = stage.stageWidth;
console.height = background.height - 20;
console.y = header.height + 10;
photoContainer.scaleX = 1;
photoContainer.scaleY = 1;
photoContainer.y = header.height;
photoContainer.x = 0; //<--------- header.width
setCamera();
}
stage.autoOrients = true;
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, onChange);
function onChange(event:StageOrientationEvent):void {
console.appendText(event.afterOrientation + "\n");
/*
if (event.afterOrientation==StageOrientation.DEFAULT) {
photoContainer.rotation=90;
photoContainer.x=800;
photoContainer.y=0;
} else if (event.afterOrientation==StageOrientation.ROTATED_RIGHT) {
photoContainer.rotation=0;
photoContainer.x=0;
photoContainer.y=0;
} else if (event.afterOrientation==StageOrientation.ROTATED_LEFT) {
photoContainer.rotation=-90;
photoContainer.x=0;
photoContainer.y=600;
}
*/
switch (event.afterOrientation) {
case StageOrientation.DEFAULT:
// re-orient display objects based on the default/correct-side-up orientation.
photoContainer.rotation = 90;
//photoContainer.scaleX = 1;
//photoContainer.scaleY = 1;
photoContainer.x = header.width;
photoContainer.y = header.height;
break;
case StageOrientation.ROTATED_RIGHT:
// Re-orient display objects based on right-hand orientation.
photoContainer.rotation = 360;
//photoContainer.scaleX = 1;
//photoContainer.scaleY = 1;
photoContainer.x = 0;
photoContainer.y = console.height;
break;
case StageOrientation.ROTATED_LEFT:
// Re-orient display objects based on left-hand orientation.
photoContainer.rotation = 0;
//photoContainer.scaleX = 1;
//photoContainer.scaleY = 1;
photoContainer.x = 0;
photoContainer.y = console.height;
break;
/*
case StageOrientation.UPSIDE_DOWN:
// Re-orient display objects based on upside-down orientation.
photoContainer.rotation = 360;
photoContainer.scaleX = 1;
photoContainer.scaleY = 1;
photoContainer.x = console.width;
photoContainer.y = console.height + 10;
break;
*/
}
}
接下来是更新的代码(包括相机的快门按钮、带有相关按钮的地理定位功能以及屏幕上控制台上传感器的一些反馈),关于 photoContainer 的结果稍微好一点,但仍远非完美智能手机屏幕上的旋转/方向:
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.StageAspectRatio;
import flash.system.Capabilities;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.MovieClip;
import flash.events.StageOrientationEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.events.GestureEvent;
import flash.events.MouseEvent;
import flash.media.Camera;
import flash.events.TouchEvent;
import flash.display.BitmapData;
import flash.media.CameraRoll;
import flash.media.Video;
import flash.sensors.Geolocation;
import flash.events.StatusEvent;
import flash.net.URLRequest;
var headerr:MovieClip;
var footerr:MovieClip;
var backgroundd:MovieClip;
var consolee:TextField;
var consoleFormat:TextFormat = new TextFormat;
//=====================================================
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//////////////////// START //////////////////////////
var camera:Camera;
var video:Video;
var cameraBitmapData:BitmapData;
var cameraBitmap:Bitmap;
function setCamera():void {
camera = Camera.getCamera();
camera.setMode(800,600, 24);
video = new Video(800, 600);
cameraBitmapData = new BitmapData(800, 600, false, 0xFFFFFF);
cameraBitmap = new Bitmap(cameraBitmapData);
video.attachCamera(camera);
photoContainer.addChild(cameraBitmap);
stage.removeEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(Event.ENTER_FRAME, loop);
}
function loop(e:Event):void {
if (video && cameraBitmapData) {
cameraBitmapData.draw(video, null, null, null, null, true)};
}
//-----------------------------------------------------
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
////tapping on shutter button
shutter_btn.addEventListener(TouchEvent.TOUCH_BEGIN, interaction_feedback);
//dim button
function interaction_feedback(event:TouchEvent):void {
shutter_btn.alpha -=0.5;
}
//shoot & save an image
shutter_btn.addEventListener(TouchEvent.TOUCH_TAP, saveImage);
function saveImage(event:TouchEvent):void {
var capture = new BitmapData(800, 600); //background.width, background.height same?
capture.draw(video);
var cameraRoll = new CameraRoll();
cameraRoll.addBitmapData(capture);
}
//light-back button
shutter_btn.addEventListener(TouchEvent.TOUCH_END, interaction_feedbackEnd);
function interaction_feedbackEnd(event:TouchEvent):void {
shutter_btn.alpha = 1;
}
//---------------------------------------------------------
///// GPS
if(Geolocation.isSupported){
var geoloc = new Geolocation();
geoloc.addEventListener(GeolocationEvent.UPDATE, onUpdate);
}
function onUpdate(e:GeolocationEvent):void {
console.appendText("latitude: " + e.latitude + "\n");
console.appendText("longitude: " + e.longitude + "\n");
console.appendText("altitude: " + e.altitude + "\n");
console.appendText("horizontal accuracy: " + e.horizontalAccuracy + "\n");
console.appendText("vertical accuracy: " + e.verticalAccuracy + "\n");
console.appendText("heading: " + e.heading + "\n");
console.appendText("speed: " + e.speed + "\n");
console.appendText("timestamp: " + e.timestamp + "\n");
geoloc.removeEventListener(GeolocationEvent.UPDATE, onUpdate);
var longitude = e.longitude;
var latitude = e.latitude;
nav_btn.addEventListener(MouseEvent.CLICK, navClick);
var navigate:URLRequest = new URLRequest( "http://maps.google.com/?q=" + String(latitude) + ", " + String(longitude) );
function navClick(event:MouseEvent):void {
navigateToURL(navigate);
}
}
///////////////////// END///////////////////////////////////////
this.stage.addEventListener(Event.RESIZE, adjustAppLayout);
function adjustAppLayout(e:Event):void {
header.width = stage.stageWidth;
header.height = stage.stageHeight*0.10;
footer.width = stage.stageWidth;
footer.height = stage.stageHeight*0.02;
footer.y = stage.stageHeight-footer.height;
background.width = stage.stageWidth;
background.height = stage.stageHeight-footer.height-header.height;
background.y = header.height;
consoleFormat.size = stage.stageHeight*0.03;
console.defaultTextFormat = consoleFormat;
console.width = stage.stageWidth;
console.height = background.height - 20;
console.y = header.height + 10;
shutter_btn.x = stage.stageWidth / 2;
shutter_btn.y = stage.stageHeight-footer.height-75;
nav_btn.x = stage.stageWidth / 2;
nav_btn.y = stage.stageHeight-background.height-50;
photoContainer.scaleX = 1;
photoContainer.scaleY = 1;
photoContainer.y = header.height;
}
stage.autoOrients = true;
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, onChange);
setCamera();
function onChange(event:StageOrientationEvent):void {
console.appendText(event.afterOrientation + "\n");
if (event.afterOrientation==StageOrientation.DEFAULT) {
photoContainer.rotation=90;
photoContainer.x = photoContainer.width;
photoContainer.y = photoContainer.height;
} else if (event.afterOrientation==StageOrientation.ROTATED_RIGHT) {
photoContainer.rotation=0;
photoContainer.x=0;
photoContainer.y=0;
} else if (event.afterOrientation==StageOrientation.ROTATED_LEFT) {
photoContainer.rotation=0;
photoContainer.x=0;
photoContainer.y=0;
}
}
//myChecks
console.appendText("screenDPI: " + Capabilities.screenDPI + "\n");
console.appendText("screenResolutionX: " + Capabilities.screenResolutionX + "\n");
console.appendText("screenResolutionY: " + Capabilities.screenResolutionY + "\n");
console.appendText("Camera: " + Camera.isSupported + "\n");
console.appendText("CameraUI: " + CameraUI.isSupported + "\n");
console.appendText("Microphone: " + Microphone.isSupported + "\n");
欢迎头脑风暴!谢谢