Saving iPhone camera photo to JPG format: conversion aka compression

You must Login before you can answer or comment on any questions.

I'm trying to compress the PNG into JPG I'm getting from the iPhone camera.

Yet, I receive the image, resizes it correctly,

var resize_photo = function(img, w, h) {
    // Original orientation
    var wd = (img.width > img.height) ? ((w>h) ? w : h) : ((h>w) ? h : w);
    var ht = (img.width > img.height) ? ((w>h) ? h : w) : ((h>w) ? w : h);
    // imageView
    var resize = Titanium.UI.createImageView ({
        width:wd,
        height:ht,
        image:img
    });
    return resize.toImage();    //returns TiBlob
};
and then I try to use this module to achieve JPEG compression: com.sideshowcoder.jpgcompressor

The module is installed in /Library/Application Support/Titanium/modules/.

This line has been added to my tiapp.xml :

<module platform="iphone" version="0.4">com.sideshowcoder.jpgcompressor</module>

And I use the provided example code to compress my image :

// (image) is a TiBlob image resulting from imageView.toImage()
var jpgcompressor = require("com.sideshowcoder.jpgcompressor");
jpgcompressor.setCompressFactor(1000);
var cImg = jpgcompressor.compress(image);
From here, I'm totally lost, since it simply does not pass the first of these 3 lines...

var jpgcompressor = require("com.sideshowcoder.jpgcompressor");

Any cue... please... :)

— asked 3 years ago by Benoit Blais
3 Comments
  • I think it has to do with jpgcompressor compilation. Will get back as soon as I find a solution...

    — commented 3 years ago by Benoit Blais

  • Correction for clarifying the question...

    This line has been added to my tiapp.xml:

    <module platform="iphone" version="0.4">com.sideshowcoder.jpgcompressor</module>

    — commented 3 years ago by Benoit Blais

  • please post the error you are getting...

    — commented 3 years ago by Matt Apperson

1 Answer

I use it here in my project, but I know there has been problems with older versions...

Here is a working-like-a-charm code excerpt :

in tiapp.xml :

<ti:app xmlns:ti="http://ti.appcelerator.org">
...
    <modules>
        <module platform="iphone" version="0.5.1">com.sideshowcoder.jpgcompressor</module>
    </modules>
...
</ti:app>
in app.js :
var useJpeg = function(filePath) {
    // working with the jpeg using file name
};
 
...
 
Ti.Media.showCamera({
    success: function(event) {
        var jpgcompressor = require('com.sideshowcoder.jpgcompressor');
 
        var image = event.media;
 
        // Resizing image to 1200x900
        if (image.width > image.height) {
            if ((image.width > 1200) || (image.height > 900)) {
                image = jpgcompressor.scale(event.media,1200,900);
            }
        } else {
            if ((image.width > 900) || (image.height > 1200)) {
                image = jpgcompressor.scale(event.media,900,1200);
            }
        }
 
        jpgcompressor.setCompressSize(184320); // around 180k
        jpgcompressor.setWorstCompressQuality(0.4); // minimum quality : 40%
 
        var fPath = jpgcompressor.compress(image, nom_photo); // compress and save image
        useJpeg(fPath);
    }
})

— answered 3 years ago by Benoit Blais
answer permalink
2 Comments
  • I'm using the same library... any idea "how low you can go" without losing quality?

    — commented 3 years ago by Elijah Windsor

  • Sorry for taking so long to answer... In fact, I've been away from all this stuff for over a year now... And now I'm gettiing back to it, I was wondering if any of you had an answer to that question: "how low you can go" and if you ever found another to do compression ?

    — commented 2 years ago by Benoit Blais

Your Answer

Think you can help? Login to answer this question!