【问题标题】:Image manipulation using CamanJS使用 CamanJS 进行图像处理
【发布时间】:2015-09-24 06:24:36
【问题描述】:

我找到了一个名为CamanJS 的图像处理库,这些示例看起来不错,所以我尝试使用它。我得到了以下html:

<ion-view class="menu-content" view-title="Filter">
    <ion-content overflow-scroll="true">
        <div id="polaroid-filter-img" class="polaroid-filter-img">
            <img id="polaroid-filter-img-img" ng-src="{{polaroid.cropped}}" />
        </div>
        <div class="polaroid-filter-examples">
            <div ng-repeat="filter in filters" ng-click="performFilter(filter.id)" id="filter.id">
                <img id="{{filter.id}}" ng-src="{{filter.image}}"/>
                <span>{{filter.name}}</span>
            </div>
        </div>
    </ion-content>
</ion-view>

这个css:

.polaroid-filter-img {
    display: block;
    position: relative;
    margin-left: auto;
    margin-right: auto;
    top: 10%;
}

.polaroid-filter-img img {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
}

@media only screen and (orientation: portrait) {
    .polaroid-filter-examples {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        overflow: auto;
        overflow-x: scroll;
        overflow-y: hidden;
        white-space: nowrap;
    }
    .polaroid-filter-examples div {
        display: inline-block;
    }
}

@media only screen and (orientation: landscape) {
    .polaroid-filter-examples {
        position: fixed;
        top: 0;
        right: 0;
        height: 100%;
        overflow: auto;
        overflow-x: hidden;
        overflow-y: scroll;
        white-space: nowrap;
    }
    .polaroid-filter-examples div {
        display: block;
    }
}

.polaroid-filter-examples div {
    font-size: 14px;
    text-align: center;
    color: white;
}

.polaroid-filter-examples img {
    margin: 0 auto;
    padding: 2px 2px 2px 2px;
    width: 125px;
    height: 125px;
}

.polaroid-filter-examples span {
    display: block;
    padding-bottom: 10px;
    padding-top: 10px;
}

还有这个 JavaScript:

angular.module("App.Polaroids-Edit-Filter")

.controller("PolaroidsEditFilterController", function ($scope, $stateParams, PolaroidsService, FiltersService) {
    $scope.polaroid = PolaroidsService.getPolaroid($stateParams.id, $stateParams.size);
    $scope.filters = FiltersService.getFilters();

    var previousFilter = null;

    $scope.performFilter = function (id) {
        if (id != null && id != previousFilter) {
            if (previousFilter != null) {
                document.getElementById(previousFilter).style.border = "none";
            }

            document.getElementById(id).style.border = "solid #FF0000";

            if (id == "normal") {
                var image = document.getElementById("polaroid-filter-img-img");

                img.src = $scope.polaroid.cropped;
            } else {
                var image = document.getElementById("polaroid-filter-img-img");

                Caman(image, function () {
                    if (id == "vintage") {
                        this.vintage();
                    }

                    this.render(function () {
                        console.log("filtering finished");
                    });
                });   
            }

            previousFilter = id;
        }
    };

    function initView() {
        var width = screen.width * 0.7;
        var height = width;
        var initialId = $scope.filters[0].id;

        previousFilter = initialId;

        document.getElementById("polaroid-filter-img").style.width = width + "px";
        document.getElementById("polaroid-filter-img").style.height = height + "px";
        document.getElementById(initialId).style.border = "solid #FF0000";
    }

    var intervalId = setInterval(function () {
        initView();

        window.clearInterval(intervalId);
    }, 100);
});

当我启动我的应用程序并选择一张图片时,我得到以下信息:

当我现在点击老式过滤器时,会发生以下情况:

如您所见,图像被调整为实际宽度和高度,之后 大约 20 秒:

过滤器被应用。所以有两种奇怪的行为我不明白。

  • 第一:为什么要调整图像大小?
  • 第二:为什么应用过滤器需要这么长时间(20 秒)?我找到了一个他们使用 CamanJS 的演示,在该演示中过滤几乎立即执行:Instagram Filters

你能帮我理解那里出了什么问题吗?

编辑

如果我使用here 所示的画布进行操作,则在应用过滤器时画布会消失。

【问题讨论】:

    标签: javascript html css image camanjs


    【解决方案1】:

    如果它仍然可以帮助您解决问题,请查看另一篇文章Angularjs code works in desktop chrome, but not in mobile chrome 那里的解决方案很好地与 Ionic+AngularJS+Phonegap 配合使用,没有奇怪的调整大小。 HTH。

    【讨论】:

    • 谢谢。这改变了调整大小的问题。速度慢的问题仍然存在。例如,如果我想在 2000 x 2000 图片上使用复古滤镜,则需要 14 秒。如果我在 100 x 100 的图片上使用它需要 2 秒。
    • 确保使用该帖子代码中使用的“克隆”技巧。我已经将它与 1,6 MB 2048x1363 jpg 文件一起使用,我的测试设备(Nexus4 和 Z1 Compact)很好地管理了它。我使用 CamanJS 和 JSManipulate 库进行了测试,发现 JSManipulate 速度更快(它的质量很可能较低,我不是纯粹主义者,对于基于电话的应用程序,我相信它已经做到了:大约 1 秒到应用过滤)。
    【解决方案2】:

    我检查了您的代码并发现了 1 个与 css 相关的小问题。在您的班级.polaroid-filter-img img 中有position: absolute;。这应该是 position: relative; 。比如:

    .polaroid-filter-img img {
        position: relative;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
    }
    

    我认为的另一个问题是您的 javascript 函数 initView 。在此函数中,您的以下值会出错 heightwidth

        var width = screen.width * 0.7;
        var height = width;
        var initialId = $scope.filters[0].id;
    

    请检查控制台日志中的heightwidth

    【讨论】:

    • 感谢您的回答。该错误与CSS无关。宽度和高度也正确:width: 268.8, height: 268.8。当我使用画布而不是图像时,应用过滤器时画布消失了。调整 camanjs 的大小一定是有原因的。速度如此之慢一定是有原因的。
    • @Mulgard 你用position: relative;检查一下
    • 是的。这不会改变任何事情。该错误与css无关。
    猜你喜欢
    • 2014-04-08
    • 2011-06-13
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多