如何在 Java 中实现启发式选择合适的图像

发布于 2024-09-27 07:30:34 字数 882 浏览 1 评论 0原文

对于展示产品信息的页面,我们必须从一组显示相同内容、但格式不同(gif、png、jpg等)、质量不同(jpeg压缩)、不同压缩比的图片中选择最佳的产品图片。尺寸(缩略图、小、中、大)等。

选择取决于用户的浏览器、页面的当前大小、图像的用途等。

我们当前的解决方案是根据当前的需求构建一个 sql 查询,如下所示this:

SELECT img.id 
FROM img 
WHERE img.format IN ('GIF','JPG') 
AND img.width <= 1000
ORDER BY img.quality DESC

这是一个简单的例子,原来的要复杂得多。创建此语句的 java 代码使用了大量的 if 和 case,并且开始变得非常难看。

在 Java 中实现这种启发式的最佳方法是什么?有什么图书馆可以提供帮助吗?也许沿着定义规则对象的思路:

Engine engine = new Engine();
engine.addRule(new IncludeRule("format", {"GIF", "JPG"}));//only gif and jpg
engine.addRule(new MaxRule("width", 1000));//max width
engine.addRule(new WeightedRule("quality", DESC));//go for high quality
Image result = engine.getResult();

我搜索了规则引擎,甚至有一个 JSR 和一些开源规则引擎,但它们似乎都在处理业务规则。

我有一种强烈的感觉,我们正在重新发明轮子,而且我们根本找不到任何解决方案,因为我们不知道这个东西的正确名称;-)

任何帮助将不胜感激!

For a page that displays product information, we have to select the best product image from a set of images that show the same content, but have different formats (gif, png, jpg, etc.), different quality (jpeg comrpession), different sizes (thumbnails, small, medium, big) etc.

The choice depends on the user's browser, current size of page, purpose of the image, etc.

Our current solution is to build an sql query according to the current requirements that might look like this:

SELECT img.id 
FROM img 
WHERE img.format IN ('GIF','JPG') 
AND img.width <= 1000
ORDER BY img.quality DESC

This is a simplyfied example, the original is much more complex. The java code that creates this statement uses a lot of ifs and cases and is starting to grow very ugly.

What is the best way to implement such a heuristic in Java? Is there any library that could help? Maybe along the lines of defining rule objects:

Engine engine = new Engine();
engine.addRule(new IncludeRule("format", {"GIF", "JPG"}));//only gif and jpg
engine.addRule(new MaxRule("width", 1000));//max width
engine.addRule(new WeightedRule("quality", DESC));//go for high quality
Image result = engine.getResult();

I searched for Rule Engine, and there even is a JSR and some open source rule engines, but they seem to be all dealing with Buissness rules.

I have the strong feeling that we are reinventing the wheel, and that we simply can't find any solution because we don't know the right name for this stuff ;-)

Any help would be greatly appreciated!

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

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

发布评论

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

评论(1

影子是时光的心 2024-10-04 07:30:34

如果您只是构建一个 SQL 查询,您可能可以自己构建引擎:

public interface Rule{
    String getSQLWhereClause();
    String getSQLOrderClause();
}

public class Engine
{
    String buildSQLFromRules(Collection<Rule> rules) {
        String s = "SELECT IMG.ID FROM IMG";
        String w = "";
        String o = "";

        for(Rule r : rules)
        {
             if (r.getSQLWhereClause() != null) {
                 if (!w.isEmpty()) { w = w + " AND " }

                  w += r.getSQLWhereClause()
             }
             ... same for order
         }

         return s + (w.isEmpty()? "" : " WHERE " + w) + (o.isEmpty()?"": " ORDER BY " + o);
      }
 }

然后您可以将接口定义为:

 public class MaxRule implements Rule
 {
       String fld;
       String max;

       public MaxRule() { ... }

       String getSQLWhereClause() { return "img." + fld + " <= " + max; }
       String getSQLOrderClause() { return null; }
 }

If you're just building a SQL query you could probably build the engine yourself:

public interface Rule{
    String getSQLWhereClause();
    String getSQLOrderClause();
}

public class Engine
{
    String buildSQLFromRules(Collection<Rule> rules) {
        String s = "SELECT IMG.ID FROM IMG";
        String w = "";
        String o = "";

        for(Rule r : rules)
        {
             if (r.getSQLWhereClause() != null) {
                 if (!w.isEmpty()) { w = w + " AND " }

                  w += r.getSQLWhereClause()
             }
             ... same for order
         }

         return s + (w.isEmpty()? "" : " WHERE " + w) + (o.isEmpty()?"": " ORDER BY " + o);
      }
 }

Then you can just define the interfaces as:

 public class MaxRule implements Rule
 {
       String fld;
       String max;

       public MaxRule() { ... }

       String getSQLWhereClause() { return "img." + fld + " <= " + max; }
       String getSQLOrderClause() { return null; }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文