2009-02-08 9 views
2

Quelqu'un at-il un exemple d'utilisation de la fonction ImageSnapshot.captureBitmapData avec une matrice de rotation? Voici le code que je utilise:Impossible d'utiliser ImageSnapshot.captureBitmapData avec une matrice de rotation

var matrix:Matrix = new Matrix(); 
matrix.rotate(degreesToRadians(90)); 
var bitmapData:BitmapData = ImageSnapshot.captureBitmapData(textInput, matrix); 

Mais malheureusement, cela jette une erreur sur la ligne suivante dans ImageSnapshot.as:

data = new BitmapData(scaledWidth, scaledHeight, true, 0x00000000); // <-- THROWS ERROR HERE AS scaledWidth/scaledHeight are extremely small numbers (-1-e16 etc) 
      data.draw(source, matrix, colorTransform, 
         blendMode, clipRect, smoothing); 
     } 
     finally 
     { 
      if (source is IUIComponent) 
       finishPrintObject(IUIComponent(source), normalState); // <-- ERROR THROWN HERE, BUT CAUSE OF ERROR IS ABOVE 
     } 

Ce que je suis en train de réaliser est une image bitmap pivotée de un contrôle d'entrée de texte (j'essaie d'éviter d'incorporer une police dans l'application). Ce code fonctionne très bien lorsque je ne fais pas pivoter le bitmap, mais à la minute où je le fais pivoter, il se casse.

Post-Acceptée-réponse Modifier

je travaillais avec une classe de chargeur dans mon problème d'origine, et je voulais aussi le texte 270degrees - voici donc le texte qui fait que:

var matrix:Matrix = new Matrix(); 
         matrix.rotate(Math.PI * 1.5); 
         matrix.translate(0, copyThis.width); 

         var bitmapData:BitmapData = ImageSnapshot.captureBitmapData(copyThis, new Matrix()); 
         var rotatedBitmap : BitmapData = new BitmapData(bitmapData.height, bitmapData.width, false, 0xFFFF0000); 

         rotatedBitmap.draw(bitmapData, matrix); 

         loader.load(new Bitmap(rotatedBitmap)); 

Merci!

Répondre

3

Je viens juste de le reproduire, donc ça semble effectivement être un bug dans la classe ImageSnapshot. Tout ce que je peux penser, c'est qu'il n'a pas été testé en utilisant une matrice de rotation, donc il a été complètement raté. Évidemment, essayer de créer un bitmapData inférieur à 1x1 est la raison pour laquelle il lance l'erreur. Je ne peux pas sembler contourner ça pour être honnête. Je pense que votre meilleur pari pourrait être de créer votre propre classe de capture instantanée simplifiée, ou simplement utiliser une matrice d'identité pour obtenir le bitmap d'origine, puis le copier dans un nouveau bitmapData qui peut contenir la version pivotée.

Modifier

sur une autre note ... en tant que composants flash sont enregistrés en haut à droite, vous devez également traduire la matrice à travers par la hauteur d'origine. Bien que cela puisse avoir résolu le problème original, mais il casse toujours. Voici une solution qui utilise une nouvelle BitmapData et transforme celui créé par ImageSnapshot

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:Script> 
     <![CDATA[ 
      import mx.graphics.ImageSnapshot; 


      private function clickHandler(event : MouseEvent) : void 
      { 
       var matrix:Matrix = new Matrix(); 
       matrix.rotate(Math.PI/2); 
       matrix.translate(copyThis.height, 0); 

       var bitmapData:BitmapData = ImageSnapshot.captureBitmapData(copyThis, new Matrix()); 
       var rotatedBitmap : BitmapData = new BitmapData(bitmapData.height, bitmapData.width, false, 0xFFFF0000); 

       rotatedBitmap.draw(bitmapData, matrix); 

       var g : Graphics = copyTo.graphics; 

       g.clear(); 
       g.beginBitmapFill(rotatedBitmap); 
       g.drawRect(0,0, rotatedBitmap.width, rotatedBitmap.height); 
       g.endFill(); 
      } 
     ]]> 
    </mx:Script> 

    <mx:VBox width="100%" height="100%" horizontalAlign="center"> 
     <mx:Canvas width="100%" height="100%"> 
      <mx:TextInput id="copyThis" text="COPY THIS" horizontalCenter="0" verticalCenter="0"/> 
     </mx:Canvas> 
     <mx:Canvas id="copyTo" width="100%" height="100%" /> 
     <mx:Button id="copyIt" label="COPY IT" click="clickHandler(event)" /> 
    </mx:VBox> 
</mx:Application> 
+0

Vous monsieur, sont brillants :) J'ai essayé en utilisant un bitmap intermédiaire précédemment, mais je ne pouvais pas le faire fonctionner - il a été la traduction manquante! Si je pouvais attribuer plus de points pour la bonne réponse, je le ferais :) Merci! –