Apache flex StageWebView-如何防止通过drawViewPortToBitmapData获得的位图拉伸?

Apache flex StageWebView-如何防止通过drawViewPortToBitmapData获得的位图拉伸?,apache-flex,Apache Flex,我试图将使用StageWebView显示的页面显示为位图。根据文档,我们需要使用DrawViewPortToBitMapData。在显示位图时 ui组件,图像正在拉伸。我怎样才能防止呢 bitmapData = new BitmapData(webView.viewPort.width, webView.viewPort.height, false, 0x000000 ); webView.drawViewPortToBitmapData(bitmapData); webViewBmp = ne

我试图将使用
StageWebView
显示的页面显示为位图。根据文档,我们需要使用
DrawViewPortToBitMapData
。在显示位图时
ui组件
,图像正在拉伸。我怎样才能防止呢

bitmapData = new BitmapData(webView.viewPort.width, webView.viewPort.height, false,
0x000000 );
webView.drawViewPortToBitmapData(bitmapData);
webViewBmp = new Bitmap(bitmapData); 
webView.stage = null;

uiComponent = new UIComponent;
uiComponent.width=webView.viewPort.width;
uiComponent.height=webView.viewPort.height;
uiComponent.addChild(webViewBmp); 

我认为您不需要对uicomponent应用宽度/高度。只要添加位图就可以了。您所做的似乎是拉伸uicomponent,即使您已经以正确的大小添加了位图

您应该在uiComponet上将scale设置为1。如果您设置applicationDPI(假设为160dpi),Flex会根据设备DPI自动缩放uiComponent,将scaleX和scaleY设置为runtimeDPI/160

试着这样做:

uiComponent.scaleX=160/parentApplication.runtimeDPI; uiComponent.scaleY=160/parentApplication.runtimeDPI

因此scaleX和scaleY以:


runtimeDPI/160(由flex完成)*160/runtimeDPI=1:)

我知道这很旧,但对我来说很有用:

        var r:Rectangle = wView.viewPort;
        var bd:BitmapData = new BitmapData(r.width, r.height);
        wView.drawViewPortToBitmapData(bd);
        var bmp:Bitmap = new Bitmap(bd);
        bmp.width = bd.width * (Capabilities.screenResolutionX / 320);
        bmp.height = bd.height * (Capabilities.screenResolutionY / 480);

希望有帮助

使用uiComponent.unscaledWidth和unscaledHeight。这是我的工作部件,修改过的FlexCapactor代码-原版:

请看快照:

    snapshotBitmapData = new BitmapData(_webView.viewPort.width, _webView.viewPort.height);
    webView.drawViewPortToBitmapData(snapshotBitmapData);
    webViewBitmap = new Bitmap(snapshotBitmapData);
    webViewBitmap.width = unscaledWidth;
    webViewBitmap.height = unscaledHeight;
    addChild(webViewBitmap);
    hideWebView(); 
完整代码:

package ctrls {

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.KeyboardEvent;
import flash.events.LocationChangeEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.StageWebView;
import flash.ui.Keyboard;

import mx.core.FlexGlobals;
import mx.core.UIComponent;

/** @copy flash.media.StageWebView#ErrorEvent.ERROR */
[Event(name="error", type="flash.events.ErrorEvent")]
/** @copy flash.media.StageWebView#Event.COMPLETE */
[Event(name="complete", type="flash.events.Event")]
/** @copy flash.media.StageWebView#LocationChangeEvent.LOCATION_CHANGING */
[Event(name="locationChanging", type="flash.events.LocationChangeEvent")]
/** @copy flash.media.StageWebView#LocationChangeEvent.LOCATION_CHANGE */
[Event(name="locationChange", type="flash.events.LocationChangeEvent")]

/**
 * This class wraps the standard StageWebView with a UIComponent.
 * This allows it to be sized and positioned in the same way
 * any UIComponent would. <br/><br/>
 *
 * The StageWebView class documentation follows:<br/>
 * @copy flash.media.StageWebView
 * */
public class WebView extends UIComponent {

//==================================================================================================

protected var _webView:StageWebView;
private var _source:String;
private var _visibleChanged:Boolean;
private var _sourceChanged:Boolean;

/** @copy flash.media.StageWebView#viewPort */
public function get viewPort():Rectangle { return _webView ? _webView.viewPort : null; }
/** @copy flash.media.StageWebView#dispose() */
public function dispose():void { hideWebView(true); }
/** @copy flash.media.StageWebView#assignFocus() */
public function assignFocus(direction:String = "none"):void { webView.assignFocus(direction); }
/** @copy flash.media.StageWebView#drawViewPortToBitmapData() */
public function drawViewPortToBitmapData(bitmap:BitmapData):void { webView.drawViewPortToBitmapData(bitmap); }
/** @copy flash.media.StageWebView#title */
public function get title():String { return _webView ? _webView.title : null; }

/** @copy flash.media.StageWebView#isHistoryBackEnabled() */
public function get isHistoryBackEnabled():Boolean { return _webView ? _webView.isHistoryBackEnabled : false; }
/** @copy flash.media.StageWebView#isHistoryForwardEnabled() */
public function get isHistoryForwardEnabled():Boolean { return _webView ? _webView.isHistoryForwardEnabled : false; }
/** @copy flash.media.StageWebView#historyBack() */
public function historyBack():void { if(_webView) _webView.historyBack(); }
/** @copy flash.media.StageWebView#historyForward() */
public function historyForward():void { if(_webView) _webView.historyForward(); }
/** @copy flash.media.StageWebView#reload() */
public function reload():void { webView.reload(); }
/** @copy flash.media.StageWebView#stop() */
public function stop():void { if(_webView) webView.stop(); }
/** Load the URL passed in or load the URL specified in the source property
 *  @see flash.media.StageWebView#loadURL() */
public function load(url:String = null):void { webView.loadURL(url ? _source = url : source); }

override public function set visible(value:Boolean):void {
    super.visible = value;
    _visibleChanged = true;
    invalidateProperties();
    invalidateSize();
}
/** @private */
public function get source():String { return _source; }
/**
 * Source URL for stage web view.
 * @see flash.media.StageWebView#loadURL()
 * */
[Bindable]
public function set source(value:String):void {
    _source = value;
    _sourceChanged = true;
    invalidateProperties();
    invalidateSize();
}

//==================================================================================================

/**
 *  Wrapper for StageWebView
 *
 *  @copy flash.media.StageWebView
 */
public function WebView() {
    addEventListener(Event.ADDED_TO_STAGE, addedToStage);
    addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
    focusEnabled = false;
}

/** @private */
public function get webView():StageWebView {
    if(!_webView) webView = new StageWebView();
    return _webView; }
/** @copy flash.media.StageWebView */
public function set webView(value:StageWebView):void {
    if(_webView == value) return;
    if(_webView) {
        _webView.removeEventListener(Event.COMPLETE, completeHandler);
        _webView.removeEventListener(ErrorEvent.ERROR, errorHandler);
        _webView.removeEventListener(FocusEvent.FOCUS_IN, focusInViewHandler);
        _webView.removeEventListener(FocusEvent.FOCUS_OUT, focusOutViewHandler);
        _webView.removeEventListener(LocationChangeEvent.LOCATION_CHANGING, locationChangingHandler);
        _webView.removeEventListener(LocationChangeEvent.LOCATION_CHANGE, locationChangeHandler); }
    _webView = value;
    _webView.addEventListener(Event.COMPLETE, completeHandler);
    _webView.addEventListener(ErrorEvent.ERROR, errorHandler);
    _webView.addEventListener(FocusEvent.FOCUS_IN, focusInViewHandler);
    _webView.addEventListener(FocusEvent.FOCUS_OUT, focusOutViewHandler);
    _webView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, locationChangingHandler);
    _webView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, locationChangeHandler);
    _webView.stage = visible ? stage : null;
    _visibleChanged = false;
    if(source) _webView.loadURL(_source);
    _sourceChanged = false;
    invalidateDisplayList();
}
/** Hides the web view @see flash.media.StageWebView#stage */
public function hideWebView(destroy:Boolean = false):void {
    if(_webView == null) return;
    _webView.stage = null;
    if(!destroy) return;
    _webView.viewPort = null;
    _webView.dispose();
    _webView = null;
}
/** Displays the web view @see flash.media.StageWebView#stage */
public function showWebView():void {
    if(_webView != null) {
        webView.stage = stage;
        return; }
    _visibleChanged = true;
    invalidateProperties();
    invalidateSize();
    invalidateDisplayList(); }
/** @copy mx.core.UIComponent#commitProperties() */
override protected function commitProperties():void {
    super.commitProperties();
    if(_visibleChanged) {
        webView.stage =  visible ? stage : null;
        _visibleChanged = false; }
    if(_sourceChanged) {
        webView.loadURL(source);
        _sourceChanged = false; }}

//==================================================================================================

/** Flag indicating if a snapshot is being shown */
[Bindable]
public var isSnapshotVisible:Boolean;
/**
 * When calling takeSnapshot or setting snapshotMode to true this 
 * property will contain the bitmap data of the view port. 
 * */
public var snapshotBitmapData:BitmapData;
/**
 * When calling takeSnapshot or setting snapshotMode a snapshot of 
 * the Stage Web View is taken and added to the stage. This is a
 * reference to the displayed bitmap. 
 * */
public var webViewBitmap:Bitmap;
/**
 * @private
 * */
public function get snapshotMode():Boolean {
    return isSnapshotVisible;
}
/**
 * When set to true hides the stage web view and displays a non-interactive 
 * snapshot of the Stage Web View when the property was set to true.  
 * */
public function set snapshotMode(value:Boolean):void {
    value ? takeSnapshot() : removeSnapshot();
}
/**
 * Creates a snapshot of the Stage Web View at the point of this call 
 * and displays that instead of the actual Stage Web View. 
 * Use removeSnapshot to dispose of the snapshot and show the web contents again. 
 * 
 * @see isSnapshotVisible
 * @see flash.media.StageWebView#drawViewPortToBitmapData()
 * */
public function takeSnapshot():BitmapData {
    destroySnapshot();
    snapshotBitmapData = new BitmapData(_webView.viewPort.width, _webView.viewPort.height);
    webView.drawViewPortToBitmapData(snapshotBitmapData);
    webViewBitmap = new Bitmap(snapshotBitmapData);
    webViewBitmap.width = unscaledWidth;
    webViewBitmap.height = unscaledHeight;
    addChild(webViewBitmap);
    hideWebView(); 
    isSnapshotVisible = true;

    return snapshotBitmapData;
}
/**
 * Removes the bitmap snapshot of the Stage Web View from the display list 
 * and displays the actual Stage Web View.
 * @copy flash.media.StageWebView#drawViewPortToBitmapData()
 * */
public function removeSnapshot():void {
    destroySnapshot();
    showWebView();
}
/**
 * Removes the web view snapshot from the display list and disposes of the 
 * bitmap data
 * */
private function destroySnapshot():void {
    if (webViewBitmap) {
        if (webViewBitmap.parent) removeChild(webViewBitmap);
        if (webViewBitmap.bitmapData) webViewBitmap.bitmapData.dispose();
        webViewBitmap = null;
    }
    if (snapshotBitmapData) {
        snapshotBitmapData.dispose();
        snapshotBitmapData = null;
    }
    isSnapshotVisible = false;
}

//==================================================================================================

/**
 * If enabled adds support for the back and search keys.
 * Back key navigates back in web view history and search navigates forward.
 * */
public var navigationSupport:Boolean;
/**
 * If enabled adds a keyboard listener to the stage.
 * This handles when the component does not have focus
 * */
public var addKeyHandlerToStage:Boolean;
/**
 * KeyCode to use when navigation support is enabled.
 * Default is Keyboard.BACK
 * */
public var backKeyCode:int = Keyboard.BACK;
/**
 * KeyCode to use when navigation support is enabled.
 * Default is Keyboard.SEARCH
 * */
public var forwardKeyCode:int = Keyboard.SEARCH;
/**
 * @copy mx.core.UIComponent#measure()
 * */
override protected function measure():void {
    super.measure();

    measuredWidth=480;
    measuredMinWidth=120;
    measuredHeight=320;
    measuredMinHeight=160;
}
/**
 * @copy mx.core.UIComponent#updateDisplayList()
 * */
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
// NOTE: IF THE WEBVIEW IS NOT BEING SIZED CORRECTLY
// check if focusEnabled is true. If it is then the soft keyboard may not be dispatching the
// deactivate event because the webview has focus when it is dispatched. set to false
// position according to the container rather than the stage
    var runtimeDPI:int = FlexGlobals.topLevelApplication.runtimeDPI;
    var applicationDPI:int = FlexGlobals.topLevelApplication.applicationDPI;
    var point:Point = localToGlobal(new Point());
    var scaleFactor:Number = runtimeDPI / applicationDPI;
    var scaledWidth:int = width * scaleFactor;
    var scaledHeight:int = height * scaleFactor;
    webView.viewPort = new Rectangle(point.x, point.y, scaledWidth, scaledHeight);
}

//--------------------------------------------------------------------------
//
//  Event handlers
//
//--------------------------------------------------------------------------

/** When the stage property is available add it to the web view */
public function addedToStage(event:Event):void {
//  adds support for keyboard events when not in focus
    if (navigationSupport && addKeyHandlerToStage)
        stage.addEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler);
    _visibleChanged = true;
    invalidateProperties();
    invalidateDisplayList();
}
/** When removed from the stage remove the web view */
protected function removedFromStage(event:Event):void {
    hideWebView();
//  removes support for keyboard events when not in focus
    if (navigationSupport && addKeyHandlerToStage)
        stage.removeEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler );
}
/** Dispatches a focus in event when the web view gains focus. */
protected function focusInViewHandler(event:FocusEvent):void {
    //webView.assignFocus();

    if (hasEventListener(event.type))
        dispatchEvent(event);

}
/** Dispatches a focus out event when the web view gains focus. */
protected function focusOutViewHandler(event:FocusEvent):void {
    //webView.assignFocus(FocusDirection.TOP);

    if (hasEventListener(event.type))
        dispatchEvent(event);
}
/** Dispatches a focus in event when the web view gains focus. */
override protected function keyDownHandler(event:KeyboardEvent):void {

    if (navigationSupport) {
        if (event.keyCode == backKeyCode && webView.isHistoryBackEnabled ) {
            webView.historyBack();
            event.preventDefault();
        }

        if (navigationSupport && event.keyCode == forwardKeyCode && webView.isHistoryForwardEnabled ) {
            webView.historyForward();
        }
    }

    super.keyDownHandler(event);
}

/** Dispatched when the page or web content has been fully loaded */
protected function completeHandler(event:Event):void {
    if(hasEventListener(event.type)) dispatchEvent(event); }
/** Dispatched when the location is about to change */
protected function locationChangingHandler(event:Event):void {
    if(hasEventListener(event.type)) dispatchEvent(event); }
/** Dispatched when the location has changed */
protected function locationChangeHandler(event:Event):void {
    if(hasEventListener(event.type)) dispatchEvent(event); }
/** Dispatched when an error occurs */
protected function errorHandler(event:ErrorEvent):void {
    if(hasEventListener(event.type)) dispatchEvent(event); }

}}
package-ctrls{
导入flash.display.Bitmap;
导入flash.display.BitmapData;
导入flash.events.ErrorEvent;
导入flash.events.Event;
导入flash.events.FocusEvent;
导入flash.events.KeyboardEvent;
导入flash.events.LocationChangeEvent;
导入flash.geom.Point;
导入flash.geom.Rectangle;
导入flash.media.StageWebView;
导入flash.ui.Keyboard;
导入mx.core.FlexGlobals;
导入mx.core.ui组件;
/**@copy flash.media.StageWebView#ErrorEvent.ERROR*/
[事件(name=“error”,type=“flash.events.ErrorEvent”)]
/**@copy flash.media.StageWebView#Event.COMPLETE*/
[事件(name=“complete”,type=“flash.events.Event”)]
/**@copy flash.media.StageWebView#LocationChangeEvent.LOCATION_正在更改*/
[事件(name=“locationChanging”,type=“flash.events.locationchangevent”)]
/**@copy flash.media.StageWebView#LocationChangeEvent.LOCATION_CHANGE*/
[事件(name=“locationChange”,type=“flash.events.LocationChangeEvent”)]
/**
*此类使用UIComponent包装标准StageWebView。
*这允许以相同的方式调整其大小和位置
*任何UIComponent都可以。

* *StageWebView类文档如下所示:
*@copy flash.media.StageWebView * */ 公共类WebView扩展UIComponent{ //================================================================================================== 受保护的var_网络视图:StageWebView; 私有变量源:字符串; 私有变量visibleChanged:Boolean; 私有变量_sourceChanged:Boolean; /**@copy flash.media.StageWebView#viewPort*/ 公共函数get viewPort():矩形{return\u webView?\u webView.viewPort:null;} /**@copy flash.media.StageWebView#dispose()*/ 公共函数dispose():void{hideWebView(true);} /**@copy flash.media.StageWebView#assignFocus()*/ 公共函数assignFocus(direction:String=“none”):void{webView.assignFocus(direction);} /**@copy flash.media.StageWebView#drawViewPortToBitmapData()*/ 公共函数drawViewPortToBitmapData(位图:BitmapData):void{webView.drawViewPortToBitmapData(位图);} /**@copy flash.media.StageWebView#title*/ 公共函数get title():字符串{return\u webView?\u webView.title:null;} /**@copy flash.media.StageWebView#isHistoryBackEnabled()*/ 公共函数get-isHistoryBackEnabled():布尔值{return\u webView?\u webView.isHistoryBackEnabled:false;} /**@copy flash.media.StageWebView#isHistoryForwardEnabled()*/ 公共函数get-isHistoryForwardEnabled():布尔值{return\u webView?\u webView.isHistoryForwardEnabled:false;} /**@copy flash.media.StageWebView#historyBack()*/ 公共函数historyBack():void{if(_webView)_webView.historyBack();} /**@copy flash.media.StageWebView#historyForward()*/ 公共函数historyForward():void{if(_webView)_webView.historyForward();} /**@copy flash.media.StageWebView#reload()*/ 公共函数重载():void{webView.reload();} /**@copy flash.media.StageWebView#stop()*/ 公共函数stop():void{if(_webView)webView.stop();} /**加载传入的URL或加载源属性中指定的URL *@see flash.media.StageWebView#loadURL()*/ 公共函数加载(url:String=null):void{webView.loadURL(url?_source=url:source);} 重写公共函数集可见(值:布尔):无效{ super.visible=值; _visibleChanged=true; 无效属性(); 无效化(); } /**@私人*/ 公共函数get source():字符串{return\u source;} /** *阶段web视图的源URL。 *@see flash.media.StageWebView#loadURL() * */ [可装订] 公共函数集源(值:字符串):void{ _来源=价值; _sourceChanged=true; 无效属性(); 无效化(); } //================================================================================================== /** *StageWebView的包装器 * *@copy flash.media.StageWebView */ 公共函数WebView(){ addEventListener(Event.ADDED_至_阶段,AddedStatage); addEventListener(Event.REMOVED_FROM_STAGE,removedfrom STAGE); 聚焦=假; } /**@私人*/ 公共函数get webView():StageWebView{ 如果(!\u webView)webView=new StageWebView(); 返回_webView;} /**@copy flash.media.StageWebView*/ 公共函数集webView(值:StageWebView):无效{ if(_webView==value)返回; 如果(_webView){ _removeEventListener(Event.COMPLETE,completeHandler); _removeEventListener(ErrorEvent.ERROR,errorHandler); _webView.removeEventListener(FocusEvent.FOCUS_IN,focusInViewHandler); _removeEventListener(FocusEvent.FOCUS_OUT,focusOutViewHandler); _webView.removeEventListener