2

I am trying to scale down a image taken via camera and place it in mapview in my app. However i can take the image but it doesn't seem to scale it down to a thumbnail size image. Here is my code for MainActivity.java

AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
            alert.setTitle("Create Marker");
            alert.setButton(DialogInterface.BUTTON_POSITIVE, "Take Photo", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                //TODO Auto=generated method stub

                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photo));
                o = Uri.fromFile(photo);
                startActivityForResult(intent, TAKE_PICTURE);

                BitmapDrawable bd = (BitmapDrawable) Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.jpg").getAbsolutePath());
                Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.8),
                                                                    (int) (bd.getIntrinsicWidth() * 0.8), false);

                OverlayItem overlayItem = new OverlayItem(touchedPoint, "What's Up", "2nd String");
                CustomPinPoint custom = new CustomPinPoint(d, MainActivity.this);
                custom.insertPinpoint(overlayItem);
                overlayList.add(custom);


            }

            });

            alert.show();
            {
        return true;
        }
        }
        return false;

and my CustomPinPoint.java

public class CustomPinPoint extends ItemizedOverlay<OverlayItem>{

BitmapDrawable bd;
Bitmap b;
private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context c;


public CustomPinPoint(Drawable defaultMarker) {
    super(boundCenter(defaultMarker));
    BitmapDrawable bd = (BitmapDrawable) Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.jpg").getAbsolutePath());
    Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.8),
                                                        (int) (bd.getIntrinsicWidth() * 0.8), false);
    // TODO Auto-generated constructor stub
}
public CustomPinPoint(Drawable m, Context context) {
    this(m);
    c = context;
    // TODO Auto-generated constructor stub

}
@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return pinpoints.get(i);
}
@Override
public int size() {
    // TODO Auto-generated method stub
    return pinpoints.size();
}
public void insertPinpoint(OverlayItem item){
    pinpoints.add(item);
    this.populate();
}
public void add(CustomPinPoint custom) {
    // TODO Auto-generated method stub

}
public void add(OverlayItem i) {
    // TODO Auto-generated method stub

}
}

Can anyone tell me what I am doing wrong?

Allrounder
  • 685
  • 2
  • 9
  • 20
  • Get inspired by reading this: http://stackoverflow.com/questions/5126654/android-image-scaling – rosco Nov 19 '12 at 14:34
  • This may be helpful to scale down image http://stackoverflow.com/questions/13116634/android-customize-photo-size/13116973#13116973 – MKJParekh Nov 19 '12 at 14:44

1 Answers1

0

To resize bitmaps, I use a function from http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/

It's working very well. Give it a try !!

Otherwise, to simply add a bitmap on a specified location on a map, do as shown:

class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.map_pin);   
            int pinPointPositiony = bmp.getWidth();
            int pinPointPositionx = bmp.getHeight() / 2;

            canvas.drawBitmap(bmp, screenPts.x - pinPointPositionx, screenPts.y - pinPointPositiony, null);         
            return true;
        }
    }

You can see under the comment "add the marker", I load a bitmap from my ressources. But you can use the one you get from the camera. Under again, there is some little calculation to ensure that the image will be placed this way: The center bottom of the image will indicate the provided coordinates for use with that image for exemple : http://fadigeorge.files.wordpress.com/2011/03/pin_6.png

And here is the code to add in your main class (Activity) :

map = (MapView)findViewById(R.id.map);
        MapController mc = map.getController();
        double lat = 41.39357371045376;
        double lng = 3.888921489715576;

        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(14); 


        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = map.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        

        map.invalidate();

Hope it helps.

pintaf
  • 130
  • 1
  • 10
  • Just to be sure. Do i place the code in the CustomPinPoint Class? If so, then i am getting syntax errors by ( and , at Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth){ – Allrounder Nov 19 '12 at 15:17
  • Well. There is a lot of problems in your code. first in mainActivity: You create a photo : File photo = new File ... And then, when you create you bitmap drawable, you create a new File object instead of using the object photo you've just created. secondly, you create a bitmap named b, and when you create your custom pinpoint, you pass a variable named d. – pintaf Nov 19 '12 at 16:36
  • Looks like the variable b is never used. You should proceed step by step : First try to load the image in an imageView to verify that the imaged is resized as it should be. It didn't understood anything to your syntax error. Can you provide a more complete description ? – pintaf Nov 19 '12 at 16:43
  • I solved the syntax error. Thank you for helping me. I think what i am going to do is start the project again and just do it step by step. (it would have been helpful if i could find a tutorial on this specific project) At the moment I am just plodding along and reading variuos posts and forums, but not really helping me to understand. Do you maybe know of a tutorial I could follow and learn from? – Allrounder Nov 19 '12 at 16:53
  • Well I also had to deal some days ago with the map. I'll complete my answer to explain you how to do that – pintaf Nov 19 '12 at 16:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19772/discussion-between-keithk-and-pintaf) – Allrounder Nov 19 '12 at 17:29