0

My program currently displays the image I have taken inside an image view, I have coded a section where when you touch somewhere on the image view it should find the color of said point and display the color by changing the TextView to it. however when I touch the screen the app crashes and closes. Some of the code do nothing atm such as the SeekBar that is for later on. Any improvements in my code and how I could improve the way I have completed a task please tell me as I am new to java.

Activity:

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;


            }
        });


    }


    }

Layout:

<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"
     />

Evin1_
  • 12,292
  • 9
  • 45
  • 47

1 Answers1

0

Use:

ToggleButton Edit; --> ToggleButton edit;
Button Finalise; Button ButtonState; --> Button finalise, buttonState;     
SeekBar SeekBarState; --> SeekBar seekBarState;
Stanojkovic
  • 1,612
  • 1
  • 17
  • 24
  • 1
    Are you suggesting this fixes the problem, or just commenting on Java naming conventions? – Andy Turner Feb 09 '16 at 20:51
  • 1
    Commenting on Java naming convention, code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. – Stanojkovic Feb 09 '16 at 20:55
  • 1
    I agree that conventions are good - but I would explain that you are commenting on convention, and why it is a good idea to follow it, rather than just saying "Use ". – Andy Turner Feb 09 '16 at 20:57
  • This improve his coding, too. – Stanojkovic Feb 09 '16 at 20:58
  • Also, provide a link to a page describing Java naming conventions more comprehensively - variable naming is just one thing. – Andy Turner Feb 09 '16 at 20:59
  • 1
    http://www.oracle.com/technetwork/java/codeconventions-150003.pdf, Java naming convention. – Stanojkovic Feb 09 '16 at 21:02