• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP int rounds to a whole number  RSS feed

 
Ma New
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="javax.imageio.*"%>
<%@ page import="java.awt.*"%>
<%@ page import="java.awt.Graphics2D"%>
<%@ page import="java.awt.image.BufferedImage"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String dir = "/Users/matt/NetBeansProjects/web/";
File orig_file = new File(dir + "PIC-0263.jpg");

BufferedImage img_size = ImageIO.read(orig_file);

int img_width = img_size.getWidth();
int img_height = img_size.getHeight();

int factor = img_width / 610;

int scaled_width = 610;
int scaled_height = img_height / factor;
...
%>

The image file "PIC-0263.jpg" is 1600x1200 I am trying to scale it so the width is 610 pixels and then calculate the height proportion with the int factor. It should calculate out to 2.66666666667 but it rounds it to 2. When it divides the scaled_height by 2 it equals 600 which does not retain the original proportions of the image. Ultimate I get an output of the image at 610x600. If anyone knows how to correct this please let me know. Thank you
 
Gopikrishna Kunisetty
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ma New,

Change the data type of that variable to float or double to get the desired result.
 
Ma New
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It still gives me a whole number. Instead of 2 it gives me 2.0 when I try both a double and a float
 
Ma New
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fixed it...!!!


BufferedImage img_size = ImageIO.read(orig_file);

int img_width = img_size.getWidth();
int img_height = img_size.getHeight();
float i_width = (float) img_width;
float factor = i_width / 610;

int scaled_width = 610;
float s_height = img_height / factor;
int scaled_height = (int) s_height;
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!