From int[] to image png

1

For a project I'm working on hiding text in images using steganography. I've realized a class in my app that ask me to select an image (.png) and modify the least significant bit of every pixel. Now the problem is that I've an array of integer from which I have to create a new Image (.png) and save it to external memory. How can I do this? PS: I cannot use BufferedImage!

This is the code until when the least significant bits are modified.

package com.dev.paolo.sicinf;

import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;

import java.io.File;
import java.math.BigInteger;
import java.sql.SQLException;

public class SendKey extends ActionBarActivity{

private static final int FILE_SELECT_CODE=0;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("file/");
    startActivityForResult(Intent.createChooser(intent, "Select an Image"), FILE_SELECT_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode)
    {
        case FILE_SELECT_CODE:
            if(resultCode==RESULT_OK)
            {
                Uri uri2 = data.getData();
                String[] proj = {MediaStore.Images.Media.DATA};
                CursorLoader cursorLoader = new CursorLoader(this,uri2,proj,null,null,null);
                Cursor cursor = cursorLoader.loadInBackground();
                int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String uri = cursor.getString(column_index);
                File file = new File(uri);
                Bitmap img = BitmapFactory.decodeFile(uri);
                int height = img.getHeight();
                int width = img.getWidth();
                int[] pixel = new int[height * width];
                img.getPixels(pixel, 0, width, 1, 1, width-1, height-1);
                String key = "prova";
                byte[] b = key.getBytes();
                int count = 0;
                for (byte current_byte : b) {
                    for (int j = 7; j >= 0; j--) {
                        int lsb = (current_byte >> j) & 1;
                        pixel[count] = (pixel[count] & 0xfffffffe) + lsb;
                        count++;
                    }
                }
                //from int[] create image and save to external memory
            }
            break;
    }
}

}

java
android
image
asked on Stack Overflow May 14, 2015 by Paolo Guizzardi

1 Answer

0

Use Bitmap.createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config) to create a bitmap object. Then use Bitmap.compress() to write it to a file

answered on Stack Overflow May 14, 2015 by Gabe Sechan

User contributions licensed under CC BY-SA 3.0