Java Array Index Out Of Bounds Exception: -1 - java

Alright , I have a problem which I can't figure out whats wrong. So I have jList and List .
I need a function that when I click on item (in jList any item ) and it would change in my label icon , (I'm dealing with images).
It works somehow, it changes my label icon to the image I pick from jList, but it throws me Exception and the program crashes , usually first 2 items cause no errors , third and further items cause it. After it crashes and throws me bunch of red text , I can still change my icon.
This is the function where I get the images and add them to List (adding path to them)
private static void getImages(String src) throws IOException {
//Exctract the name of the image from the src attribute
int indexname = src.lastIndexOf("/");
if (indexname == src.length()) {
src = src.substring(1, indexname);
}
indexname = src.lastIndexOf("/");
String name = src.substring(indexname, src.length());
//Open a URL Stream
URL url = new URL(src);
InputStream in = url.openStream();
GPath=fPath+name;
OutputStream out = new BufferedOutputStream(new FileOutputStream( GPath));
//Im adding to the list string (link to image) here
imagesListN.add(GPath);
System.out.println("list size: "+imagesListN.size());
for (int b; (b = in.read()) != -1;) {
out.write(b);
}
out.close();
in.close();
}
It adds them normally . Yes I'm downloading them , that's why I want to see them once they are downloaded.
This is where I click on jList function.
list.addListSelectionListener(new ListSelectionListener() {
private int a;
#Override
public void valueChanged(ListSelectionEvent arg0) {
if (!arg0.getValueIsAdjusting()) {
String h;
int k;
k = list.getSelectedIndex();
System.out.println("List id: "+k);
a = Main.imagesListN.indexOf(k);
System.out.println("imagesListN id: "+a);
h = Main.imagesListN.get(k);
System.out.println("h : "+h);
ImageIcon img = new ImageIcon(h);
imageReview.setIcon(img);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
Here is the jList (name list) . Exception is at
a = Main.imagesListN.indexOf(k);
it gives me -1 , but the h = Main.imagesListN.get(k); gives me the link I need and gives it to ImageIcon img = new ImageIcon(h); and then imageReview.setIcon(img); . Label icon changes everytime when I click on item I need. Maybe it's not a = Main.imagesListN.indexOf(k); that I'm looking at , but something gives me -1.
Btw I'm excecuting everything in Thread.
`public class Crawler extends Thread {
Main main = new Main();
public void run(){
try {
main.download();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}`
nothing fancy here. Each functions are in there own class , getImages() is in main , listListener is in class Langas (Class where are all buttons,labels , etc. are created nothing else) and Thread well , Thread. Also it works fine after everything is downloaded , no exceptions . Error appears durring downloading proccess

indexOf api accepts Object as parameter, while get accepts Object as well as primitive types. So when you call get with primitive type element, it looks for element by an index which you may have found.
But when you do indexOf, you are searching for an object within your list and hence you get -1.

Related

Write a Java program that downloads the first 100 comics of the webcomic XKCD. Be sure to use https:// for all URLS

This is what I have so far, and I am having trouble downloading 1-100 comics starting at https://xkcd.com/1/ and I know I am supposed to be going to the source code for the website. However, I cant seem to figure out how to get all the first 100 comics into my designated file I set it to save to. For example, I want https://xkcd.com/1/(view-source:https://xkcd.com/1/), https://xkcd.com/2/(view-source:https://xkcd.com/2/), and all the way up to comic 100. I know the img src is at line 50, but once again I don't know how to approach it.
public static void main(String[] args) {
URL imgURL = null;
for (int web = 1; web <= 100; web++) {
try {
imgURL = new URL("https://imgs.xkcd.com/comics/barrel_cropped_(1).jpg");
InputStream stream = imgURL.openStream();
Files.copy(stream, Paths.get("file/WebComics" + web + ".png"));
System.out.println("Done!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error!");
}
}
}
}
Add jsoup library jar to your project, and then try this:
static void do_page(int id) throws IOException {
Document doc = Jsoup.connect("https://xkcd.com/" + id).get();
Elements imgs = doc.select("#comic img");
for (Element e: imgs) {
System.out.println(e.attr("src"));
}
}
Then call the do_page function in a loop:
for (int i = 1; i <= 100; i++) {
do_page(i);
}
Now, instead of printing it, you can use JSoup again to probably download the images like you see fit.

How to convert an image from 8-Bit to RGB in ImageJ2 using Java

I'm trying to use ImageJ2 directly from Java to create a binarised image coming from an input image.
A somewhat working version of my code looks like this:
final File file = new File("input.png");
try {
DefaultDataTypeService dataTypeService = new DefaultDataTypeService();
Dataset dataset = imageJ.dataset().open(file.getAbsolutePath());
Img inputImg = dataset.getImgPlus();
PluginInfo pluginInfo = imageJ.plugin().getPlugin(Binarize.class);
Binarize binarizeOp = (Binarize) pluginInfo.createInstance();
binarizeOp.setContext(imageJ.getContext());
binarizeOp.setChangeInput(true);
binarizeOp.setFillMaskBackground(true);
binarizeOp.setFillMaskForeground(true);
binarizeOp.setInputData(dataset);
binarizeOp.setInputMask(null);
binarizeOp.setMaskColor(Binarize.WHITE);
binarizeOp.setMaskPixels(Binarize.INSIDE);
binarizeOp.setThresholdEachPlane(false);
binarizeOp.setDefaultThresholdMethod();
binarizeOp.run();
dataset.rgbChange();
DefaultDatasetService defaultDatasetService = new DefaultDatasetService();
Img outputImg = dataset.getImgPlus();
outputImg = outputImg.factory().imgFactory(new UnsignedByteType()).create(outputImg,new UnsignedByteType());
Dataset outputDataset = defaultDatasetService.create(outputImg);
imageJ.dataset().save(outputDataset,"input_binary.png");
} catch (IOException e) {
e.printStackTrace();
} catch (InstantiableException e) {
e.printStackTrace();
} catch (IncompatibleTypeException e) {
e.printStackTrace();
}
Running this code I have the problem that "input_binary.png" will be completely black, a behaviour I can reproduce using the ImageJ client application.
What I need to do in the client is to change the image type from "8-bit Color" to "RGB-Color". But I can not figure out how to reproduce that in Java using the current version of the net.imagej library.
I know that it would be possible using the 1.x library but I would like to to it using the 2.x.
Any help would be greatly appreciated.
You're getting black images because of this:
outputImg = outputImg.factory().imgFactory(new UnsignedByteType()).create(outputImg,new UnsignedByteType());
Which is just copying the dimensionality of your source image, not its values.
A few other key points:
It's best practice to have your Contextual objects (e.g. Services) derived from the Context instead of manually constructed.
The Binarize command has a Dataset output so it's not necessary to go Dataset > ImgPlus > Dataset
If you do want to write the dataset out you need to convert from the BitType output by Binarize to one that's supported.
See below for an example of running Binarize, getting the output, converting it and writing it out. Hope that helps!
public static void main(String... args) {
final File file = new File("inpath.png");
final File out = new File("outpath.png");
// This is just sugar for the point of illustration.
// The purpose here is just to have access to a Context
ImageJ imagej = new ImageJ();
// Cache the context for future use.
Context context = imagej.getContext();
try {
// Use the context to get the services we want to ensure they are all
// properly initialized.
// If this was a Command these could all be #Parameters to be populated
// automatically.
DatasetService datasetService = context.getService(DatasetService.class);
CommandService commandService = context.getService(CommandService.class);
DatasetIOService datasetIOService =
context.getService(DatasetIOService.class);
Dataset input = datasetIOService.open(file.getAbsolutePath());
// Start the command
Future<CommandModule> future =
commandService.run(Binarize.class, true, "inputData", input);
// Get the command output
Dataset binarized = (Dataset) future.get().getOutput("outputMask");
// The output type is a binary image which, at the moment, needs to be
// explicitly converted to something that can be written out.
// Adapted from:
// http://fiji.sc/ImgLib2_Examples#Example_2c_-_Generic_copying_of_image_data
Img inputImg = input.getImgPlus().getImg();
Img outputImg = binarized.getImgPlus().getImg();
Img typedImg =
inputImg.factory().create(inputImg, inputImg.firstElement());
scale(outputImg, typedImg);
Dataset output = datasetService.create(typedImg);
// Save the output dataset
datasetIOService.save(output, out.getAbsolutePath());
}
catch (IOException exc) {
exc.printStackTrace();
}
catch (InterruptedException exc) {
exc.printStackTrace();
}
catch (ExecutionException exc) {
exc.printStackTrace();
}
finally {
// Dispose of the context to shut down
context.dispose();
}
}
public static <T extends IntegerType<T>> void scale(
final RandomAccessible<BitType> source, final IterableInterval<T> target)
{
// create a cursor that automatically localizes itself on every move
Cursor<T> targetCursor = target.localizingCursor();
RandomAccess<BitType> sourceRandomAccess = source.randomAccess();
// iterate over the input cursor
while (targetCursor.hasNext()) {\
// move input cursor forward
targetCursor.fwd();
// set the output cursor to the position of the input cursor
sourceRandomAccess.setPosition(targetCursor);
// set the value of this pixel of the output image
BitType b = sourceRandomAccess.get();
if (b.get()) {
targetCursor.get().setOne();
}
else {
targetCursor.get().setZero();
}
}
}

Compare image to actual screen

I'd like to make my Java program compare the actual screen with a picture (screenshot).
I don't know if it's possible, but I have seen it in Jitbit (a macro recorder) and I would like to implement it myself. (Maybe with that example you understand what I mean).
Thanks
----edit-----
In other words, is it possible to check if an image is showing in? To find and compare that pixels in the screen?
You may try aShot: documentation link
1) aShot can ignore areas you mark with special color.
2) aShot can provide image which display difference between images.
private void compareTowImages(BufferedImage expectedImage, BufferedImage actualImage) {
ImageDiffer imageDiffer = new ImageDiffer();
ImageDiff diff = imageDiffer
.withDiffMarkupPolicy(new PointsMarkupPolicy()
.withDiffColor(Color.YELLOW))
.withIgnoredColor(Color.MAGENTA)
.makeDiff(expectedImage, actualImage);
// areImagesDifferent will be true if images are different, false - images the same
boolean areImagesDifferent = diff.hasDiff();
if (areImagesDifferent) {
// code in case of failure
} else {
// Code in case of success
}
}
To save image with differences:
private void saveImage(BufferedImage image, String imageName) {
// Path where you are going to save image
String outputFilePath = String.format("target/%s.png", imageName);
File outputFile = new File(outputFilePath);
try {
ImageIO.write(image, "png", outputFile);
} catch (IOException e) {
// Some code in case of failure
}
}
You can do this in two steps:
Create a screenshot using awt.Robot
BufferedImage image = new Robot().createScreenCapture(new Rctangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("/screenshot.png"));
Compare the screenshots using something like that: How to check if two images are similar or not using openCV in java?
Have a look at Sikuli project. Their automation engine is based on image comparison.
I guess, internally they are still using OpenCV for calculating image similarity, but there are plenty of OpenCV Java bindings like this, which allow to do so from Java.
Project source code is located here: https://github.com/sikuli/sikuli
Ok then, so I found an answer after a few days.
This method takes the screenshot:
public static void takeScreenshot() {
try {
BufferedImage image = new Robot().createScreenCapture(new Rectangle(490,490,30,30));
/* this two first parameters are the initial X and Y coordinates. And the last ones are the increment of each axis*/
ImageIO.write(image, "png", new File("C:\\Example\\Folder\\capture.png"));
} catch (IOException e) {
e.printStackTrace();
} catch (HeadlessException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
}
}
And this other one will compare the images
public static String compareImage() throws Exception {
// savedImage is the image we want to look for in the new screenshot.
// Both must have the same width and height
String c1 = "savedImage";
String c2 = "capture";
BufferedInputStream in = new BufferedInputStream(new FileInputStream(c1
+ ".png"));
BufferedInputStream in1 = new BufferedInputStream(new FileInputStream(
c2 + ".png"));
int i, j;
int k = 1;
while (((i = in.read()) != -1) && ((j = in1.read()) != -1)) {
if (i != j) {
k = 0;
break;
}
}
in.close();
in1.close();
if (k == 1) {
System.out.println("Ok...");
return "Ok";
} else {
System.out.println("Fail ...");
return "Fail";
}
}

Using HtmlUnit to click on multiple images on same page that have same source attribute but different onclick events

I'm using HtmlUnit to get data from a webpage. The web page has multiple images that look the same and have the same src value but each one has a different onclick event that directs it to a new page depending on which image was clicked and it is this new page that I need to save the data from. I need to loop through the images and click each one to get the results of the onclick event. At the moment the code I have loops through the images but each file contains the output from the onclick of the first image.
Can someone point out where I am missing something please? My code is as follows:
customer in the file name is a variable that I have declared earlier in the code and i changes for each loop so that each file has a different name.
DomNodeList<DomElement> iterable2 = page.getElementsByTagName("img");
Iterator<DomElement> i3 = iterable2.iterator();
int i = 0;
while(i3.hasNext())
{
HtmlElement element1 = null;
DomElement anElement = i3.next();
if(anElement instanceof HtmlImage)
{
HtmlImage input = (HtmlImage) anElement;
if(input.getSrcAttribute().equalsIgnoreCase("customise.gif") )
{
element1 = input;
page2 = element1.click();
webClient.waitForBackgroundJavaScript(30000);
String result = page2.asText();
try {
BufferedWriter out = new BufferedWriter(new FileWriter("Filepath//"+customer+i+".txt"));
out.write(result);
out.close();
}
catch (IOException e)
{
System.out.println("Exception ");
}
i++;
}
}
}
I got this working by using a solution posted for a similar problem on HtmlUnit site which can be found here http://htmlunit.10904.n7.nabble.com/Problem-in-clicking-multiple-javaScript-links-on-a-page-td22682.html
Using the solution in the final posting on the thread in the link my code is now:
page = link3.click();
// Added the following line of code
Object page1Script = page.getEnclosingWindow().getScriptObject();
HtmlPage page2 = null;
Iterable<DomElement> iterable2 = page.getElementsByTagName("img");
Iterator<DomElement> i3 = iterable2.iterator();
int i = 0;
while(i3.hasNext())
{
// Added the following two lines of code
page.getEnclosingWindow().setEnclosedPage(page);
page.getEnclosingWindow().setScriptObject(page1Script);
HtmlElement element1 = null;
page2 = null;
DomElement anElement = i3.next();
if(anElement instanceof HtmlImage)
{
HtmlImage input = (HtmlImage) anElement;
if(input.getSrcAttribute().equalsIgnoreCase("customize.gif") )
{
element1 = input;
page2 = element1.click();
webClient.waitForBackgroundJavaScript(30000);
String result = page2.asText();
try {
BufferedWriter out = new BufferedWriter(new FileWriter("Filepath\\"+customer+i+".txt"));
out.write(result);
out.close();
}
catch (IOException e)
{
System.out.println("Exception ");
}
i++;
}
}
}

Why do I get the java.io.EOFException and the ArrayOutOfBounds error? Help?

I am trying to create a sort of simple GUI where im trying to save a couple of Strings and doubles and one int, im using the basic property of OOP which is inheritance I created a class Autos which is essentially the super Class.
The problem seem to arise in a method called "cargarDatosAutos" this is in my GUI class, and here is the code:
private void cargarDatosAutos()
{
regInt = at.numRegistros(); // number of registry
if (regInt != -1)
{
curInt = 0;
ats = new AutosRentables[regInt];
try
{
RandomAccessFile f = new RandomAccessFile("Autos.txt", "rw");
at.cargarDatos(f, ats, regInt); // method in subclass
f.close();
}
catch (IOException ex)
{
Logger.getLogger(Interfaz3.class.getName()).log(Level.SEVERE, null, ex);
}
this.mostrarAutos(ats[0]); // shows data
}
}
Here are the errors:
4-Dec-2011 11:35:20 PM rent_autos.Interfaz3 cargarDatosAutos
SEVERE: null
java.io.EOFException
at java.io.RandomAccessFile.readChar(RandomAccessFile.java:695)
at rent_autos.Autos.leerModelo(Autos.java:139)
at rent_autos.AutosRentables.cargarDatos(AutosRentables.java:84)
at rent_autos.Interfaz3.cargarDatosAutos(Interfaz3.java:6076)
at rent_autos.Interfaz3.<init>(Interfaz3.java:38)
at rent_autos.Interfaz3$159.run(Interfaz3.java:6107)
the leerModelo is a method that reads strings:
public String leerModelo(RandomAccessFile file) throws IOException
{
char cadena[] = new char[25], temp;
for (int c = 0; c < cadena.length; c++)
{
temp = file.readChar();
cadena[c] = temp;
}
return new String(cadena).replace('\0', ' ');
}
And the cargarDatos is to load my data:
public void cargarDatos(RandomAccessFile file, AutosRentables[] lista, int reg) throws IOException
{
int cont = 0;
do
{
modelo = this.leerModelo(file);
color = this.leerColor(file);
tipoAM = this.leerTipoAM(file);
rendimientoGalon = file.readDouble();
placa = this.leerPlaca(file);
ACRISS = this.leerACRISS(file);
codigo = file.readInt();
costo = file.readDouble();
marca = this.leerMarca(file);
detalles = this.leerDetalles(file);
lista[cont] = new AutosRentables(modelo, color, tipoAM, rendimientoGalon, placa, ACRISS, codigo, costo, marca, detalles);
cont++;
System.out.println("Entra");
}
while (cont < reg);
}
And heres the ArrayoutOfbound error:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at rent_autos.Interfaz3.cargarDatosAutos(Interfaz3.java:6081)
at rent_autos.Interfaz3.<init>(Interfaz3.java:38)
at rent_autos.Interfaz3$159.run(Interfaz3.java:6107)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
So if anyone knows whats going on please help me out here... is it the byte size of the file?, I really dont know, HELP!
EOFException means you tried to read past the end of the stream; i.e. the end of the file in this case. Probably you aren't positioning yourself correctly in the file. Reading chars from a random access file is tricky as you can't know how many bytes they are encoded as. I suspect you need to redesign the file actually. Or else you should be reading bytes not chars if it is coming from an external system?
java.io.EOFException:
For java.io.EOFException check this link http://docs.oracle.com/javase/1.4.2/docs/api/java/io/EOFException.html.
ArrayOutOfBounds:
Out of bounds exception is occurred when you try to access an array with index that exceeded its length. maximum index of a java array is (length -1). It means that you are trying to insert a value into an array element taht doesnt exist.
For handling it you should make sure that your program doesn't access an array with index bigger than length - 1.

Categories

Resources