查找图像视图中像素的颜色错误

时间:2016-02-09 20:45:33

标签: java android colors crash rgb

我的程序当前显示我在图像视图中拍摄的图像,我编写了一个部分,当您触摸图像视图的某个位置时,它应该找到所述点的颜色并通过将TextView更改为它来显示颜色。然而,当我触摸屏幕时,应用程序崩溃并关闭。 有些代码没有做任何事情,比如稍后的SeekBar。 我的代码有任何改进,以及如何改进我完成任务的方式,请告诉我,因为我是java的新手。

活动:

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;


import java.io.File;

public class MainActivity extends Activity {

    private static final int CONTENT_REQUEST = 1337;
    private File output = null;
    Button button;
    ToggleButton Edit;
    Button Finalise;
    ImageView imageView;
    Button ButtonState;
    SeekBar SeekBarState;
    SeekBar seekBar;
    Bitmap bitmap;
    Bitmap bitmap2;
    TextView textView;

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

        button = (Button) findViewById(R.id.CameraButton);
        Edit = (ToggleButton) findViewById(R.id.EditorButton);
        Finalise = (Button) findViewById(R.id.FinaliseButton);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        imageView = (ImageView) findViewById(R.id.Image_view);
        ButtonState = (ToggleButton) findViewById(R.id.EditorButton);
        ButtonState = (Button) findViewById(R.id.FinaliseButton);
        SeekBarState = (SeekBar) findViewById(R.id.seekBar);

        ButtonState.setEnabled(false);
        SeekBarState.setVisibility(View.GONE);


        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {


                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/COMP4"); //Storage/sdcard0/DCIM/COMP4/
                dir.mkdirs();
                output = new File(dir, "COMP4Image.jpeg");
                i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
                startActivityForResult(i, CONTENT_REQUEST);


            }


        });


        Edit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SeekBarState = (SeekBar) findViewById(R.id.seekBar);
                    SeekBarState.setVisibility(View.VISIBLE);

                } else {

                    SeekBarState = (SeekBar) findViewById(R.id.seekBar);
                    SeekBarState.setVisibility(View.GONE);

                }
            }
        });


    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        ButtonState = (Button) findViewById(R.id.CameraButton);
        ButtonState.setEnabled(false);
        ButtonState = (Button) findViewById(R.id.EditorButton);
        ButtonState.setEnabled(true);
        ButtonState = (Button) findViewById(R.id.FinaliseButton);
        ButtonState.setEnabled(true);

        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(output.getAbsolutePath(), bmOptions);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setImageBitmap(bitmap);

        final Bitmap bitmap2 = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                int pixel = bitmap2.getPixel(x, y);


                int red = Color.red(pixel);
                int blue = Color.blue(pixel);
                int green = Color.green(pixel);
                textView.setTextColor(Color.rgb(red, green, blue));
                return false;


            }
        });


    }


    }

布局:

<Button
    android:layout_width="400dp"
    android:layout_height="50dp"
    android:text="Capture Image"
    android:id="@+id/CameraButton"
    android:onClick="buttonOnClick"
    android:layout_alignParentTop="true"
    android:textStyle="bold"
    android:layout_marginRight="380dp"
    android:layout_marginEnd="380dp"
    android:layout_alignRight="@+id/Image_view"
    android:layout_alignEnd="@+id/Image_view" />

<ImageView
    android:layout_width="1000dp"
    android:layout_height="1000dp"
    android:id="@+id/Image_view"
    android:contentDescription="@string/image_cd_camera"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:layout_marginBottom="0dp"/>

<Button
    android:layout_width="400dp"
    android:layout_height="50dp"
    android:text="Finalise"
    android:id="@+id/FinaliseButton"
    android:onClick="FinaliseOnClick"
    android:textStyle="bold"
    android:layout_marginLeft="380dp"
    android:layout_marginStart="380dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<SeekBar
    android:layout_width="500dp"
    android:layout_height="wrap_content"
    android:id="@+id/seekBar"
    android:layout_alignBottom="@+id/Image_view"
    android:layout_marginBottom="10dp"
    android:layout_centerHorizontal="true"
    android:max="510"
    android:progress="255"/>

<ToggleButton
    android:layout_width="400dp"
    android:layout_height="50dp"
    android:text="Edit Image"
    android:id="@+id/EditorButton"
    android:layout_alignTop="@+id/FinaliseButton"
    android:layout_toRightOf="@+id/FinaliseButton"
    android:layout_toEndOf="@+id/FinaliseButton"
    android:textStyle="bold" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="TESTING COLOURS"
    android:id="@+id/textView"
    android:textSize="50dp"
    android:layout_centerVertical="true"
    android:layout_above="@id/seekBar"
     />

1 个答案:

答案 0 :(得分:0)

使用:

ToggleButton Edit; --> ToggleButton edit;
Button Finalise; Button ButtonState; --> Button finalise, buttonState;     
SeekBar SeekBarState; --> SeekBar seekBarState;