Android NDK 数字图像处理
来源:互联网 发布:jqueryplaceholder.js 编辑:程序博客网 时间:2024/06/06 02:49
package zc.jni.image;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity
{
ImageView imgImageIn = null;
ImageView imgImageOut = null;
private Bitmap bmpOrigIn = null;
private Bitmap bmpGrayOut = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imgImageIn = (ImageView)findViewById(R.id.imgBmpIn);
this.imgImageOut = (ImageView)findViewById(R.id.imgBmpOut);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.ARGB_8888;
bmpOrigIn = BitmapFactory.decodeResource(this.getResources(), R.drawable.tyr, options);
if (bmpOrigIn != null)
{
imgImageIn.setImageBitmap(bmpOrigIn);
imgImageOut.setImageBitmap(bmpOrigIn);
}
}
public void OnButtonColor2Gray(View view)
{
this.bmpGrayOut = Bitmap.createBitmap(this.bmpOrigIn.getWidth(), this.bmpOrigIn.getHeight(), Config.ALPHA_8);
myConvertToGray(this.bmpOrigIn, this.bmpGrayOut);
// maoColor2Gray(this.bmpOrigIn, this.bmpGrayOut, this.bmpOrigIn.getWidth(), this.bmpOrigIn.getHeight(), 24);
Toast.makeText(getApplicationContext(), "图像处理完毕!", Toast.LENGTH_SHORT).show();
this.imgImageOut.setImageBitmap(this.bmpGrayOut);
}
public void OnButtonReset(View view)
{
this.imgImageOut.setImageBitmap(this.bmpOrigIn);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public native void myConvertToGray(Bitmap bmpIn, Bitmap bmpOut);
public native void maoColor2Gray(Bitmap bmpIn, Bitmap bmpOut, int width, int height, int PixelFormat);
static
{
System.loadLibrary("JniImageDemo");
}
}
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := JniImageDemo
LOCAL_SRC_FILES := Color2Gray.c
LOCAL_LDLIBS := -llog -ljnigraphics
include $(BUILD_SHARED_LIBRARY)
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <android/bitmap.h>
char* native_string(char *ch)
{
return ch;
}
jstring
Java_zc_jni_image_MainActivity_ShowString( JNIEnv *env,
jobject this )
{
return (*env)->NewStringUTF(env, native_string("Hello from \"native_string\" !"));
}
typedef struct
{
uint8_t alpha;
uint8_t red;
uint8_t green;
uint8_t blue;
} argb;
JNIEXPORT void JNICALL Java_zc_jni_image_MainActivity_myConvertToGray( JNIEnv * env,
jobject obj,
jobject bmpColor,
jobject bmpGray )
{
AndroidBitmapInfo infoColor;
void* pixColor;
AndroidBitmapInfo infoGray;
void* pixGray;
int retValue;
int y;
int x;
if ((retValue = AndroidBitmap_getInfo(env, bmpColor, &infoColor)) < 0)
{
return;
}
if ((retValue = AndroidBitmap_getInfo(env, bmpGray, &infoGray)) < 0)
{
return;
}
if (infoColor.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
{
return;
}
if (infoGray.format != ANDROID_BITMAP_FORMAT_A_8)
{
return;
}
if ((retValue = AndroidBitmap_lockPixels(env, bmpColor, &pixColor)) < 0)
{
}
if ((retValue = AndroidBitmap_lockPixels(env, bmpGray, &pixGray)) < 0)
{
}
for( y = 0; y < infoColor.height; y ++ )
{
argb * line = (argb *) pixColor;
uint8_t * grayline = (uint8_t *) pixGray;
for ( x = 0 ; x < infoColor.width; x++ )
{
grayline[x] = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue;
}
pixColor = (char *)pixColor + infoColor.stride;
pixGray = (char *) pixGray + infoGray.stride;
}
AndroidBitmap_unlockPixels(env, bmpColor);
AndroidBitmap_unlockPixels(env, bmpGray);
}
- Android NDK 数字图像处理
- 数字图像和数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- 数字图像处理
- Ejb
- 如何利用百度地图JSAPI画带箭头的线?
- linus大神写链表删除
- handlersocket使用 第五章 Updating/Deleting data
- Lucene的基础知识
- Android NDK 数字图像处理
- Windows 7主题安装失败的解决办法
- handlersocket使用 第六章 Inserting data
- 设计师需要的知识体系
- DownloadManager
- c++ struct new
- Message Queue和handle
- 什么是 Enterprise JavaBeans 组件?
- handlersocket使用 第七章 Authentication