Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Work around requested

84 views
Skip to first unread message

bH

unread,
Dec 17, 2011, 10:03:44 PM12/17/11
to
Hi All,
I have jdk1.7.0, using a Windows7 machine.
I am working on a program
that will reduce a jpeg image to a
Thumbnail sized jpeg image. My program sample
uses these three imports,
among others. They are:

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

my IDE lists the error as
"package com.sun.image.codec.jepg does
not exist."

Is there a work around that will do the same thing
but without these import listed above?
TIA for your reply.

bH

Lew

unread,
Dec 17, 2011, 10:19:40 PM12/17/11
to
No.

We'll assume the misspelling in your post was a typo and that you did not copy
and paste the message.

The existence or non-existence of a package does not depend on your 'import'
directives.

You have not provided the correct classpath to the compiler for it to detect
the needed package.

--
Lew

John B. Matthews

unread,
Dec 18, 2011, 1:36:50 AM12/18/11
to
In article
<17844998.127.1324178380897.JavaMail.geo-discussion-forums@pril5>,
Lew <lewb...@gmail.com> wrote:

> bH wrote:
> > I have jdk1.7.0, using a Windows7 machine.
> > I am working on a program
> > that will reduce a jpeg image to a
> > Thumbnail sized jpeg image. My program sample
> > uses these three imports,
> > among others. They are:
> >
> > import com.sun.image.codec.jpeg.JPEGCodec;
> > import com.sun.image.codec.jpeg.JPEGEncodeParam;
> > import com.sun.image.codec.jpeg.JPEGImageEncoder;
> >
> > my IDE lists the error as
> > "package com.sun.image.codec.jepg does
> > not exist."
> >
> > Is there a work around that will do the same thing
> > but without these import listed above?
> > TIA for your reply.
>
> No.
>
> We'll assume the misspelling in your post was a typo and that you did
> not copy and paste the message.

Good point; exact, quoted error messages are also a good search target.

> The existence or non-existence of a package does not depend on your
> 'import' directives.
>
> You have not provided the correct classpath to the compiler for it to
> detect the needed package.

I recently installed 1.7.0_02, and I can see them in either NetBeans or
the command line with the classpath option, e.g.:

javap -classpath "\Program Files\Java\jdk1.7.0_02\src.zip" com.sun.image.codec.jpeg.JPEGCodec

The bigger problem is that these classes may not be available in future
releases. You might want to look at removing the dependence. The
classes javax.imageio.ImageIO and java.awt.image.AffineTransformOp may
be all you need.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Lew

unread,
Dec 18, 2011, 2:39:44 AM12/18/11
to
John B. Matthews wrote:
> I recently installed 1.7.0_02, and I can see them in either NetBeans or
> the command line with the classpath option, e.g.:
>
> javap -classpath "\Program Files\Java\jdk1.7.0_02\src.zip" com.sun.image.codec.jpeg.JPEGCodec
>
> The bigger problem is that these classes may not be available in future
> releases. You might want to look at removing the dependence. The
> classes javax.imageio.ImageIO and java.awt.image.AffineTransformOp may
> be all you need.

Excellent point, and rather more important than trying to solve the com.sun
import.

This sort of dependency issue is really good practice, because real-world
programming mostly hinges on dependency issues.

Using standard classes as John says has an effect on dependencies, that is it removes them, and fewer dependencies mean simpler and more stable builds, easier deployment and fewer production issues. Remember ops, and code clean.

--
Lew
Message has been deleted

Roedy Green

unread,
Dec 18, 2011, 3:51:10 PM12/18/11
to
On Sat, 17 Dec 2011 19:03:44 -0800 (PST), bH <bher...@hotmail.com>
wrote, quoted or indirectly quoted someone who said :

>
>import com.sun.image.codec.jpeg.JPEGCodec;
>import com.sun.image.codec.jpeg.JPEGEncodeParam;
>import com.sun.image.codec.jpeg.JPEGImageEncoder;

see http://mindprod.com/jgloss/jpegencoder.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
For me, the appeal of computer programming is that
even though I am quite a klutz,
I can still produce something, in a sense
perfect, because the computer gives me as many
chances as I please to get it right.

bH

unread,
Dec 19, 2011, 12:01:02 AM12/19/11
to
> <http://sites.google.com/site/drjohnbmatthews>- Hide quoted text -
>
> - Show quoted text -

Hi All,
Thanks for your help and suggestions.
The workaround is now complete.
bH

bH

unread,
Dec 20, 2011, 6:46:56 PM12/20/11
to
> bH- Hide quoted text -
>
> - Show quoted text -

Hi All,
I compared the output using one large image
1600 x 1200 pixels cropped to 200 x 150 in both
programs, first program uses:
com.sun.image.codec.jpeg.JPEGCodec
and the second program with the revision as suggested
above by John Matthews:
uses the classes javax.imageio.ImageIO and
java.awt.image.AffineTransformOp.

The comparison result is that the suggested
revision by John Matthews comes in second place.
It does do crop it, but is less perfect of a
crop than the first method listed.
For instance in the second program,
the image is not as sharp and clear.

bH

Knute Johnson

unread,
Dec 20, 2011, 7:53:25 PM12/20/11
to
My guess would be that AffineTransormOP uses those very same CODECS.

There are several options that can be specified to tune the result. Why
don't you post your code and a sample image.

--

Knute Johnson

bH

unread,
Dec 20, 2011, 8:02:18 PM12/20/11
to
> Knute Johnson- Hide quoted text -
>
> - Show quoted text -

Knute,
Posting a sample image is something I have never
done before. I would appreciated your guidance
to enable me to do it.
TIA
bH

John B. Matthews

unread,
Dec 20, 2011, 8:13:46 PM12/20/11
to
In article
<4decc207-ebaa-4d1c...@k10g2000yqk.googlegroups.com>,
bH <bher...@hotmail.com> wrote:

> I compared the output using one large image
> 1600 x 1200 pixels cropped to 200 x 150 in both
> programs, first program uses:
> com.sun.image.codec.jpeg.JPEGCodec
> and the second program with the revision as suggested
> above by John Matthews:
> uses the classes javax.imageio.ImageIO and
> java.awt.image.AffineTransformOp.
>
> The comparison result is that the suggested
> revision by John Matthews comes in second place.
> It does do crop it, but is less perfect of a
> crop than the first method listed.
> For instance in the second program,
> the image is not as sharp and clear.

Kudos for comparing. What interpolationType did you use?

<http://docs.oracle.com/javase/7/docs/api/java/awt/image/AffineTransformOp.html>

bH

unread,
Dec 20, 2011, 8:33:27 PM12/20/11
to
On Dec 20, 8:13 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <4decc207-ebaa-4d1c-9952-b73f18817...@k10g2000yqk.googlegroups.com>,
>
>
>
>
>
>  bH <bherbs...@hotmail.com> wrote:
> > I compared the output using one large image
> > 1600 x 1200 pixels cropped to 200 x 150 in both
> > programs, first program uses:
> > com.sun.image.codec.jpeg.JPEGCodec
> > and the second program with the revision as suggested
> > above by John Matthews:
> > uses the classes javax.imageio.ImageIO and
> > java.awt.image.AffineTransformOp.
>
> > The comparison result is that the suggested
> > revision by John Matthews comes in second place.
> > It does do crop it, but is less perfect of a
> > crop than the first method listed.
> > For instance in the second program,
> > the image is not as sharp and clear.
>
> Kudos for comparing. What interpolationType did you use?
>
> <http://docs.oracle.com/javase/7/docs/api/java/awt/image/AffineTransfo...>- Hide quoted text -
>
> - Show quoted text -

John,
After you read my code,as usual
happens here, I'll have to take all criticism
that I deserve. I hope that I have limited my
line lengths to enable to fit it in.

Hi Knute,
I have combined your work dated 12-31-2005
found in this link:
< //http://www.velocityreviews.com/forums/t148931-how-to-resize-a-jpg-
image-file.html >

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class DataScaleImageThmbNail extends JFrame
implements ActionListener {

private JButton dataTransferBtn =
new JButton("Make Thumbnail");
private JButton closeBtn = new JButton("Close");
private JPanel infoPanel = new JPanel();

JLabel NameOrNumberLabel =
new JLabel (" Enter Image Name or Number" );
JLabel ThmbNailWidth =
new JLabel (" Enter Width for Thumbnail" );
JLabel ThmbNailHeight =
new JLabel (" Enter Height for Thumbnail" );

JTextField namOrNum = new JTextField("EnterDataHere",10);
JTextField tfThumbNailWidth = new JTextField("200",10);
JTextField tfThumbNailHeight = new JTextField("150",10);
String alfaNumeric =" ";
String stringWidth = " ";
String stringHeight = " ";


DataScaleImageThmbNail() {
setTitle("ImgDataTest");

add(infoPanel,BorderLayout.CENTER);

JPanel infoPanel= new JPanel();

// TextArea for directions
infoPanel.add(new JTextArea(content));

// TextField for name of image
infoPanel.add(NameOrNumberLabel );
namOrNum = new JTextField("12A", 12);
infoPanel.add( namOrNum );
dataTransferBtn.addActionListener(this);

// TextField for width of thumbnail
infoPanel.add(ThmbNailWidth);
infoPanel.add (tfThumbNailWidth);
dataTransferBtn.addActionListener(this);

// TextField for height of thumbnail
infoPanel.add(ThmbNailHeight);
infoPanel.add (tfThumbNailHeight);
dataTransferBtn.addActionListener(this);

dataTransferBtn.addActionListener(this);
infoPanel.add(dataTransferBtn );

infoPanel.add(closeBtn);
closeBtn.addActionListener(this);

Container contentPane= getContentPane();
contentPane.add(infoPanel);

setSize(350,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);
}
static String content =
"Please place 2 folders on the desktop. \n"
+ " Name one 'LargeImageFolder' to put your large .jpg into it.
\n"
+ " then the second folder name it 'ThmnailFolder'. \n"
+ " Caution, each .jpg should have a unique 'name' \n"
+ " as examples '031', 'Bob2. \n"
+ " Make only one thumbnail at a time. \n"
+ " For each one, click the 'Make Thumbnail' button. \n"
+ " The .jpg thumbnails have the same name\n"
+ " as the original .jpg \n"
+ " You will find them in 'ThmnailFolder' \n"
+ " A 'Close' button is provided to close the program.";


public void actionPerformed(ActionEvent e) {

if(e.getSource() == dataTransferBtn){
alfaNumeric = namOrNum.getText();
//System.out.println(alfaNumeric);

stringWidth = tfThumbNailWidth.getText();
// System.out.println(stringWidth);


stringHeight = tfThumbNailHeight.getText();
// System.out.println(stringHeight );
ScaleImage2x (alfaNumeric, stringWidth, stringHeight ) ;
}
if(e.getSource() == closeBtn){
System.exit(0);
}

}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
DataScaleImageThmbNail DataScaleImageThmbNail
= new DataScaleImageThmbNail ();
}
});
}

public void ScaleImage2x (String alnumeric,
String stringWidth,String stringHeight) {

String srcFile= "C:/Users/Bob/Desktop/LargeImageFolder/"
+ alnumeric+ ".jpg";
String destFile= "C:/Users/Bob/Desktop/ThmnailFolder/"
+ alnumeric+ ".jpg";

int destWidth = Integer.parseInt(stringWidth);
int destHeight = Integer.parseInt(stringHeight);


try {
BufferedImage src = ImageIO.read(new File(srcFile));

BufferedImage dest = new BufferedImage(destWidth,destHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = dest.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance(
(double)destWidth/src.getWidth(),
(double)destHeight/src.getHeight());
g.drawRenderedImage(src,at);
ImageIO.write(dest,"JPG",new File(destFile));
//at.close();
}
catch (Exception e)
{
e.printStackTrace( System.out ) ;
}
}
}

bH

Knute Johnson

unread,
Dec 20, 2011, 11:43:41 PM12/20/11
to
On 12/20/2011 5:02 PM, bH wrote:
> Knute,
> Posting a sample image is something I have never
> done before. I would appreciated your guidance
> to enable me to do it.
> TIA
> bH

I don't know if you can post an image to this group, I never tried it.
You could put one on a website somewhere.

--

Knute Johnson

Knute Johnson

unread,
Dec 20, 2011, 11:47:35 PM12/20/11
to
I've learned some things since then :-). See the docs for
AffineTransformOP. I've found on many occasions that when the images
look bad after some Op that they need to be converted to compatible
images (matching color space).


public static BufferedImage scaleImage(BufferedImage src, double sx,
double sy, int interpolationType) {
AffineTransformOp op = new AffineTransformOp(
AffineTransform.getScaleInstance(sx,sy),interpolationType);
return op.filter(src,null);
}

public static BufferedImage scaleImage(BufferedImage src, double sx,
double sy, RenderingHints hints) {
AffineTransformOp op = new AffineTransformOp(
AffineTransform.getScaleInstance(sx,sy),hints);
return op.filter(src,null);
}

public static BufferedImage convertToCompatible(BufferedImage image) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();

BufferedImage compatible =
gc.createCompatibleImage(image.getWidth(),
image.getHeight());

if (compatible.getType() == image.getType())
return image;

ColorConvertOp op = new ColorConvertOp(
image.getColorModel().getColorSpace(),
compatible.getColorModel().getColorSpace(),null);

return op.filter(image,compatible);
}


--

Knute Johnson

bH

unread,
Dec 21, 2011, 4:58:11 PM12/21/11
to
> Knute Johnson- Hide quoted text -
>
> - Show quoted text -

Hi Knute,
I am in need of further help as to how and
where to place what you wrote above.
I have tried various locations within my work.
Nothing seems to help to make the resulting
thumbnail show any improvement.
TIA
bH

Knute Johnson

unread,
Dec 21, 2011, 7:09:15 PM12/21/11
to
On 12/21/2011 1:58 PM, bH wrote:
>
> Hi Knute,
> I am in need of further help as to how and
> where to place what you wrote above.
> I have tried various locations within my work.
> Nothing seems to help to make the resulting
> thumbnail show any improvement.
> TIA
> bH

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;

public class test extends JPanel implements ActionListener {
private final JFrame frame;
private final RenderingHints hints;
private BufferedImage image,thumb1,thumb2;

public test(JFrame frame) {
this.frame = frame;

hints = new RenderingHints(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
hints.add(new RenderingHints(
RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY));
hints.add(new RenderingHints(
RenderingHints.KEY_COLOR_RENDERING,
RenderingHints.VALUE_COLOR_RENDER_QUALITY));

try {
URL url =
new URL("http://rabbitbrush.frazmtn.com/saturn.jpg");
image = ImageIO.read(url);
setPreferredSize(
new Dimension(image.getWidth(),image.getHeight()));

thumb1 = scaleImage(image,.25,.25,
AffineTransformOp.TYPE_BICUBIC);
thumb2 = scaleImage(image,.25,.25,hints);

// if the image needs to have the colorspace converted
// image = convertToCompatible(image);
} catch (MalformedURLException murle) {
JOptionPane.showMessageDialog(this,murle);
} catch (IOException ioe) {
JOptionPane.showMessageDialog(this,ioe);
}
}

public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("url")) {
String input =
JOptionPane.showInputDialog(this,"Enter Image URL");
if (!input.equals("")) {
try {
URL url = new URL(input);
image = ImageIO.read(url);
setPreferredSize(
new Dimension(image.getWidth(),image.getHeight()));

thumb1 = scaleImage(image,.25,.25,
AffineTransformOp.TYPE_BICUBIC);
thumb2 = scaleImage(image,.25,.25,hints);

setPreferredSize(new Dimension(image.getWidth(),
image.getHeight()));
frame.pack();
repaint();
} catch (MalformedURLException murle) {
JOptionPane.showMessageDialog(this,murle);
} catch (IOException ioe) {
JOptionPane.showMessageDialog(this,ioe);
}
}
}
}

public void paintComponent(Graphics g) {
g.drawImage(image,0,0,null);
g.drawImage(thumb1,0,0,null);
g.drawImage(thumb2,getWidth()-thumb2.getWidth(),0,null);
}

public static BufferedImage scaleImage(BufferedImage src, double sx,
double sy, int interpolationType) {
AffineTransformOp op = new AffineTransformOp(
AffineTransform.getScaleInstance(sx,sy),interpolationType);
return op.filter(src,null);
}

public static BufferedImage scaleImage(BufferedImage src, double sx,
double sy, RenderingHints hints) {
AffineTransformOp op = new AffineTransformOp(
AffineTransform.getScaleInstance(sx,sy),hints);
return op.filter(src,null);
}

public static BufferedImage convertToCompatible(BufferedImage image) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();

BufferedImage compatible =
gc.createCompatibleImage(image.getWidth(),
image.getHeight());

if (compatible.getType() == image.getType())
return image;

ColorConvertOp op = new ColorConvertOp(
image.getColorModel().getColorSpace(),
compatible.getColorModel().getColorSpace(),null);

return op.filter(image,compatible);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

test t = new test(f);

JMenuBar mb = new JMenuBar();
f.setJMenuBar(mb);
JMenu file = new JMenu("file");
mb.add(file);
JMenuItem url = new JMenuItem("url");
url.addActionListener(t);
file.add(url);

f.add(t);
f.pack();
f.setVisible(true);
}
});
}
}

--

Knute Johnson

bH

unread,
Dec 21, 2011, 9:58:25 PM12/21/11
to
Hi Knute,
Thanks for your help. I have
experimented with your program and
find that the thumbnail images have no
noticeable distortion.

The first time I ran "test.java",
using your image from your URL
I thought the small planet drawings in the
upper corners were a part of the initial download
URL source. I should have read your "test.java"
more carefully.

However, when I replaced your URL, with my
localhost url, I then saw where the thumbnails
are placed.

Finally it was also clear in "test.java"
what you intended me to write from your earlier
help.

Thank you very much.

bH

Knute Johnson

unread,
Dec 21, 2011, 10:09:59 PM12/21/11
to
You are very welcome.

--

Knute Johnson
Message has been deleted

Joerg Meier

unread,
Dec 23, 2011, 8:18:14 PM12/23/11
to
On Sun, 18 Dec 2011 12:51:10 -0800, Roedy Green wrote:

> On Sat, 17 Dec 2011 19:03:44 -0800 (PST), bH <bher...@hotmail.com>
> wrote, quoted or indirectly quoted someone who said :
>>import com.sun.image.codec.jpeg.JPEGCodec;
>>import com.sun.image.codec.jpeg.JPEGEncodeParam;
>>import com.sun.image.codec.jpeg.JPEGImageEncoder;
> see http://mindprod.com/jgloss/jpegencoder.html

It really seems like you should provide a (BIG!) warning before listing
com.sun packages as an alternative, especially considering that Oracle is
finally moving the reference implementation to OpenJDK.

Liebe Gruesse,
Joerg

--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
0 new messages