如何使用cfimage将图像保存为渐进式jpg?

时间:2015-12-09 12:24:05

标签: coldfusion jpeg coldfusion-11

我使用ColdFusion的<cfimage>来调整图像大小,然后在服务器上另存为#imagename#.jpg

我可以看到指定quality的选项,但我看不到任何将jpg保存为渐进式甚至优化的内容。我想要进步,因为它给人一种在页面上加载更快的印象。

有可能这样做吗?

1 个答案:

答案 0 :(得分:2)

CFImage不支持开箱即用编写渐进式jpegs。据我所读,在某种程度上是supported in java。但是,它是一个主题。所以这绝不是一个完整的答案,只是一个起点。

<强>爪哇

我对jpeg格式的了解非常简陋,但在java中创建一个基本的渐进式jpeg似乎很简单:

  1. 抓住JPEG编写器的实例
  2. 初始化图像参数和输出设置
  3. 从CF图像对象中提取基础BufferedImage
  4. 将新jpeg写入磁盘
  5. 查看行动的一种方法是using Fiddler2 to simulate a slow connection。根据规则&gt;性能,选择“模拟调制解调器速度”和“禁用缓存”。

    示例:

        // Create your CF image object 
        yourCFImage = imageNew("c:\path\input.jpg"); 
    
        // ... resizing and other stuff 
    
        // Grab a JPEG writer. Validation omitted for brevity
        // In real code, verify writer exists and progressives is supported first
        // ie jpegWriters.hasNext() and imageParam.canWriteProgressive()
        ImageIO = createObject("java", "javax.imageio.ImageIO");
        jpegWriters = ImageIO.getImageWritersByFormatName("jpg");
        writer = jpegWriters.next();
        imageParam = writer.getDefaultWriteParam();
    
        // Where to save new image on disk
        destinationFile = createObject("java", "java.io.File").init( "c:\path\outut.jpg" );
        destinationStream = ImageIO.createImageOutputStream( destinationFile );
    
        // Parameters for desired image quality and interlacing
        // NB: Compression type support varies (JPEG-LS, ...)
        // Check availability with getCompressionType()
        writer.setOutput( destinationStream );
        imageParams = writer.getDefaultWriteParam();
        imageParams.setCompressionMode( imageParams.MODE_EXPLICIT);
        imageParams.setCompressionQuality( javacast("float", 0.80) ); // 80%
        imageParams.setProgressiveMode( imageParams.MODE_DEFAULT );
    
        // Write the new image to disk
        buffImage = ImageGetBufferedImage( yourCFImage );
        IIOImage = createObject("java", "javax.imageio.IIOImage");
        imageToSave = IIOImage.init( buffImage, javacast("null", ""), javacast("null", ""));
        writer.write( javacast("null", ""), imageToSave, imageParams );
    
        // Cleanup object
        destinationStream.flush(); 
        destinationStream.close(); 
        writer.dispose(); 
    

    外部工具

    另一个选项是使用cfexecutejpegtran等外部工具<cfexecute name="C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" arguments=" -interlace line -quality 85 c:\path\source.jpg c:\path\output.jpg" ... > 。两者都支持渐进式jpeg和其他自定义选项。例如,jpegtran支持自定义ImageMagic,它提供了很多控制。 (不幸的是,我还没有完全围绕那些......)。首先使用默认设置进行尝试。这些可能足以满足您的目的。

    12-11 22:09:18.926  31825-31825/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: tum.andrive.lanedetection, PID: 31825
    java.lang.UnsatisfiedLinkError: Couldn't load LaneDetectionNative from     loader dalvik.system.PathClassLoader[dexPath=/data/app/tum.andrive.lanedetection-5.apk,libraryPath=/data/app-lib/tum.andrive.lanedetection-5]: findLibrary returned null
            at java.lang.Runtime.loadLibrary(Runtime.java:358)
            at java.lang.System.loadLibrary(System.java:526)
            at tum.andrive.lanedetection.LaneDetector$1.onManagerConnected(LaneDetector.java:53)
            at org.opencv.android.AsyncServiceHelper$3.onServiceConnected(AsyncServiceHelper.java:318)
            at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1119)
            at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1136)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5377)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
    12-11 22:09:19.246    891-32069/? E/android.os.Debug﹕ !@Dumpstate > sdumpstate -k -t -z -d -o /data/log/dumpstate_app_error
    12-11 22:09:22.886  32153-32153/? E/QSEECOMAPI:﹕ Error::Failed to open /dev/qseecom device
    12-11 22:09:22.886  32153-32153/? E/QSEECOMAPI:﹕ Error::Failed to open /dev/qseecom device
    

    顺便说一句,我在研究时遇到了一个有趣的工具:scan files。它不习惯创建渐进式jpeg,但它确实有助于解释它们是如何生成的以及扫描适合该过程的位置。