Reading and writing java image files

Read the BMP file into bufferedimage. File File2 = new file ("C: \ \ testimages \ \ tttt" + ". BMP")// BufferedImage bi = backstore. getBufferedImage(); try { output = ImageIO.read(file2); } catch (IOException e) { e.printStackTrace(); } Output BMP file File2 = new file ("C: \ \ testimages \ \ tttt" + ". BMP"); ImageIO. write(bi,"bmp",file2); Byte [] output to file byte [] buf = utilzip zipObjectToByte(cache); File file2 = new File("c:\\testimages\\cache.bin"); FileOutputStream fos =new FileOutputStream(file2); fos. write(buf); fos. flush(); fos. close(); Read the file into byte [] file File2 = new file ("C: \ \ testimages \ \ cache. Bin"); FileInputStream fis = new FileInputStream(file2); byte[] buf = new byte[(int) file2.length()]; fis. read(buf); fis. close(); Fill color to the entire canvas bufferedimage Bi = backstore getBufferedImage(); Graphics g2 = bi. getGraphics(); g2. setColor(Color.red); g2. fillRect(0,Common.width, Common.height); Image graying operation public finalbufferedimage getgraypicture (bufferedimage originalpic) {int imagewidth = originalpic. Getwidth(); int imageheight = originalpic. Getheight(); bufferedimage newpic = new bufferedimage (imagewidth, imageheight, bufferedimage. Type_3byte_bgr); colorconvertop CCO = new colorconvertop (colorspace. GetInstance) (ColorSpace.CS_GRAY),null); cco. filter(originalPic,newPic); return newPic; } ImageIO javax. imageio. ImageIO lets you save and restore Images to disk in a platform independent format. It works using plug-in modules that handle varIoUs formats including "gif","png" and "jpeg" (all lower case,or all upper case,but not mixed). "jpeg" or "jpg" is acceptable. Use ImageIO. getWriterFormatNames() to find out which types are supported on your platform: import javax. imageio. ImageIO; public class Jai { public static void main ( String[] args ) { String[] names = ImageIO.getWriterFormatNames(); for ( String name: names ) { System.out.println( name ); } } }

Saving an Image to raw bytes // BufferedImage to raw bytes import javax. imageio. ImageIO; import java. awt. image. BufferedImage; import java. io. ByteArrayOutputStream; ... // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // W R I T E ImageIO. write( aBufferedImage,"jpeg", baos ); // C L O S E baos. flush(); byte[] resultImageAsRawBytes = baos. toByteArray(); baos. close(); Loading a BufferedImage from a file // file to BufferedImage import java. awt. image. BufferedImage; import java. io. File; import javax. imageio. ImageIO; ... BufferedImage image = ImageIO. read( new File( "rabbit.jpg" ) ); Saving a BufferedImage to a file // BufferedImage to File import javax. imageio. ImageIO; import java. awt. image. BufferedImage; import java. io. File; ... ImageIO. write( aBufferedImage, new File ( "snap.jpg" ) ); ImageWriteParam is a way of controlling exactly how the image in encoded. There is currently no PNG support for it. This is not for injecting Meta info. Loading a BufferedImage from an URL // url to BufferedImage import java. awt. image. BufferedImage; import javax. imageio. ImageIO; ... BufferedImage image = null; try { image = ImageIO.read( url ); } catch ( IOException e ) { System.out.println( "image missing" ); }

Converting Image to BufferedImage // Image to BufferedImage import java. awt. image. BufferedImage; import java. awt. Image; ... BufferedImage bufferedImage = new BufferedImage ( imageWidth, imageHeight, BufferedImage.TYPE_INT_BGR ); bufferedImage. createGraphics(). drawImage( image,this );

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>