Java Code Examples for javax.imageio.ImageIO.read()

The following are Jave code examples for showing how to use read() of the javax.imageio.ImageIO class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: osoon   File: UserFileService.java   View Source Code Vote up 6 votes
@Async
public String createThumbnail(UserFile userFile) {
    String path = userFile.getPath();
    Path originalImagePath = Paths.get(properties.getUploadFileRootPath(), path);

    try {
        BufferedImage originalImage = ImageIO.read(originalImagePath.toFile());
        if (originalImage != null) {
            BufferedImage thumbnailImage = this.createThumbnailImage(originalImage, 300, 200);
            String ext = path.substring(path.lastIndexOf(".") + 1);
            Path thumbNailPath = Paths.get(properties.getUploadFileRootPath(), userFile.getThumbnailPath());
            ImageIO.write(thumbnailImage, ext, Files.newOutputStream(thumbNailPath));
            return thumbNailPath.toString();
        }
    } catch (IOException e) {
        logger.error("Failed to create thumbnail of '{}'", path);
    }

    return "";
}
 
Example 2
Project: Hotel-Properties-Management-System   File: HotelPropertiesWindow.java   View Source Code Vote up 6 votes
private BufferedImage convertStringToBufferedImage(String image) {

        if (image != null) {
            final byte[] imageInByte = Base64.decodeBase64(image.getBytes());
            final InputStream in = new ByteArrayInputStream(imageInByte);
            BufferedImage bImageFromConvert = null;
            try {

                bImageFromConvert = ImageIO.read(in);

                return bImageFromConvert;
            } catch (IOException e) {
                JOptionPane.showMessageDialog(modalFrame, "Image conversation error!\nPlease reopen this window.",
                        JOptionPane.MESSAGE_PROPERTY, JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Image cannot be null!",
                    JOptionPane.MESSAGE_PROPERTY, JOptionPane.WARNING_MESSAGE);
        }
        return null;
    }
 
Example 3
Project: OOPIter2   File: TileDrawingVisitor.java   View Source Code Vote up 6 votes
@Override
public void visitArmy(Army army) {
	BufferedImage texture = null;
	if (!army.hasBattleGroup()){
	    return;
       }
	try {
		texture = ImageIO.read(new File("src/application/images/units/BlueArmy.png"));
	} catch (IOException e) {
           try {
               texture = ImageIO.read(new File("../src/application/images/units/BlueArmy.png"));
           } catch (IOException e2) {
               e2.printStackTrace();
           }		}
       texture = resizeImage(texture);
	addToPriorityQueue(ARMY, texture);
}
 
Example 4
Project: incubator-netbeans   File: IconEditor.java   View Source Code Vote up 6 votes
private NbImageIcon iconFromFileName(String fileName) {
    File file = new File(fileName);
    if (file.exists()) {
        try {
            try {
                Image image = ImageIO.read(file);
                if (image != null) {
                    return new NbImageIcon(TYPE_FILE, fileName, new ImageIcon(image));
                }
            } catch (IllegalArgumentException iaex) { // Issue 178906
                Logger.getLogger(IconEditor.class.getName()).log(Level.INFO, null, iaex);
                return new NbImageIcon(TYPE_FILE, fileName, new ImageIcon(fileName));
            }
        } catch (IOException ex) {
            Logger.getLogger(IconEditor.class.getName()).log(Level.INFO, null, ex);
        }
    }
    return null;
}
 
Example 5
Project: betaexpansion   File: TextureCompassFX.java   View Source Code Vote up 6 votes
public TextureCompassFX(Minecraft minecraft)
{
    super(Item.compass.getIconFromDamage(0));
    isCustom = true;
    mc = minecraft;
    res = (Integer)mc.renderEngine.textureSizeIdMap.get(mc.renderEngine.getTexture("/gui/items.png"))/16;
    compassIconImageData = new int[res*res];
    imageData = new byte[res*res*4];
    tileImage = 1;
    try
    {
        BufferedImage bufferedimage = ImageIO.read(mc.texturePackList.selectedTexturePack.getResourceAsStream("/gui/items.png"));
        int i = (iconIndex % 16) * res;
        int j = (iconIndex / 16) * res;
        bufferedimage.getRGB(i, j, res, res, compassIconImageData, 0, res);
    }
    catch(IOException ioexception)
    {
        ioexception.printStackTrace();
    }
}
 
Example 6
Project: raven   File: ImageUtils.java   View Source Code Vote up 6 votes
/**
 * 灏嗗浘鐗囦互width涓哄噯杩涜绛夋瘮缂╂斁
 * 
 * @param imageBytes
 * @param width
 * @param imageFormatName
 * @return
 */
public static byte[] scaleImageByWidth(byte[] imageBytes, int width,
		String imageFormatName) {
	Image img = null;
	int height = 0;
	try {
		img = ImageIO.read(new ByteArrayInputStream(imageBytes));
		int old_w = img.getWidth(null);
		int old_h = img.getHeight(null);
		if (old_h > 0 && old_w > 0) {
			// 浠ュ搴︿负鍑嗭紝璁$畻楂樺害 绛夋瘮缂╂斁
			height = (int) ((float) width / old_w * old_h);
		}
	} catch (Exception e) {
		logger.error("scaleImageByWidth faild!", e);
	}
	return ImageUtils.scaleImage(imageBytes, width, height,
			Image.SCALE_SMOOTH, imageFormatName);
}
 
Example 7
Project: Dr-Assistant   File: PatientCard.java   View Source Code Vote up 5 votes
private void showImage(Patient patient){
        if (patient.getThumbnail() == null) {
           imageView.setImage(new Image(getClass().getResource("/image/avater.jpg").toExternalForm()));
       } else {
           File file = new File(path + "\\Documents\\DrAssistant\\" + patient.getThumbnail());
           try {
               BufferedImage bufferedImage = ImageIO.read(file);
               Image image = SwingFXUtils.toFXImage(bufferedImage, null);
               imageView.setImage(image);
           } catch (IOException ex) {
               Logger.getLogger(PatientCard.class.getName()).log(Level.SEVERE, null, ex);
           }
       }
}
 
Example 8
Project: ScreenStealer   File: ScreenSpyServer.java   View Source Code Vote up 5 votes
@NotNull
private static BufferedImage readImage(int imageSize, InputStream inputStream) throws IOException {
    byte[] imageAr = new byte[imageSize];

    for (int i = 0; i < imageSize; i++) {
        imageAr[i] = (byte) inputStream.read();
    }

    return ImageIO.read(new ByteArrayInputStream(imageAr));
}
 
Example 9
Project: stock-api-sdk   File: DownSampleUtilTest.java   View Source Code Vote up 5 votes
@Test
void downSampleImageUtil_should_downSample_image_width_greater_than_1000_pixels()
        throws IOException, StockException{
    BufferedImage originalImage = ImageIO
            .read(new File("src/test/resources/OriginalImageWidth.jpg"));
    byte[] downSampledImage = DownSampleUtil.downSampleImageUtil(
            imageToByteArray(originalImage));
    ByteArrayInputStream bais = new ByteArrayInputStream(downSampledImage);
    BufferedImage downSampledImg =  ImageIO.read(bais);
    ImageIO.write(downSampledImg, "jpg", new File("src/test/resources/DownSampledImageWidth.jpg"));
    Assert.assertEquals(1000, downSampledImg.getWidth());
    Assert.assertEquals(954, downSampledImg.getHeight());
}
 
Example 10
Project: Backmemed   File: MinecraftServer.java   View Source Code Vote up 5 votes
public void applyServerIconToResponse(ServerStatusResponse response)
{
    File file1 = this.getFile("server-icon.png");

    if (!file1.exists())
    {
        file1 = this.getActiveAnvilConverter().getFile(this.getFolderName(), "icon.png");
    }

    if (file1.isFile())
    {
        ByteBuf bytebuf = Unpooled.buffer();

        try
        {
            BufferedImage bufferedimage = ImageIO.read(file1);
            Validate.validState(bufferedimage.getWidth() == 64, "Must be 64 pixels wide", new Object[0]);
            Validate.validState(bufferedimage.getHeight() == 64, "Must be 64 pixels high", new Object[0]);
            ImageIO.write(bufferedimage, "PNG", (OutputStream)(new ByteBufOutputStream(bytebuf)));
            ByteBuf bytebuf1 = Base64.encode(bytebuf);
            response.setFavicon("data:image/png;base64," + bytebuf1.toString(Charsets.UTF_8));
        }
        catch (Exception exception)
        {
            LOG.error((String)"Couldn\'t load server icon", (Throwable)exception);
        }
        finally
        {
            bytebuf.release();
        }
    }
}
 
Example 11
Project: ForgeHax   File: WaifuESP.java   View Source Code Vote up 5 votes
private BufferedImage getImageFromUrl(String link) {
    BufferedImage image = null;
    try {
        URL url = new URL(link);
        image = ImageIO.read(url);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return image;
}
 
Example 12
Project: ProjectAres   File: ImageUtils.java   View Source Code Vote up 5 votes
static byte[] thumbnail(Path path, int height) {
    try {
        final BufferedImage image = ImageIO.read(path.toFile());
        final BufferedImage thumb = resize(image, height * image.getWidth() / image.getHeight(), height);
        return png(thumb);
    } catch(IOException e) {
        throw Throwables.propagate(e);
    }
}
 
Example 13
Project: ZentrelaRPG   File: FallenAngelEffect.java   View Source Code Vote up 5 votes
public void loadFile(File file) {
    try {
        image = ImageIO.read(file);
    } catch (Exception ex) {
        ex.printStackTrace();
        image = null;
    }
}
 
Example 14
Project: openjdk-jdk10   File: NullStreamCheckTest.java   View Source Code Vote up 5 votes
private static void verifyFileRead() throws IOException {
    File inputTestFile = createTestFile("inputTestFile");
    try {
        ImageIO.read(inputTestFile);
    } catch (IOException ex) {
        if (verifyInputExceptionMessage(ex))
            throw ex;
    } finally {
        inputTestFile.delete();
    }
}
 
Example 15
Project: os   File: MatrixToImageWriterEx.java   View Source Code Vote up 5 votes
/**
	 * 将logo添加到二维码中间
	 * @author:chenming
	 * @date:2014年12月31日
	 *
	 * @param logoPath	logo路径
	 * @param image	需要绘制的二维码图片
	 * @param logoConfig	配置参数
	 * @throws java.io.IOException
	 */
	private static void drawImage(String logoPath,BufferedImage image,MatrixToLogoImageConfig logoConfig) throws IOException{
		if(logoConfig == null){
			logoConfig = DEFAULT_CONFIG;
		}
		
		try {
			BufferedImage logo = ImageIO.read(new File(logoPath));
			logo.setRGB(0, 0, BufferedImage.TYPE_INT_BGR);
			Graphics2D g = image.createGraphics();
			
			//考虑到logo照片贴到二维码中,建议大小不要超过二维码的1/5;
			int width = image.getWidth() / logoConfig.getLogoPart();
			int height = image.getHeight() / logoConfig.getLogoPart();
			
			//logo起始位置,此目的是为logo居中显示
			int x = (image.getWidth() - width) / 2;
			int y = (image.getHeight() - height) / 2;
			
			//绘制图
			g.drawImage(logo, x, y, width, height, null);
			
			//给logo画边框
			//构造一个具有指定线条宽度以及 cap 和 join 风格的默认值的实心 BasicStroke
//		g.setStroke(new BasicStroke(logoConfig.getBorder()));
//		g.setColor(logoConfig.getBorderColor());
//		g.drawRect(x, y, width, height);
			
			g.dispose();
		} catch (Exception e) {   //捕捉异常后不做任何处理,防止图片路径错误而导致二维码生成失败
			
		}
	}
 
Example 16
Project: geomapapp   File: ImageComponent.java   View Source Code Vote up 5 votes
public void open(File file) throws IOException {
	BufferedImage image = ImageIO.read( file );
	if(image==null)return;
	this.image = image;
	width = image.getWidth();
	height = image.getHeight();
	this.file = file;
	repaint();
}
 
Example 17
Project: Cognizant-Intelligent-Test-Scripter   File: AboutUI.java   View Source Code Vote up 5 votes
private void setImage() {
    try {
        BufferedImage img = ImageIO.read(getClass().getResource("/ui/resources/splash.png"));
        Image dimg = img.getScaledInstance(400, img.getHeight() - 50, Image.SCALE_SMOOTH);
        iconLabel.setIcon(new javax.swing.ImageIcon(dimg));
    } catch (IOException ex) {
        Logger.getLogger(AboutUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example 18
Project: OpenJSharp   File: ImagePrinter.java   View Source Code Vote up 4 votes
ImagePrinter(InputStream stream) {
    try {
        image = ImageIO.read(stream);
    } catch (Exception e) {
    }
}
 
Example 19
Project: PekaED   File: PK2Sprite.java   View Source Code Vote up 4 votes
private void loadBufferedImage() {
	try {
		BufferedImage tilesheet = ImageIO.read(new File(Settings.SPRITE_PATH + cleanString(imageFile)));
		
		try {
			image = tilesheet.getSubimage(frameX, frameY, frameWidth, frameHeight);
		} catch (Exception ex) {
			System.out.println(filename + " - " + frameX + ", " + frameY + " - " + frameWidth + " - " + frameHeight);
		}
			
		BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
		 
	    // make color transparent
	    int oldRGB = new Color(148, 209, 222).getRGB();
	    int oldRGB2 = new Color(128, 205, 214).getRGB();
	    int oldRGB3 = new Color(155, 232, 224).getRGB();
	    int oldRGB4 = new Color(114, 200, 228).getRGB();
	 
	    for (int i = 0; i < image.getWidth(); i++) {
	    	for (int j = 0; j < image.getHeight(); j++) {
	    		if (image.getRGB(i, j) != oldRGB && image.getRGB(i, j) != oldRGB2 && image.getRGB(i, j) != oldRGB3 && image.getRGB(i, j) != oldRGB4) {
	    			result.setRGB(i, j, image.getRGB(i, j));
	    		}
	    	}
	    }
	    
	   if (color != 255) {
		  /*
		   * if (this->vari != VARI_NORMAALI){ //Change sprite colors
				PisteDraw2_Image_GetSize(bufferi,w,h);
		
				PisteDraw2_DrawImage_Start(bufferi,*&buffer,leveys);
		
				for (x=0;x<w;x++)
					for (y=0;y<h;y++)
						if ((vari = buffer[x+y*leveys]) != 255){
							vari %= 32;
							vari += this->vari;
							buffer[x+y*leveys] = vari;
						}
		
				PisteDraw2_DrawImage_End(bufferi);
			}
			
			TODO Tint sprite
		   */
	   }
	    
	    image = result;
	} catch (IOException e) {
		//e.printStackTrace();
	}
}
 
Example 20
Project: open-rmbt   File: ImageExport.java   View Source Code Vote up 4 votes
@Override
public BufferedImage generateImage(String lang, double upload, double download, double ping, String isp, String typ, String signal, String os) throws IOException{            
    String unknownString = (lang.equals("de")) ? "unbekannt" : "unknown";
    BufferedImage img = new BufferedImage(600, 200, BufferedImage.TYPE_INT_ARGB);  
    img.createGraphics();  
    Graphics2D g = (Graphics2D)img.getGraphics(); 
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    BufferedImage img2 = ImageIO.read(getClass().getResourceAsStream("forumbanner_" + lang + ".png"));
    g.drawImage(img2, null, 0, 0);

    //Speeds
    g.setColor(Color.black);
    g.setFont(new Font("Droid Sans", Font.BOLD, 60));
    g.drawString(formatNumber(download, lang), 30, 105);
    g.drawString(formatNumber(upload, lang), 230, 105);
    g.drawString(formatNumber(ping, lang), 440, 105);


    //ISP and other information
    g.setColor(Color.WHITE);
    g.setFont(new Font("Verdana", Font.BOLD,16));

    //de
    if (lang.equals("de")) {
        //left
        g.drawString((typ == null)?unknownString:typ, 110, 168);
        g.drawString((isp == null)?unknownString:isp, 110, 191);

        //right
        g.drawString((signal==null)?unknownString:signal + " dBm", 410, 168);
        g.drawString((os==null)?unknownString:os, 410, 191);
    } 
    else { //en
        //left
        g.drawString((typ == null)?unknownString:typ, 130, 168);
        g.drawString((isp == null)?unknownString:isp, 90, 191);

        //right
        g.drawString((signal==null)?unknownString:signal + " dBm", 445, 168);
        g.drawString((os==null)?unknownString:os, 445, 191);
    }
    
    return img;
}