如何在 Java 中实现启发式选择合适的图像
对于展示产品信息的页面,我们必须从一组显示相同内容、但格式不同(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只是构建一个 SQL 查询,您可能可以自己构建引擎:
然后您可以将接口定义为:
If you're just building a SQL query you could probably build the engine yourself:
Then you can just define the interfaces as: