java - 从 int[] 到图像 png

标签 java android image

对于一个项目,我正在研究使用隐写术隐藏图像中的文本。我在我的应用程序中实现了一个类,要求我选择一个图像(.png)并修改每个像素的最低有效位。现在的问题是我有一个整数数组,我必须从中创建一个新图像(.png)并将其保存到外部存储器。我怎样才能做到这一点? PS:我无法使用BufferedImage!

这是修改最低有效位之前的代码。

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;
    }
}

}

最佳答案

使用 Bitmap.createBitmap(int[] Colors, int offset, int stride, int width, int height, Bitmap.Config config) 创建位图对象。然后使用Bitmap.compress()将其写入文件

关于java - 从 int[] 到图像 png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30248344/

相关文章:

java - 在 Spring webflow 中捕获 "dead" session

java - JTable 在悬停/单击/滚动新数据时显示以前的数据

android - 在安卓中分享图片

android - 相机在方向上崩溃

java - Controller有很多@Autowired服务

android - 启动时无法在 Android 应用程序中实例化 fragment

java - 我的 Android 应用程序运行非常缓慢

java - 将图像绑定(bind)到数组值?

ruby-on-rails - rails : Why images are not showing in my rails basic app

java - Java 8 中的抽象类和接口(interface)有什么区别?