Based on this question asked by #ben75: Android : save a Bitmap to bmp file format
My question now is: How can I have a BMP image with the depth of 1 bit per pixel (Black & White)?
Answering my own question...
After some tough search all over the web I realized I had to create 2 things: a bitmap black and white - and did that using the approach of making all 0's for colors below 128 and 255's for the rest, like this (this is C# code, as I'm using Xamarin to code my app):
private void ConvertArgbToOneColor (Bitmap bmpOriginal, int width, int height){
int pixel;
int k = 0;
int B=0,G=0,R=0;
try{
for(int x = 0; x < height; x++) {
for(int y = 0; y < width; y++, k++) {
pixel = bmpOriginal.GetPixel(y, x);
R = Color.GetRedComponent(pixel);
G = Color.GetGreenComponent(pixel);
B = Color.GetBlueComponent(pixel);
R = G = B = (int)(0.299 * R + 0.587 * G + 0.114 * B);
if (R < 128) {
m_imageArray[k] = 0;
} else {
m_imageArray[k] = 1;
}
}
if(m_dataWidth>width){
for(int p=width;p<m_dataWidth;p++,k++){
m_imageArray[k]=1;
}
}
}
}catch (Exception e) {
System.Console.WriteLine ("Converting to grayscale ex: " + e.Message);
}
}
Then get the byteArray of Monochrome image:
int length = 0;
for (int i = 0; i < m_imageArray.Length; i = i + 8) {
byte first = m_imageArray[i];
for (int j = 0; j < 7; j++) {
byte second = (byte) ((first << 1) | m_imageArray[i + j]);
first = second;
}
m_rawImage[length] = first;
length++;
}
And finally create the bitmap "by hand" using the following variables and placing them into a FileOutputStream to save the file:
private static int FILE_HEADER_SIZE = 14;
private static int INFO_HEADER_SIZE = 40;
// Bitmap file header
private byte[] bfType = { (byte) 'B', (byte) 'M' };
private int bfSize = 0;
private int bfReserved1 = 0;
private int bfReserved2 = 0;
private int bfOffBits = FILE_HEADER_SIZE + INFO_HEADER_SIZE + 8;
// Bitmap info header
private int biSize = INFO_HEADER_SIZE;
private int biWidth = 0;
private int biHeight = 0;
private int biPlanes = 1;
private int biBitCount = 1;
private int biCompression = 0;
private int biSizeImage = 0;
private int biXPelsPerMeter = 0x0;
private int biYPelsPerMeter = 0x0;
private int biClrUsed = 2;
private int biClrImportant = 2;
// Bitmap raw data
private byte[] bitmap;
// Scanlinsize;
int scanLineSize = 0;
// Color Pallette to be used for pixels.
private byte[] colorPalette = { 0, 0, 0, (byte) 255, (byte) 255,
(byte) 255, (byte) 255, (byte) 255 };
Bitmap bmpMonochrome = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpMonochrome);
ColorMatrix ma = new ColorMatrix();
ma.setSaturation(0);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(ma));
canvas.drawBitmap(bmpSrc, 0, 0, paint);
Like so!
Source
Related
First Method
When I set
javasetRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); or java setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Camera2 is running fine and save image from imageavailablelistener.
Second Method
When I set java setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); or java setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
It will exit from the app and cannot save image
Conversion Credit
YUV Image to Bitmap method is credit to link
Caller
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
#Override
public void onImageAvailable(ImageReader reader) {
final Image image = reader.acquireLatestImage();
if (image == null)
return;
BitmapFunctions bitmapFunctions = new BitmapFunctions();
// QrDecoder qrDecoder = new QrDecoder();
//Convert YUV to RGB and return bitmap
Bitmap bitmap = bitmapFunctions.mediaImageToBitmap(image, activity);
Actual Conversion Functions
public class BitmapFunctions {
private Allocation allocationYuv;
private Allocation allocationRgb;
private RenderScript rs;
public Bitmap mediaImageToBitmap(Image image, Context context) {
final ByteBuffer yuvBytes = imageToByteBuffer(image);
// Convert YUV to RGB
rs = RenderScript.create(context);
Bitmap bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
allocationRgb = Allocation.createFromBitmap(rs, bitmap);
allocationYuv = Allocation.createSized(rs, Element.U8(rs), yuvBytes.array().length);
allocationYuv.copyFrom(yuvBytes.array());
ScriptIntrinsicYuvToRGB scriptYuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
scriptYuvToRgb.setInput(allocationYuv);
scriptYuvToRgb.forEach(allocationRgb);
allocationRgb.copyTo(bitmap);
return bitmap;
}
public void release() {
allocationYuv.destroy();
allocationRgb.destroy();
rs.destroy();
}
private ByteBuffer imageToByteBuffer(final Image image) {
final Rect crop = image.getCropRect();
final int width = crop.width();
final int height = crop.height();
final Image.Plane[] planes = image.getPlanes();
final byte[] rowData = new byte[planes[0].getRowStride()];
final int bufferSize = width * height * ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8;
final ByteBuffer output = ByteBuffer.allocateDirect(bufferSize);
int channelOffset = 0;
int outputStride = 0;
for (int planeIndex = 0; planeIndex < 3; planeIndex++) {
if (planeIndex == 0) {
channelOffset = 0;
outputStride = 1;
} else if (planeIndex == 1) {
channelOffset = width * height + 1;
outputStride = 2;
} else if (planeIndex == 2) {
channelOffset = width * height;
outputStride = 2;
}
final ByteBuffer buffer = planes[planeIndex].getBuffer();
final int rowStride = planes[planeIndex].getRowStride();
final int pixelStride = planes[planeIndex].getPixelStride();
final int shift = (planeIndex == 0) ? 0 : 1;
final int widthShifted = width >> shift;
final int heightShifted = height >> shift;
buffer.position(rowStride * (crop.top >> shift) + pixelStride * (crop.left >> shift));
for (int row = 0; row < heightShifted; row++) {
final int length;
if (pixelStride == 1 && outputStride == 1) {
length = widthShifted;
buffer.get(output.array(), channelOffset, length);
channelOffset += length;
} else {
length = (widthShifted - 1) * pixelStride + 1;
buffer.get(rowData, 0, length);
for (int col = 0; col < widthShifted; col++) {
output.array()[channelOffset] = rowData[col * pixelStride];
channelOffset += outputStride;
}
}
if (row < heightShifted - 1) {
buffer.position(buffer.position() + rowStride - length);
}
}
}
return output;
}
Error
2019-06-19 17:04:47.551 23356-23423/com.dsonic.datasonicscanner E/AndroidRuntime: FATAL EXCEPTION: CameraBackground
Process: com.dsonic.datasonicscanner, PID: 23356
java.lang.IllegalStateException: buffer is inaccessible
at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:219)
at com.dsonic.dsoniccamera2lib.Camera2.BitmapFunctions.imageToByteBuffer(BitmapFunctions.java:105)
at com.dsonic.dsoniccamera2lib.Camera2.BitmapFunctions.mediaImageToBitmap(BitmapFunctions.java:36)
at com.dsonic.dsoniccamera2lib.Camera2.Camera2Manager$4.onImageAvailable(Camera2Manager.java:302)
at android.media.ImageReader$ListenerHandler.handleMessage(ImageReader.java:812)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.os.HandlerThread.run(HandlerThread.java:65)
ctrl+f to find the error line
BitmapFunctions.java:105 is channelOffset += outputStride;
BitmapFunctions.java:36 is Bitmap bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
Camera2Manager.java:302 is Bitmap bitmap = bitmapFunctions.mediaImageToBitmap(image, activity);
Update 20/6/2019
Everything run fine now, same code nothing change. Using same cable but phone is restarted this morning and added Log.e("Image Size", "Width = " + image.getWidth() + " Height = " + image.getHeight());.
After I comment it out it auto exit on me again. Now I remove the comment, it can't work again. WTF?
Can anyone explain to me?
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR) instead of setting this.
Change it to force orientation in Manifest. Don't know why this work
Hope someone can explain to me.
I am trying to convert from Bitmap to Mat using opencv android from org.bytedeco.javacpp-presets:opencv:3.4.1-1.4.1
This version of opencv does not seem to have Utils.bitmapToMat or Utils.matToBitmap
Here is my code.
Bitmap bradsFace = BitmapFactory.decodeResource(getResources(), R.drawable.brad_face);
int orgWidth = bradsFace.getWidth();
int orgHeight = bradsFace.getHeight();
int[] pixels = new int[orgWidth * orgHeight];
bradsFace.getPixels(pixels, 0, orgWidth, 0, 0, orgWidth, orgHeight);
Mat m = new Mat(orgHeight, orgWidth, CvType.CV_8UC4);
int id = 0;
for(int row = 0; row < orgHeight; row++) {
for(int col = 0; col < orgWidth; col++) {
int color = pixels[id];
int a = (color >> 24) & 0xff; // or color >>> 24
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff;
int b = (color ) & 0xff;
m.put(row, col, new int[]{a, r, g, b});
id++;
}
}
In the .put method for Mat object my app is crashing. Am I doing something wrong?
In case anyone is looking for a method of converting Mat to Bitmap in android. I was able to find something.
public static Bitmap matToBitmap(Mat m) {
int mWidth = m.width();
int mHeight = m.height();
int[] pixels = new int[mWidth * mHeight];
int type = m.type();
int index = 0;
int a = 255;
int b = 255;
int g = 255;
int r = 255;
double[] abgr;
Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
try {
for (int ro = 0; ro < m.rows(); ro++) {
for (int c = 0; c < m.cols(); c++) {
abgr = m.get(ro, c);
if(type == CvType.CV_8UC4) {
a = (int)abgr[0];
b = (int)abgr[1];
g = (int)abgr[2];
r = (int)abgr[3];
} else if(type == CvType.CV_8UC3) {
a = 255;
b = (int)abgr[0];
g = (int)abgr[1];
r = (int)abgr[2];
}
int color = ((a << 24) & 0xFF000000) +
((r << 16) & 0x00FF0000) +
((g << 8) & 0x0000FF00) +
(b & 0x000000FF);
pixels[index] = color;
index++;
}
}
bitmap.setPixels(pixels, 0, mWidth, 0, 0, mWidth, mHeight);
}catch(Exception e) {
return bitmap;
}
return bitmap;
}
here i have a question on LUTs in android.
my question is, i have 4X4 LUTs, Using these LUTs apply filter effect for bitmap image in android. Below is my sample LUT file link.
Lut link sample
Is it Possible in android? if possible please help me how to apply.
Thanks in advance.
I'm working on a LUT applier library which eases the use of LUT images in Android. It uses the algorythm below, but I'd like to enhance it in the future for optimising memory usage. Now it also guesses the color axes of the LUT:
https://github.com/dntks/easyLUT/wiki
Your LUT image has the red-green-blue color dimension in an other order than what I've been used to, so I had to change the order of when getting the lutIndex (at getLutIndex()).
Please check my edited answer:
final static int X_DEPTH = 16;
final static int Y_DEPTH = 16; //One little square has 16x16 pixels in it
final static int ROW_DEPTH = 4;
final static int COLUMN_DEPTH = 4; // the image consists of 4x4 little squares
final static int COLOR_DISTORTION = 16; // 256*256*256 => 256 no distortion, 64*64*64 => 256 dividied by 4 = 64, 16x16x16 => 256 dividied by 16 = 16
private Bitmap applyLutToBitmap(Bitmap src, Bitmap lutBitmap) {
int lutWidth = lutBitmap.getWidth();
int lutColors[] = new int[lutWidth * lutBitmap.getHeight()];
lutBitmap.getPixels(lutColors, 0, lutWidth, 0, 0, lutWidth, lutBitmap.getHeight());
int mWidth = src.getWidth();
int mHeight = src.getHeight();
int[] pix = new int[mWidth * mHeight];
src.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
int R, G, B;
for (int y = 0; y < mHeight; y++)
for (int x = 0; x < mWidth; x++) {
int index = y * mWidth + x;
int r = ((pix[index] >> 16) & 0xff) / COLOR_DISTORTION;
int g = ((pix[index] >> 8) & 0xff) / COLOR_DISTORTION;
int b = (pix[index] & 0xff) / COLOR_DISTORTION;
int lutIndex = getLutIndex(lutWidth, r, g, b);
R = ((lutColors[lutIndex] >> 16) & 0xff);
G = ((lutColors[lutIndex] >> 8) & 0xff);
B = ((lutColors[lutIndex]) & 0xff);
pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
}
Bitmap filteredBitmap = Bitmap.createBitmap(mWidth, mHeight, src.getConfig());
filteredBitmap.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
return filteredBitmap;
}
//the magic happens here
private int getLutIndex(int lutWidth, int redDepth, int greenDepth, int blueDepth) {
int lutX = (greenDepth % ROW_DEPTH) * X_DEPTH + blueDepth;
int lutY = (greenDepth / COLUMN_DEPTH) * Y_DEPTH + redDepth;
return lutY * lutWidth + lutX;
}
This is how you would process an image with RenderScript's ScriptIntrinsic3DLUT
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsic3DLUT;
import android.renderscript.Type;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView imageView1;
RenderScript mRs;
Bitmap mBitmap;
Bitmap mLutBitmap;
ScriptIntrinsic3DLUT mScriptlut;
Bitmap mOutputBitmap;
Allocation mAllocIn;
Allocation mAllocOut;
Allocation mAllocCube;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.imageView);
mRs = RenderScript.create(this);
Background background = new Background();
background.execute();
}
class Background extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
if (mRs == null) {
mRs = RenderScript.create(MainActivity.this);
}
if (mBitmap == null) {
mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.bugs);
mOutputBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig());
mAllocIn = Allocation.createFromBitmap(mRs, mBitmap);
mAllocOut = Allocation.createFromBitmap(mRs, mOutputBitmap);
}
if (mLutBitmap == null) {
mLutBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.dawizfe);
int w = mLutBitmap.getWidth();
int h = mLutBitmap.getHeight();
int redDim = w / 4;
int greenDim = h / 4;
int blueDim = 16;
android.renderscript.Type.Builder tb = new Type.Builder(mRs, Element.U8_4(mRs));
tb.setX(redDim);
tb.setY(greenDim);
tb.setZ(blueDim);
Type t = tb.create();
mAllocCube = Allocation.createTyped(mRs, t);
int[] pixels = new int[w * h];
int[] lut = new int[w * h];
mLutBitmap.getPixels(pixels, 0, w, 0, 0, w, h);
int i = 0;
for (int r = 0; r < redDim; r++) {
for (int g = 0; g < greenDim; g++) {
for (int b = 0; b < blueDim; b++) {
int gdown = g / 4;
int gright = g % 4;
lut[i] = pixels[b + r * w + gdown * w * redDim + gright * blueDim];
i++;
}
}
}
// This is an identity 3D LUT
// i = 0;
// for (int r = 0; r < redDim; r++) {
// for (int g = 0; g < greenDim; g++) {
// for (int b = 0; b < blueDim; b++) {
// int bcol = (b * 255) / blueDim;
// int gcol = (g * 255) / greenDim;
// int rcol = (r * 255) / redDim;
// lut[i] = bcol | (gcol << 8) | (rcol << 16);
// i++;
// }
// }
// }
mAllocCube.copyFromUnchecked(lut);
}
if (mScriptlut == null) {
mScriptlut = ScriptIntrinsic3DLUT.create(mRs, Element.U8_4(mRs));
}
mScriptlut.setLUT(mAllocCube);
mScriptlut.forEach(mAllocIn, mAllocOut);
mAllocOut.copyTo(mOutputBitmap);
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
imageView1.setImageBitmap(mOutputBitmap);
}
}
}
u can go through this, hope it will help you to get the right process.
photo is the main bitmap here.
mLut3D is the array of LUT images stored in drawable
RenderScript mRs;
Bitmap mLutBitmap, mBitmap;
ScriptIntrinsic3DLUT mScriptlut;
Bitmap mOutputBitmap;
Allocation mAllocIn;
Allocation mAllocOut;
Allocation mAllocCube;
int mFilter = 0;
mRs = RenderScript.create(yourActivity.this);
public Bitmap filterapply() {
int redDim, greenDim, blueDim;
int w, h;
int[] lut;
if (mScriptlut == null) {
mScriptlut = ScriptIntrinsic3DLUT.create(mRs, Element.U8_4(mRs));
}
if (mBitmap == null) {
mBitmap = photo;
}
mOutputBitmap = Bitmap.createBitmap(mBitmap.getWidth(),
mBitmap.getHeight(), mBitmap.getConfig());
mAllocIn = Allocation.createFromBitmap(mRs, mBitmap);
mAllocOut = Allocation.createFromBitmap(mRs, mOutputBitmap);
// }
mLutBitmap = BitmapFactory.decodeResource(getResources(),
mLut3D[mFilter]);
w = mLutBitmap.getWidth();
h = mLutBitmap.getHeight();
redDim = w / h;
greenDim = redDim;
blueDim = redDim;
int[] pixels = new int[w * h];
lut = new int[w * h];
mLutBitmap.getPixels(pixels, 0, w, 0, 0, w, h);
int i = 0;
for (int r = 0; r < redDim; r++) {
for (int g = 0; g < greenDim; g++) {
int p = r + g * w;
for (int b = 0; b < blueDim; b++) {
lut[i++] = pixels[p + b * h];
}
}
}
Type.Builder tb = new Type.Builder(mRs, Element.U8_4(mRs));
tb.setX(redDim).setY(greenDim).setZ(blueDim);
Type t = tb.create();
mAllocCube = Allocation.createTyped(mRs, t);
mAllocCube.copyFromUnchecked(lut);
mScriptlut.setLUT(mAllocCube);
mScriptlut.forEach(mAllocIn, mAllocOut);
mAllocOut.copyTo(mOutputBitmap);
return mOutputBitmap;
}
you increase the mFilter value to get different filter effect with different LUT images, you have, check it out.
you can go through the this link on github for more help, i got the answer from here:-
https://github.com/RenderScript/RsLutDemo
hope it will help
i implement some code for water color effect on image in android but it was to slow(it's take more then 2 minute) now i try to implement this in JNI for batter speed ,
hear is my java code for
the inPixels is pixel of Bitmap .
protected int[] filterPixels( int width, int height, int[] inPixels )
{
int levels = 256;
int index = 0;
int[] rHistogram = new int[levels];
int[] gHistogram = new int[levels];
int[] bHistogram = new int[levels];
int[] rTotal = new int[levels];
int[] gTotal = new int[levels];
int[] bTotal = new int[levels];
int[] outPixels = new int[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
for (int i = 0; i < levels; i++)
rHistogram[i] = gHistogram[i] = bHistogram[i] = rTotal[i] = gTotal[i] = bTotal[i] = 0;
for (int row = -range; row <= range; row++)
{
int iy = y+row;
int ioffset;
if (0 <= iy && iy < height)
{
ioffset = iy*width;
for (int col = -range; col <= range; col++)
{
int ix = x+col;
if (0 <= ix && ix < width) {
int rgb = inPixels[ioffset+ix];
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
int ri = r*levels/256;
int gi = g*levels/256;
int bi = b*levels/256;
rTotal[ri] += r;
gTotal[gi] += g;
bTotal[bi] += b;
rHistogram[ri]++;
gHistogram[gi]++;
bHistogram[bi]++;
}
}
}
}
int r = 0, g = 0, b = 0;
for (int i = 1; i < levels; i++)
{
if (rHistogram[i] > rHistogram[r])
r = i;
if (gHistogram[i] > gHistogram[g])
g = i;
if (bHistogram[i] > bHistogram[b])
b = i;
}
r = rTotal[r] / rHistogram[r];
g = gTotal[g] / gHistogram[g];
b = bTotal[b] / bHistogram[b];
outPixels[index] = (inPixels[index] & 0xff000000) | ( r << 16 ) | ( g << 8 ) | b;
index++;
}
}
return outPixels;
}
**OUTPUT image **
and i try to convert this java code to c code but i don't what is the wrong ,
hear the code for C
void filterPixels( int width, int height, int inPixels[] )
{
int levels = 256;
int index = 0;
int rHistogram [levels];
int gHistogram [levels];
int bHistogram [levels];
int rTotal [levels];
int gTotal [levels];
int bTotal [levels];
int outPixels [width * height];
//Loop Variables
int y ;
int x ;
int i ;
int row ;
int col ;
int j ;
int range = 5 ;
for ( y = 0; y < height; y++)
{
for ( x = 0; x < width; x++)
{
for ( i = 0; i < levels; i++)
rHistogram[i] = gHistogram[i] = bHistogram[i] = rTotal[i] = gTotal[i] = bTotal[i] = 0;
for ( row = -range; row <= range; row++)
{
int iy = y+row;
int ioffset;
if (0 <= iy && iy < height)
{
ioffset = iy*width;
for ( col = -range; col <= range; col++)
{
int ix = x+col;
if (0 <= ix && ix < width) {
int rgb = inPixels[ioffset+ix];
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
int ri = r*levels/256;
int gi = g*levels/256;
int bi = b*levels/256;
rTotal[ri] += r;
gTotal[gi] += g;
bTotal[bi] += b;
rHistogram[ri]++;
gHistogram[gi]++;
bHistogram[bi]++;
}
}
}
}
int r = 0, g = 0, b = 0;
for ( j = 1; j < levels; j++)
{
if (rHistogram[j] > rHistogram[r])
r = j;
if (gHistogram[j] > gHistogram[g])
g = j;
if (bHistogram[j] > bHistogram[b])
b = j;
}
r = rTotal[r] / rHistogram[r];
g = gTotal[g] / gHistogram[g];
b = bTotal[b] / bHistogram[b];
outPixels[index] = (inPixels[index] & 0xff000000) | ( r << 16 ) | ( g << 8 ) | b;
index++;
}
}
}
i check the pixel value of java code and c code both are same(for same image)
code for call native function from my android activity .
int[] pix = new int[oraginal.getWidth() * oraginal.getHeight()];
Bitmap bitmap = oraginal.copy(oraginal.getConfig(), true);
bitmap.getPixels(pix, 0, bitmap.getWidth(), 0, 0,bitmap.getWidth(), bitmap.getHeight());
filterPixelsJNI(bitmap.getWidth(), bitmap.getHeight(), pix);
bitmap.setPixels(pix, 0, bitmap.getWidth(), 0, 0,bitmap.getWidth(), bitmap.getHeight());
myView.setImageBitmap(bitmap);
this is my first try for JNI so plz help me in this .
UPDATE
public native void filterPixelsJNI( int width, int height, int inPixels[] );
JNI
JNIEXPORT void JNICALL Java_com_testndk_HelloWorldActivity_filterPixelsJNI (JNIEnv * env, jobject obj , jint width,jint height,jint inPixels[]){
filterPixels( width, height, inPixels);
}
filterPixels method witch is call from c code .
There are several problems with your JNI code. The algorithmic part is probably correct, but you're not dealing with the Java array to C array conversion correctly.
First of all, the last argument of Java_com_testndk_HelloWorldActivity_filterPixelsJNI should be of type jintArray, and not jint []. This is how you pass a Java array to C code.
Once you get this array, you can't process it directly, you'll have to convert it to a C array:
JNIEXPORT void JNICALL Java_com_testndk_HelloWorldActivity_filterPixelsJNI (JNIEnv * env, jobject obj , jint width, jint height, jintArray inPixels) {
int *c_inPixels = (*env)->GetIntArrayElements(env, inPixels, NULL);
filterPixels( width, height, c_inPixels);
// passing 0 as the last argument should copy native array to Java array
(*env)->ReleaseIntArrayElements(env, inPixels, c_inPixels, 0);
}
I advise you to look at the JNI documentation, which explains how to deal with arrays: http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html
Note that there are now easier ways of processing Java Bitmap objects using android NDK. See an other of my answers here for details.
i want blur a image,i used :
public Bitmap mohu(Bitmap bmpOriginal,int hRadius,int vRadius) {
int width, height, r,g, b, c,a, gry,c1,a1,r1,g1,b1,red,green,blue;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
int iterations = 5;
int[] inPixels = new int[width*height];
int[] outPixels = new int[width*height];
Bitmap bmpSephia = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bmpSephia);
Paint paint = new Paint();
int i=0;
canvas.drawBitmap(bmpOriginal, 0, 0, null);
for(int x=0; x < width; x++) {
for(int y=0; y< height; y++) {
c = bmpOriginal.getPixel(x, y);
inPixels[i]=c;
i++;
}
}
for (int k = 0; k< iterations; k++ ) {
blur( inPixels, outPixels, width, height, hRadius );
blur( outPixels, inPixels, height, width, vRadius );
}
bmpSephia.setPixels(outPixels, 0, width, 0, 0, width, height);
return bmpSephia;
}
public static void blur( int[] in, int[] out, int width, int height, int radius ) {
int widthMinus1 = width-1;
int tableSize = 2*radius+1;
int divide[] = new int[256*tableSize];
for ( int i = 0; i < 256*tableSize; i++ )
divide[i] = i/tableSize;
int inIndex = 0;
for ( int y = 0; y < height; y++ ) {
int outIndex = y;
int ta = 0, tr = 0, tg = 0, tb = 0;
for ( int i = -radius; i <= radius; i++ ) {
int rgb = in[inIndex + ImageMath.clamp(i, 0, width-1)];
ta += (rgb >> 24) & 0xff;
tr += (rgb >> 16) & 0xff;
tg += (rgb >> 8) & 0xff;
tb += rgb & 0xff;
}
for ( int x = 0; x < width; x++ ) {
out[ outIndex ] = (divide[ta] << 24) | (divide[tr] << 16) | (divide[tg] << 8) | divide[tb];
int i1 = x+radius+1;
if ( i1 > widthMinus1 )
i1 = widthMinus1;
int i2 = x-radius;
if ( i2 < 0 )
i2 = 0;
int rgb1 = in[inIndex+i1];
int rgb2 = in[inIndex+i2];
ta += ((rgb1 >> 24) & 0xff)-((rgb2 >> 24) & 0xff);
tr += ((rgb1 & 0xff0000)-(rgb2 & 0xff0000)) >> 16;
tg += ((rgb1 & 0xff00)-(rgb2 & 0xff00)) >> 8;
tb += (rgb1 & 0xff)-(rgb2 & 0xff);
outIndex += height;
}
inIndex += width;
}
}
///
public static float clamp(float x, float a, float b) {
return (x < a) ? a : (x > b) ? b : x;
}
the method for some images is good,for some images .the effect not well,ot looks very rude,can you give me some advice, i have readed http://www.jhlabs.com/ip/blurring.html
and http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Image-enhance.doc.html#51172 ,but i cannot find good method for android
Your intermediate image is RGB565. That means 16 bits, 5 bits for R, 6 for G and 5 for B. If the original image is RGB888 then it would look bad after blurring. Can you not create an intermediate image in the same format as the original ?
Also, if the original image is RGB888, how is it converted to 565 ? Your code has:
c = bmpOriginal.getPixel(x, y);
inPixels[i]=c;
It looks like there is no controlled conversion.
Your blur function is for an ARGB image. As well as being inefficient since you've hard coded for 565, if the original image is ARGB8888 then your conversion to RGB565 is going to do strange things with the alpha channel.
If this answer is not enough, it would be helpful to see some "bad" images that this code creates.