Thursday, April 23, 2015

Bitmap to ByteArray Android

public static byte[] convertBitmapToByteArray(Bitmap bitmap) {
if (bitmap == null) {
return null;
} else {
byte[] b = null;
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, byteArrayOutputStream);
b = byteArrayOutputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return b;
}

}

Wednesday, April 22, 2015

ARC forbids explicit message send of 'release'

ARC forbids explicit message send of 'release'

ARC is an automatic reference counting. Here no need to release memory manually, ARC will release automatically.

Its a compiler feature that , it uses automatic memory management of objective c objects.

Sunday, April 19, 2015

Thursday, April 16, 2015

OpenCv tutorial

Open Source Computer Vision (OpenCV)  -  Four Modules are there.

CV - Main OpenCV functions, image processing and vision algorithms.

CVAUX  - Experimental OpenCv functions.

CXCORE -Data structure support and other algorithms.

HIGHGUI : Graphical User Interface functions , image and video.












Wednesday, April 15, 2015

!!! FAILED BINDER TRANSACTION !!!

 !!! FAILED BINDER TRANSACTION !!!


I got this issue while transferring large bitmap data.

intent.putExtra("imagedata", data);

To resolve this issue, avoid transferring of large bitmaps.

The binder transaction failed due to large size.During remote procedure call, arguments and the returned value of call transferred as Parcel objects stored under the binder transaction buffer. If the returned value or arguments too large to fit in the transaction butter, the call will get fail and TransactionTooLargeException will be thrown.





Friday, April 10, 2015

java.lang.UnsatisfiedLinkError Android NDK

04-10 14:49:35.505: E/AndroidRuntime(14206): java.lang.UnsatisfiedLinkError: Native method not found: com.permadi.testjni.TestJNIActivity.stringFromJNICPP:()Ljava/lang/String;
04-10 14:49:35.505: E/AndroidRuntime(14206): at com.permadi.testjni.TestJNIActivity.stringFromJNICPP(Native Method)
04-10 14:49:35.505: E/AndroidRuntime(14206): at com.permadi.testjni.TestJNIActivity.onCreate(TestJNIActivity.java:18)
04-10 14:49:35.505: E/AndroidRuntime(14206): at android.app.Activity.performCreate(Activity.java:5275)
04-10 14:49:35.505: E/AndroidRuntime(14206): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)




Solution: please check the package name and method name for an activity. I got this issue due to case sensitive in cpp file for package name or method name.

Android NDK Hello World


TestJNIActivity.java

package com.permadi.testjni;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class TestJNIActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_jni);


TextView myTextField = (TextView)findViewById(R.id.myTextField);
       myTextField.setText(stringFromJNICPP());
}


public native String  stringFromJNICPP();

static {
        System.loadLibrary("TestJNI");
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_jni, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}




TestJNI.cpp

#include <string.h>
#include <jni.h>
#include <android/log.h>

 extern "C" {
     JNIEXPORT jstring JNICALL Java_com_permadi_testjni_TestJNIActivity_stringFromJNICPP(JNIEnv * env, jobject obj);
 };

 JNIEXPORT jstring JNICALL Java_com_permadi_testjni_TestJNIActivity_stringFromJNICPP(JNIEnv * env, jobject obj)
 {
return env->NewStringUTF("Hello World");
 }

Tuesday, March 3, 2015

Custom ImageView Circular imageview

Circular imageview android


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;

import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundImageView extends ImageView {

public RoundImageView(Context context) {
super(context);
}

public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {
BitmapDrawable drawable = (BitmapDrawable) getDrawable();

if (drawable == null) {
return;
}

if (getWidth() == 0 || getHeight() == 0) {
return;
}

Bitmap fullSizeBitmap = drawable.getBitmap();

int scaledWidth = getMeasuredWidth();
int scaledHeight = getMeasuredHeight();

Bitmap mScaledBitmap;
if (scaledWidth == fullSizeBitmap.getWidth()
&& scaledHeight == fullSizeBitmap.getHeight()) {
mScaledBitmap = fullSizeBitmap;
} else {
mScaledBitmap = Bitmap.createScaledBitmap(fullSizeBitmap,
scaledWidth, scaledHeight, true /* filter */);
}


Bitmap circleBitmap = getCircledBitmap(mScaledBitmap);

canvas.drawBitmap(circleBitmap, 0, 0, null);

}

public Bitmap getRoundedCornerBitmap(Context context, Bitmap input,
int pixels, int w, int h, boolean squareTL, boolean squareTR,
boolean squareBL, boolean squareBR) {

Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final float densityMultiplier = context.getResources()
.getDisplayMetrics().density;

final int color = 0xff424242;

final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, w, h);
final RectF rectF = new RectF(rect);

// make sure that our rounded corner is scaled appropriately
final float roundPx = pixels * densityMultiplier;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

// draw rectangles over the corners we want to be square
if (squareTL) {
canvas.drawRect(0, 0, w / 2, h / 2, paint);
}
if (squareTR) {
canvas.drawRect(w / 2, 0, w, h / 2, paint);
}
if (squareBL) {
canvas.drawRect(0, h / 2, w / 2, h, paint);
}
if (squareBR) {
canvas.drawRect(w / 2, h / 2, w, h, paint);
}

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(input, 0, 0, paint);

return output;
}

Bitmap getCircledBitmap(Bitmap bitmap) {

Bitmap result = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(result);

int color = Color.BLUE;
Paint paint = new Paint();
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getHeight() / 2, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return result;
}


}