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

Getting StackOverflow exception during some calculations [duplicate]
java.lang.NoClassDefFoundError: com/singularity/ee/agent/appagent/services/bciengine/templates/IHttpServletRequest
Why Rational is the type that the add method returns? - Java
Changing an ImageResource (drawable) programmatically
How can I configure Jackson to get a serializer via a static getInstance() method?
Thread.sleep alternative for an operation that needs to block for x miliseconds
Nesting formats in String.format
Java - method parameters, String followed by List
why i am getting org.json.JSONException: Value property of type java.lang.String cannot be converted to JSONObject?
How to record video in front camera using surfaceview without mirror effect?
How to use 'ANY' function with JOOQ
Handling Generics list within object for JAX-RS Client
Protected constructor accessible in sibling-class [duplicate]
Guava ImmutableBiMap becomes LinkedHashMap and cause Spring autowiring mistake
Mockito: Mocked instance is not injected after annotating spring bean with #RefreshScope [duplicate]
JSON - * equivalent to return all values within timeStamp

Categories

HOME
sendgrid
asp.net-core
hook
netsuite
amazon-ec2
iot
dot
tesseract
google-oauth
fingerprint
ios-charts
portia
pheatmap
awesome-wm
dacpac
numeral.js
messages
designer
after-effects
code-review
immutable.js
reverse-proxy
visjs
opencover
cloudhub
angular2-aot
sparse-matrix
fish
centos6.5
semantic-analysis
wpfdatagrid
fluentvalidation
primitive
bpel
instant-messaging
skia
twilio-api
winrt-xaml-toolkit
webtest
catalog
simplexml
dartium
service-discovery
quadratic-programming
eigenvalue
no-www
prolog-setof
togetherjs
drupal-6
composite-key
nsarray
mako
spring-mongodb
crosswalk-runtime
gnome-shell-extensions
file-writing
mongocsharpdriver
flash-cs5
sigabrt
fadeout
design-by-contract
teamcity-8.0
collapse
cyclomatic-complexity
tmuxinator
citrus-pay
tarjans-algorithm
ionic
splash
generic-programming
composite
gwidgets
issuu
onactivityresult
article
algebraic-data-types
stxxl
fmod
terminal-services
gnu-smalltalk
ccss
cdc
inbox
apc
shellexecute
buster.js
limejs
ocunit
kyotocabinet
libstdc++
gdata-api
osql
work-stealing
vdsp
genshi
django-tagging
rfc1123
pyinotify
sortable-tables
mozilla-prism
xsdobjectgen
forums
multi-tier
genealogy
grid-system
lzh

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App