java


ArrayIndexOutOf BoundsException while trying to read pixels from an image


i am getting an ArrayIndexoutOfBoundsException while trying to read pixels from image . the code returns the pixel values for some images and not for other images..i tried searching the net, and most of them referred to reading from 0 to n...
The code is given below..any help would be truly appreciated..
i tried writing saving the image in bi,and it gets saved..so bi is never getting null value..
and my image dimension is always 125*150..
i tried printing the values in inputFace,but in those images that doesn't give pixel values,i am not even getting any output while printing...
Doesn't array get initialized with 0 as soon as memory is allocated??
And thanks in advance
private double[] getImageData(String imageFileName) {
BufferedImage bi = null;
double[] inputFace = null;
try{
bi = ImageIO.read(new File(imageFileName));
}catch(IOException ioe){
ioe.printStackTrace();
}
if (bi != null){
int imageWidth = bi.getWidth();
int imageHeight = bi.getHeight();
inputFace = new double[imageWidth * imageHeight];
bi.getData().getPixels(0, 0, imageWidth, imageHeight,inputFace);
}
else
{
System.out.println("Null in bi");
}
return inputFace;
}
You aren't taking into account the number of bands per pixel - each pixel is made up of a number of bands (channels, e.g. Red, Green, Blue, Alpha for images of TYPE_INT_ARGB) depending on the image type. The array you're allocating needs to be (pixel width * pixel height * number of bands) in size:
int numBands = bi.getData().getNumBands();
inputFace = new double[imageWidth * imageHeight * numBands];
This will give you an array containing all the values for each channel of each pixel.

Related Links

difference between --executor-memory 12G and --executor-memory 8G --conf spark.yarn.memoryoverhead 4G
Where is the entry count for an object stored in Java
Cannot get method annotation from JDK dynamic proxy
Why does Java robot pressing Enter not work?
Display image on button click from InternalFrame
Enable/Disable Log4jLogger programmatically?
How to resolve unable to find valid certification path to requested target exception?
how to display records from database using jsp table
How to keep the program running if the user entered Y?
Dataset.map() throws ClassCastException while running in yarn-client mode from eclipse, but pass in local mode
Android pressed button, accelerate count
Convert Java Comparator to Scala Ordering
Java 8 flatmap unwrap multiple objects from collection
Sonar failed with an API incompatibility for Absent Code attribute
Trying to connect to a SQLite database, keep getting no Suitable Driver Found
Creating multiple Scenes with CLASS and calling them(classes)

Categories

HOME
log4j
pdf
gitlab
google-api-php-client
kalman-filter
jira
razor
filtering
youtube-api-v3
opengl-es-2.0
wamp
in-app-purchase
bs4
webrequest
windows-10-universal
kentor-authservices
collectd
static-libraries
messages
lombok
oracle-coherence
text-rendering
django-cms
shared-hosting
viewport
dbext
visjs
paging
orleans
mustache.php
kudan
captiveportal
icloud-api
h2db
sqlcipher
xpages-ssjs
url-scheme
google-cloud-nl
gsmcomm
media-queries
.net-4.0
catch-all
framemaker
android-kernel
android-nestedscrollview
ghost4j
suricata
hot-module-replacement
user-controls
ws-security
eclipse-gef
filepicker
paxos
pnotify
python-webbrowser
celery-task
hls.js
yii2-extension
font-size
crosswalk-runtime
pintos
parallel-data-warehouse
httplistener
trash
transmitfile
etsy
holder.js
ubuntu-10.04
skype4py
browser-link
vhd
content-length
unity3d-gui
mmc
pick
jscript.net
network-interface
asp.net-web-api-odata
id3v2
jubula
android-2.2-froyo
didselectrowatindexpath
concurrent-collections
app42
spring-io
ruby-datamapper
ftps
bulkloader
padarn
rabl
hungarian-algorithm
eventlistener
clipper
free-variable
specification-pattern
anonymous-methods
first-responder
pysimplesoap
javaspaces
uiviewanimation-curve
nsmanagedobject
removeclass
hirefire
blackberry-playbook
coercion
yetanotherforum
nhibernate.search
dbal
mirah
fixed-width
web-architecture
virtual-functions

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App