从图像中获取像素颜色?

时间:2015-02-22 21:47:01

标签: android colors camera android-camera-intent getpixel

我正在尝试创建一个可以获取图像像素颜色的应用。我成功创建了一个可以拍照的相机应用程序,它注入了一个ImageView。

到目前为止,我的想法正在发挥作用,但我不知道应该如何检索像素颜色。我试图获取一个像素的RGB颜色,并在我拍照后将其注入阵列。它不起作用,我不知道如何继续这样做。

我的应用目前:

    package com.camera_test_01.camera_test_01;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    ImageView iv;
    TextView text;

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

        iv = (ImageView) findViewById(R.id.ivImage);

        Button bTakePicture = (Button) findViewById(R.id.bTakePicture);
        bTakePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Bitmap bm = (Bitmap) data.getExtras().get("data");
        iv.setImageBitmap(bm);

        Bitmap result;
        result = Bitmap.createBitmap(bm, 180, 180, 48, 48);

        int x = 0;
        int y = 0;
        int clri;
        int atlr = 0, atlg = 0, atlb = 0;
        int atlro, atlgo, atlbo;
        Integer[] clr = new Integer[2304];
        Integer[] clg = new Integer[2304];
        Integer[] clb = new Integer[2304];
        for (int i = 0; i < 48; i++) {
            for (int j = 0; j < 48; j++) {
                int pixel = result.getPixel(x,y);
                x++;
                clri = 48*i+j;
                clr[clri] = Color.red(pixel);
                clg[clri] = Color.green(pixel);
                clb[clri] = Color.blue(pixel);
            }
            x = 0;
            y++;
        }

        text = (TextView) findViewById(R.id.text);
        text.setText("RED: "+clr[1]+" GREEN: "+clg[1]+" BLUE: "+clb[1]);

    }
}

0 个答案:

没有答案