GitHubDroid / GEODROID

0 stars 0 forks source link

Sketch Note on Map Canvas #2

Closed GitHubDroid closed 11 years ago

GitHubDroid commented 11 years ago

Add the ability for the user to choose to sketch on an empty canvas or relevant map area.

GitHubDroid commented 11 years ago

I was able to put the following code together:

Test.java

    public class Test extends Activity implements OnClickListener, OnTouchListener { 

   ImageView choosenImageView;
   Button choosePicture;
   Button savePicture;

    Bitmap bmp;
    Bitmap alteredBitmap;
    Canvas canvas;
    Paint paint;
    Matrix matrix;
    float downx = 0;
     float downy = 0;
     float upx = 0;
      float upy = 0;

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

       choosenImageView = (ImageView) this.findViewById(R.id.ChoosenImageView);
       choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton);
       savePicture = (Button) this.findViewById(R.id.SavePictureButton);

       savePicture.setOnClickListener(this);
       choosePicture.setOnClickListener(this);
        choosenImageView.setOnTouchListener(this);
       }

    public void onClick(View v) {

     if (v == choosePicture) {
     Intent choosePictureIntent = new Intent(
      Intent.ACTION_PICK,
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      startActivityForResult(choosePictureIntent, 0);
      } else if (v == savePicture) {

      if (alteredBitmap != null) {
        ContentValues contentValues = new ContentValues(3);
        contentValues.put(Media.DISPLAY_NAME, "Draw On Me");

        Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, contentValues);
        try {
         OutputStream imageFileOS = getContentResolver().openOutputStream(imageFileUri);
         alteredBitmap.compress(CompressFormat.JPEG, 90, imageFileOS);
          Toast t = Toast.makeText(this, "Saved!", Toast.LENGTH_SHORT);
          t.show();

         } catch (Exception e) {
         Log.v("EXCEPTION", e.getMessage());
                  }
            }
           }
        }

       protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
       super.onActivityResult(requestCode, resultCode, intent);

         if (resultCode == RESULT_OK) {
         Uri imageFileUri = intent.getData();
          try {
          BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
          bmpFactoryOptions.inJustDecodeBounds = true;
           bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
            imageFileUri), null, bmpFactoryOptions);

            bmpFactoryOptions.inJustDecodeBounds = false;
            bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
            imageFileUri), null, bmpFactoryOptions);

            alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
           .getHeight(), bmp.getConfig());
            canvas = new Canvas(alteredBitmap);
            paint = new Paint();
            paint.setColor(Color.GREEN);
            paint.setStrokeWidth(5);
            matrix = new Matrix();
            canvas.drawBitmap(bmp, matrix, paint);

            choosenImageView.setImageBitmap(alteredBitmap);
            choosenImageView.setOnTouchListener(this);

              } catch (Exception e) {
              Log.v("ERROR", e.toString());
               }
            }
         }

          public boolean onTouch(View v, MotionEvent event) {
          int action = event.getAction();
           switch (action) {
           case MotionEvent.ACTION_DOWN:
           downx = event.getX();
           downy = event.getY();
           break;
           case MotionEvent.ACTION_MOVE:
            upx = event.getX();
            upy = event.getY();
            canvas.drawLine(downx, downy, upx, upy, paint);
            choosenImageView.invalidate();
            downx = upx;
            downy = upy;
             break;
              case MotionEvent.ACTION_UP:
               upx = event.getX();
               upy = event.getY();
               canvas.drawLine(downx, downy, upx, upy, paint);
               choosenImageView.invalidate();
               break;
               case MotionEvent.ACTION_CANCEL:
                break;
                default:
                break;
                       }
                return true;
                }
             }

main.xml

   <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
      android:layout_width="fill_parent"
       android:layout_height="fill_parent"
      >
   <ImageView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/ChoosenImageView">
  </ImageView>

  <Button
    android:id="@+id/ChoosePictureButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Choose Picture" />

  <Button
    android:id="@+id/SavePictureButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Save Picture" />

    </LinearLayout>
GitHubDroid commented 11 years ago

What is happening is that if I choose a tmp.png I get the following screen where I am able to sketch on it as you can see but the buttons no longer show for saving or choosing a picture. screenshot_2013-09-13-10-27-23

Now if I select an image like 5479c368-1be7-11e3-9057-d0ef8e34bb9d I get the following screen where I am able to sketch on it and all the buttons are visible. screenshot_2013-09-13-10-28-52

GitHubDroid commented 11 years ago

Working:

Test.java

                            package com.app.test;

                                  @SuppressWarnings("unused")
                                public class Test extends Activity implements OnClickListener, OnTouchListener { 

                             ImageView chosenImageView;
                             Button choosePicture;
                             Button savePicture;

                             Bitmap bmp;
                             Bitmap alteredBitmap;
                             Canvas canvas;
                             Paint paint;
                             Matrix matrix;
                             float downx = 0;
                             float downy = 0;
                             float upx = 0;
                             float upy = 0;

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

                                   chosenImageView = (ImageView) this.findViewById(R.id.ChosenImageView);
                                   choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton);
                                   savePicture = (Button) this.findViewById(R.id.SavePictureButton);

                                    savePicture.setOnClickListener(this);
                                    choosePicture.setOnClickListener(this);
                                    chosenImageView.setOnTouchListener(this);
                                        }

                                      public void onClick(View v) {

                                         if (v == choosePicture) {
                                          Intent choosePictureIntent = new Intent(
                                          Intent.ACTION_PICK,
                                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                            startActivityForResult(choosePictureIntent, 0);
                                              } else if (v == savePicture) {

                                             if (alteredBitmap != null) {
                                             ContentValues contentValues = new ContentValues(3);
                                             contentValues.put(Media.DISPLAY_NAME, "Draw On Me");

                                   Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,   
                                         contentValues);
                                 try {
                                     OutputStream imageFileOS =   
                                              getContentResolver().openOutputStream(imageFileUri);
                                 alteredBitmap.compress(CompressFormat.JPEG, 90, imageFileOS);
                                 Toast t = Toast.makeText(this, "Saved!", Toast.LENGTH_SHORT);
                                 t.show();

                                 } catch (Exception e) {
                                Log.v("EXCEPTION", e.getMessage());
                                             }
                                      }
                                 }
                              }

                                   protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
                                   super.onActivityResult(requestCode, resultCode, intent);

                                   if (resultCode == RESULT_OK) {
                                    Uri imageFileUri = intent.getData();
                                    try {
                                      BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                                      bmpFactoryOptions.inJustDecodeBounds = true;
                                      bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
                                      imageFileUri), null, bmpFactoryOptions);

                                       bmpFactoryOptions.inJustDecodeBounds = false;
                                       bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
                                        imageFileUri), null, bmpFactoryOptions);

                                        alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
                                       .getHeight(), bmp.getConfig());
                                        canvas = new Canvas(alteredBitmap);
                                         paint = new Paint();
                                         paint.setColor(Color.GREEN);
                                         paint.setStrokeWidth(5);
                                         matrix = new Matrix();
                                         canvas.drawBitmap(bmp, matrix, paint);

                                         chosenImageView.setImageBitmap(alteredBitmap);
                                         chosenImageView.setOnTouchListener(this);

                                            } catch (Exception e) {
                                                Log.v("ERROR", e.toString());
                                                      }
                                                   }
                                                }

                                          public boolean onTouch(View v, MotionEvent event) {
                                           int action = event.getAction();
                                           switch (action) {
                                            case MotionEvent.ACTION_DOWN:
                                            downx = event.getX();
                                            downy = event.getY();
                                            break;
                                            case MotionEvent.ACTION_MOVE:
                                            upx = event.getX();
                                            upy = event.getY();
                                            canvas.drawLine(downx, downy, upx, upy, paint);
                                            chosenImageView.invalidate();
                                            downx = upx;
                                            downy = upy;
                                             break;
                                               case MotionEvent.ACTION_UP:
                                               upx = event.getX();
                                               upy = event.getY();
                                               canvas.drawLine(downx, downy, upx, upy, paint);
                                               chosenImageView.invalidate();
                                               break;
                                               case MotionEvent.ACTION_CANCEL:
                                                break;
                                                default:
                                                break;
                                                    }
                                             return true;
                                                 }
                                              }

main.xml

                                    <?xml version="1.0" encoding="utf-8"?>
                                    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                     android:layout_width="fill_parent"
                                     android:layout_height="fill_parent"
                                     android:orientation="vertical" >

                                     <Button
                                       android:id="@+id/ChoosePictureButton"
                                       android:layout_width="wrap_content"
                                       android:layout_height="wrap_content"
                                       android:layout_gravity="center"
                                       android:text="Choose Picture" />

                                       <Button
                                           android:id="@+id/SavePictureButton"
                                           android:layout_width="wrap_content"
                                           android:layout_height="wrap_content"
                                           android:layout_gravity="center"
                                            android:text="Save Picture" />

                                         <ImageView
                                              android:id="@+id/ChosenImageView"
                                              android:layout_width="wrap_content"
                                              android:layout_height="wrap_content"
                                              android:layout_gravity="center"
                                              android:adjustViewBounds="true"
                                              android:fitsSystemWindows="true"
                                              android:scaleType="matrix" />

                                            </LinearLayout>

This is working but, I need to figure out the best way to implement into the DrawingActivity.

GitHubDroid commented 11 years ago

Possible Other Options:

Tested Working code is below:

This activity will retrieve specific image and send the image to skitch for editing.

MainActivity.java

                 public class MainActivity extends Activity {

           public void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main);

                 ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {

                    String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/geodroid/";

                    File dir = new File(file_path);

                    dir.mkdirs();

                    File file = new File(dir, "tmp.png");

                                    //Share

                     Intent shareIntent = new Intent(Intent.ACTION_SEND);

                     shareIntent.setType("image/png");                      

                 shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));

                     shareIntent.putExtra(Intent.EXTRA_TEXT,"My Image");

                     PackageManager pm = getApplicationContext().getPackageManager();

                     List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);

                    for (final ResolveInfo app : activityList) {

                        if ((app.activityInfo.name).contains("skitch")) {

                            final ActivityInfo activity = app.activityInfo;

                            final ComponentName name = new  
                            ComponentName(activity.applicationInfo.packageName,activity.name);

                            shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);

                            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK

                                                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

                            shareIntent.setComponent(name);

                            startActivity(shareIntent);

                                }

                            }

                        }

                       });

                       }

               }

activity_main.xml

                  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"

                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                 android:paddingBottom="@dimen/activity_vertical_margin"
                 android:paddingLeft="@dimen/activity_horizontal_margin"
                 android:paddingRight="@dimen/activity_horizontal_margin"
                 android:paddingTop="@dimen/activity_vertical_margin"
                  tools:context=".MainActivity" >

                 <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:text="Choose" />
                  </RelativeLayout>      
GitHubDroid commented 11 years ago

Issue now cont in (Integrate Geo-DROID with Skitch-DROID issue)