Re: Reading NSData from a file
Re: Reading NSData from a file
- Subject: Re: Reading NSData from a file
- From: Pascal Robert <email@hidden>
- Date: Tue, 3 Apr 2007 13:59:36 -0400
Le 07-04-03 à 00:56, Mike Schrag a écrit :
What I'm trying to do here is to read back in the results of
having run convert on an uploaded image file and making a
thumbnail from it. The files are being written only for the
convenience of convert, so if I could get out of writing them
that would be all the better.
What are you using to convert it? AWT is not too bad for a Java
solution.
If by "not too bad" you mean "100x slower than native code" ... It
all depends on your needs:
* Slow, Easy, Pure Java, Cross Platform = JAI / Java2D (JAI is not
hardware accelerated on OS X)
* Faster, Pretty Annoying, Native, Mostly Cross Platform (portable
c) = System call to ImageMagick
* Faster, Way Annoying, Native, Mostly Cross Platform (portable c)
= JNI ImageMagick (this has huge memory pitfalls, don't do it)
* Really Fast, Easy, OS X Only = System call to 'sips'
* Super Fast, More Annoying, OS X Only = JNI CoreImage/ImageIO
(Brendan posted his version of this on wocode)
Or you can use QuickTime4Java, we use it to convert pictures that
people upload with a WOFileUpload. It can work on Windows since
QuickTime is available on it (you have to do a custom install on
Windows to get QT4J). QT4J use JNI, so I guess the speed is ok.
Sample (sorry for the French names)
import quicktime.util.*;
import quicktime.qd.*;
import quicktime.std.movies.media.*;
import quicktime.std.image.*;
import quicktime.QTSession;
public static NSData ajoutPhoto(NSData nouvellePhotoData) {
ImageDescription descOriginale;
ImageDescription descNouvelle = null;
float ratio;
int nWidth = 0;
int nHeigth = 0;
Float fWidth;
Float fHeigth;
try {
// Nous devons toujours ouvrir une session QuickTime
QTSession.open();
QTHandleRef fileContentsHandle = new QTHandle
(nouvellePhotoData.bytes());
GraphicsImporter aGraphicsImporter = new GraphicsImporter(new
DataRef(fileContentsHandle));
// La classe ImageDescription retourne quelques informations a
propos de l'image
descOriginale = ImageDescription.fromGraphicsImporter
(aGraphicsImporter);
fWidth = new Float(descOriginale.getWidth());
fHeigth = new Float(descOriginale.getHeight());
NumberFormat unDigit = NumberFormat.getInstance(Locale.US);
unDigit.setMaximumFractionDigits(1);
unDigit.setMinimumFractionDigits(1);
if (descOriginale.getWidth() > descOriginale.getHeight()) {
nWidth = 300;
ratio = fWidth.floatValue() / fHeigth.floatValue();
if (unDigit.format(ratio).equals("1.5")) {
nHeigth = 200;
} else if (unDigit.format(ratio).equals("1.3")) {
nHeigth = 225;
} else {
// C'est quoi ce ratio ??
Float dRatio = new Float(ratio);
float ratio3 = (new Float("300.00")).floatValue
() / ratio;
nHeigth = Math.round(ratio3);
NSLog.out.appendln("Format bizarre de photo: " +
unDigit.format(ratio));
}
}
QDRect myFinalRect = new QDRect();
myFinalRect.setWidth(nWidth);
myFinalRect.setHeight(nHeigth);
myFinalRect.setX(0);
myFinalRect.setY(0);
QDGraphics imagesGWorld = aGraphicsImporter.getImageDescription
().newGWorld(0);
// On ouvre le contenu du fichier original
QDGraphics myGWorld = new QDGraphics(32, myFinalRect, 0);
QTImage.decompress(new RawEncodedImage(nouvellePhotoData.bytes()),
aGraphicsImporter.getImageDescription(),
imagesGWorld,
myFinalRect,
0 /* srcCopy */);
// Et on genere la nouvelle image
RawEncodedImage encodedImage = new RawEncodedImage(1200000, true);
descNouvelle = QTImage.compress(imagesGWorld,
myFinalRect, 1024,0x6A706567, /* "JPEG" */ encodedImage);
nouvellePhotoData = new NSData(encodedImage.getBytes(), 0,
descNouvelle.getDataSize());
QTSession.close();
return nouvellePhotoData;
} catch (quicktime.std.StdQTException qtex) {
NSLog.out.appendln(qtex.toString());
} catch (quicktime.QTException qtex) {
NSLog.out.appendln(qtex.toString());
}
return null;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden