Android Jni OpenCV-绘制朱利亚(Julia)集合图形

一,java代码,收集Bitmap 信息

private Button btnProc;
private ImageView imageView;
private Bitmap bmp;

// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}

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

    // Example of a call to a native method
    btnProc = (Button) findViewById(R.id.btn_gray_process);
    imageView = (ImageView) findViewById(R.id.image_view);

    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.test7);
    imageView.setImageBitmap(bmp);
    btnProc.setOnClickListener(this);
}

/**
 * A native method that is implemented by the 'native-lib' native library,
 * which is packaged with this application.
 */
public static native int[] grayProc(int[] pixels, int w, int h);


@Override
public void onClick(View view) {

    int w = bmp.getWidth();
    int h = bmp.getHeight();
    int[] pixels = new int[w*h];
    bmp.getPixels(pixels, 0, w, 0, 0, w, h);

    long startTime = System.currentTimeMillis();
    int[] resultInt = grayProc(pixels, w, h);
    long endTime = System.currentTimeMillis();

    Log.e("JNITime",""+(endTime-startTime));
    Bitmap resultImg = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    //(@ColorInt int[] pixels, int offset, int stride,int x, int y, int width,   int height)
    resultImg.setPixels(resultInt, 0, w, 0, 0, w, h);
    imageView.setImageBitmap(resultImg);

}

二,jni实现

这里写图片描述

using namespace std;
using namespace cv;

void printMAtMessage(Mat &mat);

const int iterations = 200; //迭代次数

//控制图像点
const float cReal = -0.88; //实部
const float dVirtual = 0.201; //虚部
double m_real, m_image; //Mandelbro集

class Nephogram
{
public:
double wide;
double height;
public:
Nephogram(double w = 0,double h = 0){

    wide = w;
    height = h;
}

};

Nephogram operator+(const Nephogram& m, const Nephogram &n)
{
Nephogram nephogram;
nephogram.wide = m.wide + n.wide;
nephogram.height = m.height + n.height;
return nephogram;
}

Nephogram operator*(const Nephogram& m, const Nephogram &n)
{
Nephogram nephogram;
nephogram.wide = m.wide * n.wide - m.height * n.height;
nephogram.height = m.height * n.wide + m.wide * n.height;
return nephogram;
}

double Model(Nephogram nephogram)
{
return sqrtf(nephogram.wide * nephogram.wide + nephogram.height * nephogram.height);
}

double Iteration(Nephogram nephogram,int n){
if(n == 0){
return Model(nephogram);
}else{
Nephogram temp = nephogram*nephogram;
temp.wide += cReal;
temp.height += dVirtual;
return Iteration(temp, n - 1);
}
}

Vec3b drawingJulia(double distArea)
{
if (distArea < 1000000 && distArea >-1000000)
return Vec3b(100, 180, 255);
else
return Vec3b(0, 0, 0); //Julia集之外的区域置为黑色背景
}

extern “C”
JNIEXPORT jintArray JNICALL
Java_com_example_dgxq008_opencv_1readpixel_MainActivity_grayProc(JNIEnv *env, jclass type
, jintArray pixels_
, jint w
, jint h) {

jint* pixels = env->GetIntArrayElements(pixels_, NULL);
if (pixels==NULL){
    return 0;
}

//图片一进来时是ARGB  通过mat转换BGRA
Mat img(h,w,CV_8UC4,(uchar *)pixels);  //pixels 操作的是同一份数据

Mat temp;
//转化为单通道灰度图,并打印信息
cvtColor(img,temp,COLOR_RGBA2GRAY);

Mat image = Mat(Size(w, h), CV_8UC3, Scalar::all(10));

int i,j ;
int height =image.rows;
int wide = image.cols ;

for ( i= 0; i < height; i++)
{
    for (j = 0; j < wide; j++)
    {
        float w = (j - wide / 2) / 200.0;
        float h = (i - height / 2) / 200.0;

        m_real = w;
        m_image = h;

        Nephogram a(w, h);
        float dist = Iteration(a, iterations);

        image.at<Vec3b>(i, j) = drawingJulia(dist);
    }
}

//转化为单通道灰度图,并打印信息
cvtColor(image,img,COLOR_RGB2RGBA);

printMAtMessage(img);

//对应数据指针
int size = w*h;
jintArray result = env->NewIntArray(size);
env->SetIntArrayRegion(result,0,size,pixels);

env->ReleaseIntArrayElements(pixels_, pixels, 0);

return result;

}

void printMAtMessage(Mat &mat) {
LOGD(“***************************Mat信息开始************************”);
LOGD(“mat.rows %d”,mat.rows);
LOGD(“mat.cols %d”,mat.cols);
LOGD(“mat.total %d”,mat.total());
LOGD(“mat.channels %d”,mat.channels());
LOGD(“mat.depth %d”,mat.depth());
LOGD(“mat.type %d”,mat.type());
LOGD(“mat.flags %d”,mat.flags);
LOGD(“mat.elemSize %d”,mat.elemSize());
LOGD(“mat.elemSize1 %d”,mat.elemSize1());
LOGD(“***************************Mat信息结束************************”);
}

三,效果图

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值