|
QJCC homepage | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object biz.chitec.quarterback.swing.ImageEditModel
Image editing model for some basic operations: Cropping, scaling, changing format and compression level
ImageEditModel allows some basic operation on a given image file: Cropping to take only a part of the image, scaling to make the image smaller (or larger...), and changing storage type (PNG versus JPG) and JPG compression level, if applicable.
This model gets its image by the setImage(imagedata,lossyflag)
method. The
image data is taken verbatim from a file or any other source producing an image in JPG or PNG
encoding. To get an editing result, call getImage()
. To see whether any editing
took place at all, call isEdited()
and/or hook a listener onto the EDITED
property. Images can also be loaded and stored directly between the model and a disk file.
ImageEditModel creates internally a java.awt.Image from the given data. All operations are always performed on this representation and are therefore lossless, even if output is set to, say, JPG with a very low quality level. An editor can show the data in the selected quality level on screen, but the model keeps the lossless representation internally. The first place where this quality-decreased data shows up again programmatically is the getImage() method. Therefore, during editing, it is even possible to change JPG quality level in both directions. Undo always starts again with the data given through setImage().
This class has been derived from the TinyImageEditor which featured both editor widget and model at once. As the possibility to change an image's proportions etc. can also be useful without the dialog widget, the data representation has been seperated from the user interface lateron. Note that this model is not aware of any thread changing issues as TinyImageEditor. the model works totally independent from the AWT thread. Especially, the events generated by ImageEditModel are not sent on the AWT thread! An event receiver must dispatch itself appropriately if it wants to do anything on the GUI.
As usual, the model interacts with any other parts of the program through the means of events. Note however that it does not pass the image data itself with the imageChanged events. Any interested event receiver must call the getImage() operations to receive the image data after the event has been received.
Field Summary | |
private static float |
DELTA
|
private boolean |
edited
|
private java.awt.image.BufferedImage |
losslessimage
|
private boolean |
lossy
|
private boolean |
lossychanged
|
private java.awt.image.BufferedImage |
lossyimage
|
private float |
lossyquality
|
private byte[] |
originalbytes
|
private boolean |
originallossy
|
private java.beans.PropertyChangeSupport |
pcs
|
private byte[] |
resultbytes
|
private boolean |
resultchanged
|
Constructor Summary | |
ImageEditModel()
|
Method Summary | |
void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
|
void |
addPropertyChangeListener(java.lang.String propertyName,
java.beans.PropertyChangeListener listener)
|
void |
clear()
|
private void |
createLossyImage()
Create the lossy image by reading the lossy byte stream. |
private void |
createResultBytes()
Creates the result byte stream and the lossy image from the lossless image. |
void |
cropImage(java.awt.Rectangle croprect)
Performs the crop operation The rectangle to be left over from the crop operation is taken from the image pane. |
private void |
fireEdited(boolean newval)
Set internally whether something has been edited compared to the setImage() state. |
byte[] |
getImage()
Returns the edited image as lossy or lossless byte stream. |
int |
getImageHeight()
Return height of the image. |
int |
getImageWidth()
Return width of the image. |
java.awt.image.BufferedImage |
getLossLessBufferedImage()
Returns the internally stored lossless image object. |
java.awt.image.BufferedImage |
getLossyBufferedImage()
Returns the internally stored lossy image object. |
float |
getLossyQuality()
Get the lossy quality setting. |
java.beans.PropertyChangeListener[] |
getPropertyChangeListeners()
|
java.beans.PropertyChangeListener[] |
getPropertyChangeListeners(java.lang.String propertyName)
|
boolean |
hasListeners(java.lang.String propertyName)
|
boolean |
isEdited()
Returns whether the current editor content is edited in any way. |
boolean |
isLossy()
Returns whether the result byte stream is lossy or not. |
void |
loadImage(java.lang.String filename)
Load an image directly from a file into the edit model. |
void |
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
|
void |
removePropertyChangeListener(java.lang.String propertyName,
java.beans.PropertyChangeListener listener)
|
void |
rescaleImage(int newx,
int newy,
boolean keepratio)
Performs the rescale operation. |
private void |
restoreImage()
Restore the complete editor from the originally given image. |
void |
setImage(byte[] imagedatax,
boolean lossyx)
Sets the image to work on. |
void |
setLossy(boolean lx)
Sets whether the output should be lossy or not |
void |
setLossyQuality(float lq)
Sets the lossy quality of the output. |
int |
setLossyQualityBySize(int size,
float maxquality)
Set the lossy quality so that the resulting file is about the size given as parameter in bytes. |
void |
softenImage(float factor)
Performs a soften operation. |
void |
storeImage(java.lang.String filename)
Stores an image directly from the model into a file. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final float DELTA
private byte[] originalbytes
private java.awt.image.BufferedImage losslessimage
private java.awt.image.BufferedImage lossyimage
private byte[] resultbytes
private boolean originallossy
private boolean lossy
private boolean edited
private boolean resultchanged
private boolean lossychanged
private float lossyquality
private java.beans.PropertyChangeSupport pcs
Constructor Detail |
public ImageEditModel()
Method Detail |
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
public void addPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
public java.beans.PropertyChangeListener[] getPropertyChangeListeners()
public java.beans.PropertyChangeListener[] getPropertyChangeListeners(java.lang.String propertyName)
public boolean hasListeners(java.lang.String propertyName)
public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
public void removePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
public void loadImage(java.lang.String filename) throws java.io.IOException
filename
- Full name of the file with the image. Must end with ".JPG" or ".PNG" (case insensitive)
java.io.IOException
- If something goes wrong with the loading.public void setImage(byte[] imagedatax, boolean lossyx)
imagedatax
- The image data as byte stream (e.g. the contents of a JPG file on disk)lossyx
- Flag whether the image data should be handled as lossy (true) or lossless (false)public byte[] getImage()
Note that this is not the method a GUI should use for representing the model on screen. For this purpose, the two getLossy/LossLessBufferedImage()-methods below should be used.
public void storeImage(java.lang.String filename) throws java.io.IOException
filename
- Full name of the file with the image. Must end with ".JPG" or ".PNG" (case insensitive)
java.io.IOException
- If something goes wrong with the storing.public void clear()
public void setLossy(boolean lx)
public boolean isLossy()
public boolean isEdited()
public void setLossyQuality(float lq)
lq
- Quality between 0 and 1.public float getLossyQuality()
public java.awt.image.BufferedImage getLossLessBufferedImage()
public java.awt.image.BufferedImage getLossyBufferedImage()
public int getImageWidth()
public int getImageHeight()
private void fireEdited(boolean newval)
newval
- The new "edited" state.private void restoreImage()
private void createResultBytes()
private void createLossyImage()
If the current output mode is "lossless", the method simply sets the lossy image object to be the same as the lossless one.
public int setLossyQualityBySize(int size, float maxquality)
Note that setting the lossy quality this way will of course only work if after calling this method no further changes are made to the image (cropping, resizing etc.). Otherwise, the size constaint will almost certainly get lost. Therefore, this method should only be called after all other operations are done and the image will finally be saved somewhere.
Due to the iterative approach, the method might be slow. This is especially true for (pixelwise) larger images as for each loop, the JPEG file has to be encoded and this is a rather time-consuming operation.
size
- Target file size. Derived range is between 90% and 100% of the given value.maxquality
- Maximum quality level of the target image.
public void rescaleImage(int newx, int newy, boolean keepratio)
newx
- New width of the imagenewy
- New height of the imagekeepratio
- if true, ratio of original image is kept. Either x or y might be shorter then newx/newy parameter.public void cropImage(java.awt.Rectangle croprect)
public void softenImage(float factor)
factor
-
|
QJCC homepage | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |