How to add image to ArrayList<string> in android? - android

I need to add an image into "item". item is an xml file with TextView...
item = new ArrayList<String>();
item.add("an image");

Try this code
ArrayList<Bitmap> mBit = new ArrayList<Bitmap>(9);
for (int i = 0; i < 9; i++)
{
mBit.add(Bitmap.createBitmap(bitmapOrg, (i % 3) * newWidth, (i / 3) * newHeight, newWidth, newHeight));
}
Collections.shuffle(mBit);
for (int i = 0; i < 10; i++)
{
Bitmap bitmap = mBit.get(i));
//Do something here
}

You should create an ArrayList of objects and you can put everything you want in it and manipulate like this :
ArrayList<Object> array = new ArrayList<Object>();
array.put(0,"A string");
array.put(1,yourbitmap);
String string = (String) array.get(0);
Bitmap bitmap = (Bitmap) array.get(1);
You must cast when you get get because it is an object array.

If by image, you mean an image File and not Image object. Then use
add(fileObject.toString())
and while retrieving recreate File object using that object String.
new File(array.get(0)).getPath()

Related

How to load and get pixel value of a 16-bit single channel PNG image?

I tried to load a 16-bit single channel PNG image in Android and get the value of each pixels.
I used function "BitmapFactory.decodeStream" and get the value of pixels directly. I got the pixel values like "-16250872".
How can I get the correct values of this type of image in Android?
InputStream ist= null;
try {
ist = getAssets().open(pic_PATH2);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bmp2 = BitmapFactory.decodeStream(ist);
int width = bmp2.getWidth();//get the width of the image
int height = bmp2.getHeight();//get the height of the image
float[][] Px2 = new float[width][height];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
Px2[j][i] = bmp2.getPixel(j, i);
}

how to get Image.getPlanes() information for an image available in drawable folder?

I have an image(.jpg) available in my drawable folder. I need the Image.getPlanes() information of it for my image classification project. So how do we read that image and get the getPlanes information out of it?
Existing Code:
private byte[][] yuvBytes;
final Plane[] planes = **image**.getPlanes(); // How to create this image object?
fillBytes(planes, yuvBytes);
/* */
protected void fillBytes(final Plane[] planes, final byte[][] yuvBytes) {
for (int i = 0; i < planes.length; ++i) {
final ByteBuffer buffer = planes[i].getBuffer();
if (yuvBytes[i] == null) {
yuvBytes[i] = new byte[buffer.capacity()];
}
buffer.get(yuvBytes[i]);
}
}
Need an Image object, that reads the image present in the Drawable folder.

How to Retrieve a Bitmap Arraylist from the sdcard

Here, I break the image in to 9*9 pieces, placed all the pieces in an array like this, ArrayList values. Then next I stored those ArrayList value in the sdcard successfully. Here my Question is how to retrieve those ArrayList value, and in that Arraylist value contains some break images. Below you can observe details.
ArrayList<Bitmap> cut = new ArrayList<Bitmap>(number_bixs);
Bitmap Bb = Bitmap.createScaledBitmap(Bsbmp, width,height, true);
rows = cols = (int) Math.sqrt(number_bixs);
hgt = Bb.getHeight() / rows;
wdt = Bb.getWidth() / cols;
int yaxis = 0;
for (int x = 0; x < rows; x++) {
int xaxis = 0;
for (int y = 0; y < cols; y++) {
cut.add(Bitmap.createBitmap(Bb, xaxis, yaxis, wdt, hgt));
xaxis += wdt;
}
}
here below code stored that ArrayList value in the sdcard.
File FracturedDirectory = new File(
Environment.getExternalStorageDirectory()
+ "/Fractured photoes/");
FracturedDirectory.mkdirs();
try {
FileOutputStream out = new FileOutputStream(Environment
.getExternalStorageDirectory().toString()
+ "/Fractured photoes/" + cut);
selectedphoto_bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
Then now, How can i retrieve those ArrayList value "cut" from the sdcard and store those in one more/ another ArrayList value.
Any suggestions thanks in advance
You are doing a few things wrong.
First, you should give a name to each bitmap in the arraylist (it could be as simple as 0.png, 1.png, 2.png etc. depending on the position of the bitmap in the arraylist) and pass the individual names to the FileOutputStream() constructor instead of passing cut. Passing cut makes no sense as the mentioned constructor should receive a single file name.
Don't forget to call out.close() after selectedphoto_bitmap.compress(...).
In order to read the bitmaps you can do something similar to this:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
for (int i = 0; i < 81; i++) {
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
cut.add(bitmap);
}

[cocos2dx android]Rendering CCSprite using raw data from Bitmap

I am trying to fetch an image from a URL into a Bitmap and then using the raw data from the Bitmap am trying to create a CCSprite. The issue here is that the image is corrupted when I display the sprite. I created a standalone android only application(no cocos2dx) and used the same code to fetch and display the Bitmap and its displayed correctly. Any reason why the image is not being properly rendered in cocos2dx?
My code to fetch the image from the URL is:
String urlString = "http://www.mathewingram.com/work/wp-content/themes/thesis/rotator/335f69c5de_small.jpg";//http://graph.facebook.com/"+user.getId()+"/picture?type=large";
Bitmap pic = null;
pic = BitmapFactory.decodeStream((InputStream) new URL(urlString).getContent());
int[] pixels = new int[pic.getWidth() * pic.getHeight()];
pic.getPixels(pixels, 0, pic.getWidth(), 0, 0,pic.getWidth(),pic.getHeight());
int len = pic.getWidth()* pic.getHeight();
nativeFbUserName(pixels,len,pic.getWidth(), pic.getHeight());
The function "nativeFbUserName" is a call to a native c++ function which is :
void Java_com_WBS_Test0001_Test0001_nativeFbUserName(JNIEnv *env, jobject thiz,jintArray name, jint len, jint width, jint height) {
jint *jArr = env->GetIntArrayElements(name,NULL);
int username[len];
for (int i=0; i<len; i++){
username[i] = (int)jArr[i];
}
HelloWorld::getShared()->picLen = (int)len;
HelloWorld::getShared()->picHeight = (int)height;
HelloWorld::getShared()->picWidth = (int)width;
HelloWorld::getShared()->saveArray(username);
HelloWorld::getShared()->schedule(SEL_SCHEDULE(&HelloWorld::addSprite),0.1);
}
void HelloWorld::saveArray(int *arrayToSave)
{
arr = new int[picLen];
for(int i = 0; i < picLen; i++){
arr[i] = arrayToSave[i];
}
}
void HelloWorld::addSprite(float time)
{
this->unschedule(SEL_SCHEDULE(&HelloWorld::addSprite));
CCTexture2D *tex = new CCTexture2D();
bool val = tex->initWithData(arr,(cocos2d::CCTexture2DPixelFormat)0,picWidth,picHeight, CCSizeMake(picWidth,picHeight));
CCLog("flag is %d",val);
CCSprite *spriteToAdd = CCSprite::createWithTexture(tex);
spriteToAdd->setPosition(ccp(500, 300));
this->addChild(spriteToAdd);
}
Edit:
So I found this link Access to raw data in ARGB_8888 Android Bitmap that states that it might be a bug. Has anyone found a solution to this?
EDIT
So I just noticed a corruption of images on the lower right corner of the image.I am not sure why this is happening and how to fix it. Any ideas?
EDIT END
Answering my own question, I obtained a byte array from the bitmap using:
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pic.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
And then passed this byte array to the native code.

How to load multiple drawables from id using a loop?

Must be a way looping through this code:
private void loadSprites() {
this.sprites[0] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom01);
this.sprites[1] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom02);
this.sprites[2] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom03);
this.sprites[3] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom04);
this.sprites[4] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom05);
this.sprites[5] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom06);
this.sprites[6] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_boom07);
}
Thanks!
If I am understand your question, you want something like,
int[] drawables = {R.drawable.ic_boom01,R.drawable.ic_boom02,R.drawable.ic_boom03,R.drawable.ic_boom04,R.drawable.ic_boom05,R.drawable.ic_boom06,R.drawable.ic_boom07}
private void loadSprites() {
for(int i=0; i<this.sprites.length; i++)
this.sprites[i] = BitmapFactory.decodeResource(getResources(), drawables[i]);
}
You should place your images in the assets folder. From their you can access them via their file name. See http://developer.android.com/reference/android/content/res/AssetManager.html.
Please look at below code for that.
First make int array
int[] mImgArray = { R.drawable.ic_boom01, R.drawable.ic_boom02,
R.drawable.ic_boom03, R.drawable.ic_boom04, R.drawable.ic_boom05,
R.drawable.ic_boom06, R.drawable.ic_boom07 };
or Set this Image to ImageView using below code
mImgView1.setImageResource(mImgArray[0]);
And you can convert this image directly to bitmap and store into bitmap array using below code.
Bitmap mBmpArray=new Bitmap[mImgArray.length];
for(int i=0; i<mImgArray.length; i++)
mBmpArray[i] = BitmapFactory.decodeResource(getResources(), mImgArray[i]);
}
This works for me
//Bitmap of images for fly asset
private Bitmap fly[] = new Bitmap[8];
//Initialize array of images
for(int i = 0; i < fly.length; i++){
this.fly[i] = BitmapFactory.decodeResource( getResources(), getResources().getIdentifier("fly_frame" + i, "drawable", context.getPackageName() ) );
}
You can do that in a simple for loop.. and start from the first id and increment it by 1 at each loop.. though I don't recommend you to do this..

Categories

Resources