Tuesday, April 29, 2014

Passing int as parameter to UIGestureRecognizer

Tag attribute is the key

-----------------

int i;
for(i = 0; i < count; i++) {
    // some image
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    // attach to some view
    imageView.tag = i;
 
    UITapGestureRecognizer *g = [[UITapGestureRecognizer alloc]
                                    initWithTarget: self
                                           action: @selector(imageTap:)];
    [imageView addGestureRecognizer:g];
}
 
// handler
- (void)imageTap:(UITapGestureRecognizer *)sender {
  // identifier can be referenced in sender.view.tag

UIImageView *imageView = (UIImageView *)sender.view;

}

Friday, April 25, 2014

Iphone version detection

#define IS_DEVICE_RUNNING_IOS_7_AND_ABOVE() ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending)
#define iPhone4Or5oriPad ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : 999))
Now in programming you can say...
if (IS_DEVICE_RUNNING_IOS_7_AND_ABOVE()) {
    NSLog("This is iOS 7");
} else {
    NSLog("This is iOS 6 or below");
}


if (iPhone4Or5oriPad==4) {
    NSLog("This is 3.5 inch iPhone - iPhone 4s or below");
} else if (iPhone4Or5oriPad==5) {
    NSLog("This is 4 inch iPhone - iPhone 5 & above");
} else {
    NSLog("This is iPad");
}

Thursday, March 6, 2014

gdb and mpi

Compile for both MPI and openMP

mpic++ -fopenmp -g aa.cpp -o test5 -std=c++11

Run
mpirun -np 3 test5

Run with gdb

mpirun -np 1 xterm -e gdb test5

Breakpoint for gdb
Break linenumber

Run in gdb
run

Step over in gdb
n
Continue in gdb
c

Wednesday, August 7, 2013

Android Google map marker onclick listener

theMap.setOnMarkerClickListener(new OnMarkerClickListener()
        {

            @Override
            public boolean onMarkerClick(Marker arg0) {
                //if(arg0.getTitle().equals("MyHome")) // if marker source is clicked
                     Toast.makeText(MainActivity.this, arg0.getTitle(),1000).show();// display toast
                return true;
            }

        }); 

Wednesday, July 31, 2013

location to address in android

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);

String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);

Wednesday, July 24, 2013

Android: save camera image to file-folder

In menifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In activity class
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.triage_form);
     
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            //File file = new File(Environment.getExternalStorageDirectory() + "/photousman.jpg");
                //Uri outputFileUri = Uri.fromFile(file);
           
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
               // cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String filename = myfilename.png;
    File sd = new File(Environment.getExternalStorageDirectory()+ "/triage");
    boolean success = true;
    if (!sd.exists()) {
       success = sd.mkdir();
    }
    File dest = new File(sd, filename);
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(photo);
           
            try {
                FileOutputStream out = new FileOutputStream(dest);
                photo.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
           } catch (Exception e) {
                e.printStackTrace();
           }
           
        }
    } 

Android- Add camera to activity

Taken from the link

package edu.gvsu.cis.masl.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity {
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST); 
            }
        });
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }  
    } 
}


Note that the camera app itself gives you the ability to review/retake the image, and once an image is accepted, the activity displays it.
Here is the layout that the above activity uses. It is simply a LinearLayout containing a Button with id button1 and an ImageView with id imageview1:
<?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"
    >
    <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button>
    <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>

</LinearLayout>

And one final detail, be sure to add:
<uses-feature android:name="android.hardware.camera"></uses-feature> 
and if camera is optional to your app functionality. make sure to set require to false in the permission. like this
<uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>
to your manifest.xml.