关于Flex中图像处理的一些问题

发布于 2024-11-04 02:29:10 字数 304 浏览 5 评论 0原文

这就是我想做的。应用程序运行时默认加载图像。有一种方法可以通过指定 url 来加载用户想要的另一张图像。加载用户定义的图像时,默认图像仍然在后台,并且有一些方法可用于在整个图像上应用一些过滤器(我的意思是具有默认图像和用户加载图像的结果图像)混合),然后我想将最终图像保存为 jpg 或 png。

现在,我仍然是 Flex 的初学者,对所有画布、图像控件、位图数据等都感到困惑。我需要帮助的是实现我想要的最好方法是什么?我应该将默认图像加载到带有 url/embed 的图像中还是应该将其加载为 BitmapData,如何加载第二个用户定义的图像?混合两个图像的最佳方法是什么?

Here's what I want to do. An image is loaded by default when the app is run. There is a way to load another image the user wants by specifying an url. When the user defined image is loaded, the default image is still in the background and there are some method that would be used to apply some filters on the image as a whole (I mean the resultant image with both the default and the user loaded image blended) and then I want to save the final image as jpg or png.

Now, I am still a beginner at Flex, and getting all confused with all the canvas, image control, bitmapdata etc. Where I want help is what's the best way to implement what I want? Should I load the default image into an Image with url/embed or should I load it as a BitmapData, how do I load the second user defined image? Whats the best way to blend the two images?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2024-11-11 02:29:10

您可以将默认图像保留为嵌入图像,并检索其 BitmapData 以进一步使用。

加载用户定义的图像时,您检索其 BitmapData,并在用户定义的图像上绘制嵌入图像:

/**
 * An embedded image's class (your default image)
 */
[Embed(source="/assets/logo.png")]
public static var Logo:Class;

/**
 * @param bitmapData: The user-defined BitmapData that you want to modify
 * @param matrix: The transofrmation matrix applied to the resulting BitmapData 
 */
public function getCustomBitmapData(bitmapData:BitmapData, matrix:Matrix):BitmapData
{
    // Initialize and drawing the resulting BitmapData's first layer 
    var result:BitmapData = new BitmapData(bitmapData.height, bitmapData.width);
    result.draw(bitmapData, matrix);

    // Load the BitmapData of the embedded Logo image
    var bitmapAsset:BitmapAsset = new Logo();
    var logoBd:BitmapData = bitmapAsset.bitmapData;

    // Draw the logo over the result with an alpha of 0.3
    result.draw(logoBd, matrix, new ColorTransform(1, 1, 1, .3));
    //TODO: You should play with the size of the images, apply filters, etc.

    return result;
}

然后您可以将生成的 BitmapData 实例保存到本地文件系统:

/**
 * Save the BitmapData to a local file
 * @param bitmapData: the data to save
 */
public function saveBitmapData(bitmapData:BitmapData):void
{
    // Initialize the encoder
    var pngEncoder:PNGEncoder = new PNGEncoder();
    // Encode the BitmapData and save its byte array
    var imageBytes:ByteArray = pngEncoder.encode(bitmapData);
    // Create a new FileReference:
    var imageFile:FileReference = new FileReference();
    // Save the file:
    imageFile.save(imageBytes, "myimage.png");
}

You can leave the default image as embedded one, and retrieve its BitmapData to work with further.

When the user-defined image is loaded, you retrieve its BitmapData, and draw the embedded image over the user-defined one:

/**
 * An embedded image's class (your default image)
 */
[Embed(source="/assets/logo.png")]
public static var Logo:Class;

/**
 * @param bitmapData: The user-defined BitmapData that you want to modify
 * @param matrix: The transofrmation matrix applied to the resulting BitmapData 
 */
public function getCustomBitmapData(bitmapData:BitmapData, matrix:Matrix):BitmapData
{
    // Initialize and drawing the resulting BitmapData's first layer 
    var result:BitmapData = new BitmapData(bitmapData.height, bitmapData.width);
    result.draw(bitmapData, matrix);

    // Load the BitmapData of the embedded Logo image
    var bitmapAsset:BitmapAsset = new Logo();
    var logoBd:BitmapData = bitmapAsset.bitmapData;

    // Draw the logo over the result with an alpha of 0.3
    result.draw(logoBd, matrix, new ColorTransform(1, 1, 1, .3));
    //TODO: You should play with the size of the images, apply filters, etc.

    return result;
}

Then you can save the resulting BitmapData instace to the local file system:

/**
 * Save the BitmapData to a local file
 * @param bitmapData: the data to save
 */
public function saveBitmapData(bitmapData:BitmapData):void
{
    // Initialize the encoder
    var pngEncoder:PNGEncoder = new PNGEncoder();
    // Encode the BitmapData and save its byte array
    var imageBytes:ByteArray = pngEncoder.encode(bitmapData);
    // Create a new FileReference:
    var imageFile:FileReference = new FileReference();
    // Save the file:
    imageFile.save(imageBytes, "myimage.png");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文