public BufferedImage rotateImage(BufferedImage rotateImage) { AffineTransformOp op = null; try { AffineTransform tx = new AffineTransform(); //Rotate 90ยบ tx.rotate(Math.toRadians(90), rotateImage.getWidth() / 2.0, rotateImage.getHeight() / 2.0); AffineTransform translationTransform; translationTransform = findTranslation(tx, rotateImage); tx.preConcatenate(translationTransform); op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); } catch (Exception e) { //Do something here } return op.filter(rotateImage, null); }
Wednesday, December 22, 2010
Rotate and translate images with AffineTransform
Staying on the same subject as my previous posts, the next example will help you rotate and translate any BufferedImage.
Labels:
Functions
Thursday, December 16, 2010
How to convert images
javax.imageio.ImageIO let's you convert and save images from one format into another.It works using plug-in modules that handle various formats, including gif, png, bmp, jpg and others. We can use getWriterFormatNames() to find out all supported types.
Next, let's load our image into a BufferedImage which is a subclass of Image class:
Finally convert the image:
In this example, the image file bmp was converted into a jpg image file.
String[] types = ImageIO.getWriterFormatNames(); for (String name: types){ System.out.println(name); }
Next, let's load our image into a BufferedImage which is a subclass of Image class:
File inputFile = new File("1.bmp"); BufferedImage image = ImageIO.read(inputFile);
Finally convert the image:
File outputFile = new File("1.jpg"); ImageIO.write(image, "jpg", outputFile);
In this example, the image file bmp was converted into a jpg image file.
Labels:
Functions
Wednesday, December 15, 2010
How to resize Buffered Images
When we need to resize or scale images in Java, the Java2D Team has been encouraging developers to move away from Image.getScaledInstance() to more updated APIs. This is exactly what I tried to do. So, if you are in need of a fairly good function that resizes images, look no further :)
Aside note: Some of my co-workers asked me about AffineTransform.scale() when performing scaled functions. Well, there is nothing wrong in using AffineTransform.Here's an example:
But in a majority of cases, there is a better approach. Simply use the scaling variant of Graphics.drawImage()
private BufferedImage resizedImage(BufferedImage oriImage) { int type = oriImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : oriImage.getType(); BufferedImage scaledImage = new BufferedImage((int) scaleWidth, (int) scaleHeight, type); // Paint scaled version of image to new image Graphics2D graphics2D = scaledImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(oriImage, 0, 0, (int) scaleWidth, (int) scaleHeight, null); // Clean up graphics2D.dispose(); //Return scaled image return scaledImage; }
Aside note: Some of my co-workers asked me about AffineTransform.scale() when performing scaled functions. Well, there is nothing wrong in using AffineTransform.Here's an example:
AffineTransform xform = AffineTransform.getScaleInstance(2, 2); graphics2D.drawImage(img, xform, null);
But in a majority of cases, there is a better approach. Simply use the scaling variant of Graphics.drawImage()
graphics2D.drawImage(oriImage, 0, 0, oriImage.getWidth()*2, oriImage.getHeight()*2, null);
Labels:
Functions
Friday, December 10, 2010
Reading a file into a Byte Array
To read a file (image file) into a Byte Array all we need to is copy the next function into your source code.
I tweaked it to fit my needs, however it is not entirely mine. I found it by goggling a while ago (don't remember when) and most of the credits will have to go to the creator (sorry... don't know who you are either, because the page where I found this function didn't have anything regarding the creator).
Labels:
Functions
Tuesday, December 7, 2010
JComboBox Text Alignment
Today someone asked me how can we make the text in a JComboBox align to the right instead of the left? Here's your answer :)
Import this,
DefaultListCellRenderer dlcr = new DefaultListCellRenderer(); //DefaultListCellRenderer.LEFT; //DefaultListCellRenderer.CENTER; //DefaultListCellRenderer.RIGHT; dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT); jComboBox1.setRenderer(dlcr);
Import this,
import javax.swing.DefaultListCellRenderer;and your done.
Labels:
Swing Components
Monday, December 6, 2010
How to set an Application Icon in Swing
To set an application icon simply paste this code in you application:
Swing components, such as labels and buttons can be decorated with an icon by using the Icon interface: IconImage.
InputStream icon = this.getClass().getResourceAsStream("MyIcon.gif"); if (icon != null) { BufferedImage image = ImageIO.read(icon); setIconImage(image); }
Swing components, such as labels and buttons can be decorated with an icon by using the Icon interface: IconImage.
Labels:
Swing Components
Thursday, December 2, 2010
Oracle and Apple Announce OpenJDK Project for Mac OS X
This may be old news, but apparently Oracle and Apple announced the OpenJDK project for Mac OS X.
“We are excited to welcome Apple as a significant contributor in the growing OpenJDK community,” said Hasan Rizvi, Oracle’s senior vice president of Development. Personally I'm more eager to see how Oracle, Apple and the JAVA community will handle the issue of Apple's use of private API in their JAVA development.
Let's just hope that Apple's implementation doesn't end up like Microsoft's :(
For more information on JAVA and on the subject, visit:
“We are excited to welcome Apple as a significant contributor in the growing OpenJDK community,” said Hasan Rizvi, Oracle’s senior vice president of Development. Personally I'm more eager to see how Oracle, Apple and the JAVA community will handle the issue of Apple's use of private API in their JAVA development.
Let's just hope that Apple's implementation doesn't end up like Microsoft's :(
For more information on JAVA and on the subject, visit:
Labels:
News
Friday, November 26, 2010
Working with JTables
With JTables you can display tables of data, allowing the user to edit the data. Fallow this example if you want to learn how to fill a specific position in a JTable.
- Create a new JDialog Form.
- Drag and drop the JTable swing component into the JDialog Form. You should have something like this,
Labels:
Swing Components
Tuesday, November 16, 2010
Thursday, November 11, 2010
How to sign JAR files
Since JAR files need to be downloaded as part of a Java applet you should digitally sign them. Before we can start make sure you have Java SDK keytool and jarsigner in your path. You can find these tools in Java SDK bin directory.
Create a new key,
When asked, fill the information regarding the new key name, password, etc. This procedure creates myKeystore on you disk.
Create a self signed certificate,
List the contents of the keystore (not mandatory),
Keystore type: jks
Keystore provider: SUN
Your keystore contains 1 entry:
myself, Fri Nov 12 19:29:32 PST 2010, keyEntry,
Certificate fingerprint (MD5):
C2:E9:BF:F9:D3:DF:4C:8F:3C:5F:22:9E:AF:0B:42:9D
Repeat these last step to sign all your JAR files,
Create a new key,
- keytool -genkey -keystore myKeystore -alias myself
When asked, fill the information regarding the new key name, password, etc. This procedure creates myKeystore on you disk.
Create a self signed certificate,
- keytool -selfcert -alias myself -keystore myKeystore
List the contents of the keystore (not mandatory),
- keytool -list -keystore myKeystore
Keystore type: jks
Keystore provider: SUN
Your keystore contains 1 entry:
myself, Fri Nov 12 19:29:32 PST 2010, keyEntry,
Certificate fingerprint (MD5):
C2:E9:BF:F9:D3:DF:4C:8F:3C:5F:22:9E:AF:0B:42:9D
Repeat these last step to sign all your JAR files,
- jarsigner -keystore myKeystore test.jar myself
Labels:
Digital Signatures
Read certificates from windows store
If you are on Java 6, you can use the MSCAPI keystore to read it. Just open your keystore like this,
KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI"); ks.load(null, null); java.util.Enumeration en = ks.aliases(); while (en.hasMoreElements()) { String aliasKey = (String) en.nextElement(); Certificate[] chain = ks.getCertificateChain(aliasKey); X509Certificate x509 = ((X509Certificate) chain[0]); boolean keyUsage[] = x509.getKeyUsage(); //keyUsage[0] -> digitalSignature //keyUsage[1] -> nonRepudiation //keyUsage[2] -> keyEncipherment //keyUsage[3] -> dataEncipherment //keyUsage[4] -> keyAgreement //keyUsage[5] -> keyCertSign //keyUsage[6] -> cRLSign //keyUsage[7] -> encipherOnly //keyUsage[8] -> decipherOnly }
To load windows root certificates simple replace the first line (highlighted) with this,
KeyStore ks = KeyStore.getInstance("Windows-MY", "Windows-ROOT");
To know more about the subject please read this
Labels:
Digital Signatures
Tuesday, November 9, 2010
How to sign a PDF using iText and iTextSharp
iText supports visible and invisible signing using the following modes:
Signing is done with this simple code:
- Self signed (Adobe.PPKLite)
- VeriSign plug-in (VeriSign.PPKVS)
- Windows Certificate Security (Adobe.PPKMS)
Signing is done with this simple code:
KeyStore ks = KeyStore.getInstance("pkcs12"); ks.load(new FileInputStream("my_private_key.pfx"), "my_password".toCharArray()); String alias = (String)ks.aliases().nextElement(); PrivateKey key = (PrivateKey)ks.getKey(alias, "my_password".toCharArray()); Certificate[] chain = ks.getCertificateChain(alias); PdfReader reader = new PdfReader("original.pdf"); FileOutputStream fout = new FileOutputStream("signed.pdf"); PdfStamper stp = PdfStamper.createSignature(reader,fout,'\0'); PdfSignatureAppearance sap = stp.getSignatureAppearance(); sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED); sap.setReason("I'm the author"); sap.setLocation("Lisbon"); sap.setVisibleSignature(new Rectangle(50,50,200,200),1,null); stp.close();
Labels:
Digital Signatures
Subscribe to:
Posts (Atom)