Change the size of several images
in
Programming Questions
•
2 years ago
Hi all ,
Am making a program that change the size of 28 images according to value given by a user ,the idea of the program is , every time the program asks the user to input anew value to change the size for the current image.The thing is every time I entered a new value for each image and then save these images , then when I check the size of the images after save them I notice the size of all the images are the same according to the latest value I have entered . Please I need an idea about how to change the size of each single image according to a given value every time.
here is my code
Am making a program that change the size of 28 images according to value given by a user ,the idea of the program is , every time the program asks the user to input anew value to change the size for the current image.The thing is every time I entered a new value for each image and then save these images , then when I check the size of the images after save them I notice the size of all the images are the same according to the latest value I have entered . Please I need an idea about how to change the size of each single image according to a given value every time.
here is my code
- /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package migpackage;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
import processing.core.PApplet;
import processing.core.PImage;
/**
*
* @author tamara
*/
public class MarkerImageGenerator extends PApplet {
// ArrayList<TableRecord>csvInputTableRecords= new ArrayList <TableRecord>();
ArrayList <PImage> images = new ArrayList<PImage>();
ArrayList<String> arraylistofImages= new ArrayList<String>();
ArrayList <PImage> markers = new ArrayList<PImage>();
ArrayList<String> arraylistofMarkers= new ArrayList<String>();
public int index=0;
public float cm2;
public float cm;
public int markerWidth;
public int markerHeight;
Scanner input = new Scanner(System.in);
public float pixelPerCmOfWidth;
public float pixelPerCmOfHeight;
public static void main(String[] args)
{
PApplet.main(new String[]{"--present", "migpackage.MarkerImageGenerator"});
// ArrayList<TableRecord>csvInputTableRecords= new ArrayList <TableRecord>();
// TODO code application logic here
}
private StringTokenizer st;
private StringTokenizer st1;
private ArrayList<TableRecord> csvInputTableRecords = new ArrayList<TableRecord> ();
private ArrayList<MarkerId> csvInputMarkerIds = new ArrayList<MarkerId> ();
@Override
public void setup()
{
frameRate(1);
loadCsv("C:\\Documents and Settings\\tamara\\My Documents\\NetBeansProjects\\MIGPackage\\src\\migpackage\\csv\\test4.txt");
String []imgs =getImageFilenames(csvInputTableRecords);
for (int i = 0 ; i < imgs.length ; i++)
{
images.add(loadImage("C:\\Documents and Settings\\tamara\\My Documents\\NetBeansProjects\\MIGPackage\\src\\migpackage\\images\\"+imgs[i]));
}
System.out.println("images size:"+ images.size());
loadCsv2("C:\\Documents and Settings\\tamara\\My Documents\\NetBeansProjects\\MIGPackage\\src\\migpackage\\csv2\\test7.txt");
String []marks =getMarkerImageFilenames(csvInputMarkerIds);
for (int j = 0 ; j < marks.length ; j++)
{
markers.add(loadImage("C:\\Documents and Settings\\tamara\\My Documents\\NetBeansProjects\\MIGPackage\\src\\migpackage\\markers\\"+marks[j]));
}
for( int i = 0 ; i < markers.size(); i ++)
{
System.out.println("marker no:" + markers.size());
markerWidth = markers.get(i).width;
markerHeight = markers.get(i).height;
println("The size of the marker before scaling is" + markerWidth + " " + markerHeight);
float pixelPerCmOfWidth = (100f / 2.54f);
float pixelPerCmOfHeight = (100f / 2.54f);
println("the size of the marker per cm is " + pixelPerCmOfWidth + " " + pixelPerCmOfHeight);
//println("the size of the marker per cm is " + pixelPerCmOfWidth + " " + pixelPerCmOfHeight);
System.out.print("Enter the size");
float size1 = input.nextFloat();
System.out.print("Enter the width");
cm = size1 *pixelPerCmOfHeight;
cm2 = size1*pixelPerCmOfWidth;
scale(cm,cm2);
println(cm + " " + cm2);
size((int)cm, (int)cm2);
}
/*println("The size of the marker before scaling is" + markerWidth + " " + markerHeight);
float pixelPerCmOfWidth = (100f / 2.54f);
float pixelPerCmOfHeight = (100f / 2.54f);
println("the size of the marker per cm is " + pixelPerCmOfWidth + " " + pixelPerCmOfHeight);
System.out.print("Enter the size of an image");
float size1 = input.nextFloat();
cm = size1 * pixelPerCmOfWidth;
cm2 = size1 * pixelPerCmOfHeight;
scale(cm,cm2);
println(cm + " " + cm2);
size((int)cm, (int)cm2);*/
}
void loadCsv(String filename) //throws IOException
{
try
{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
// declare String variable and prime the read
String read = br.readLine( );
while( read != null ) // end of the file?
{
st = new StringTokenizer( read, "," );
//We will check for unique items and add to the arraylist.
// println(sc);
// Get our scene names
String sc= st.nextToken();
String cond = st.nextToken( );
String img = st.nextToken( );
// String marker = st.nextToken( );
try
{
TableRecord trTemp = new TableRecord(sc,cond,img);
csvInputTableRecords.add(trTemp);
}
catch( NumberFormatException nfe )
{
System.out.println( "Error in table record: "
+ read
+ "; record ignored");
} //end of catch
// read the next line
read = br.readLine( );
}// release resources associated with "flights.txt"
br.close( );
}//end of try
catch( FileNotFoundException fnfe )
{
System.out.println( "Unable to find test4.txt" );
}
catch(IOException ioe)
{
ioe.printStackTrace( );
}
}
String [] getImageFilenames(ArrayList <TableRecord> firstFile)
{
for( int i =0 ; i < firstFile.size() ; i++)
{
arraylistofImages.add(firstFile.get(i).getImgName());
}
String tempImage[] =new String[arraylistofImages.size()];
arraylistofImages.toArray(tempImage);
return tempImage;
}
void loadCsv2(String filename1) //throws IOException
{
try
{
FileReader fr1 = new FileReader(filename1);
BufferedReader br1 = new BufferedReader(fr1);
// declare String variable and prime the read
String read1 = br1.readLine( );
// System.out.println(read1);
while( read1 != null ) // end of the file?
{
st1 = new StringTokenizer( read1, "," );
System.out.println(st1.countTokens());
//We will check for unique items and add to the arraylist.
String mk = new String();
String patt = new String();
// println(sc);
while (st1.hasMoreElements())
{
mk = st1.nextToken();
patt = st1.nextToken( );
}
try
{
MarkerId trTemp1 = new MarkerId(mk,patt);
csvInputMarkerIds.add(trTemp1);
}
catch( NumberFormatException nfe )
{
System.out.println( "Error in table record: "
+ read1
+ "; record ignored");
} //end of catch
// read the next line
read1 = br1.readLine( );
}// release resources associated with "flights.txt"
br1.close( );
}//end of try
catch( FileNotFoundException fnfe )
{
System.out.println( "Unable to find test7.txt" );
}
catch(IOException ioe)
{
ioe.printStackTrace( );
}
}
String [] getMarkerImageFilenames(ArrayList <MarkerId> firstFile1)
{
for( int i =0 ; i < firstFile1.size() ; i++)
{
arraylistofMarkers.add(firstFile1.get(i).getMarker());
}
String tempMarker[] =new String[arraylistofMarkers.size()];
arraylistofMarkers.toArray(tempMarker);
return tempMarker;
}
@Override
public void draw()
{
System.out.println(markers.size());
if(index<markers.size())
{
System.out.println("hello");
;
image(markers.get(index), 0, 0,cm, cm2);
save("image_name" + index + ".png");
index++;
}
}
}// </editor-fold>// </editor-fold>
1