画布蓝调

时间:2015-01-17 06:52:54

标签: android canvas gps

开发应用程序以拍摄照片并在其上绘制GPS坐标。 刚刚对drawtext()方法进行了测试,该方法没有显示出来。 GPS工作正常。 相机工作正常。 画布有问题。

mainactivity.java的代码

package com.example.camera;

    import android.os.Bundle;
    import android.app.Activity;

    import android.widget.ImageView;
    import android.widget.Button;
    import android.view.View;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.net.Uri;
    import android.content.Context;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.provider.MediaStore;
    import java.io.File;
    import java.io.FileOutputStream;

    import android.os.Environment;
    import android.util.Log;
    import java.text.SimpleDateFormat;
    import java.util.Locale;
    import java.util.Date;
    import android.widget.Toast;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.PorterDuff;
    import android.graphics.PorterDuffXfermode;

public class MainActivity extends Activity  
{
    double lat,lon;
    private static final String TAG = "CallCamera";
    private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;
        private String photoUri=null;
    Uri fileUri = null;
    ImageView photoImage = null;

    private File getOutputPhotoFile() 
    {
          File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), getPackageName());
        Log.d("directory check ", directory.toString());
          if (!directory.exists()) 
          {
            if (!directory.mkdirs()) 
            {
              Log.e(TAG, "Failed to create storage directory.");
              return null;
            }
          }

          String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss", Locale.US).format(new Date());
          return new File(directory.getPath() + File.separator + "IMG_"  
                  + timeStamp + ".jpg");

    }

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

        photoImage = (ImageView) findViewById(R.id.photo_image);

        try
        {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
        }
        catch(Exception e)
        {
            Log.d("GPS","Exception in location part-->"+e.toString());
        }


        Button callCameraButton = (Button) 
        findViewById(R.id.button_callcamera);

        callCameraButton.setOnClickListener( new View.OnClickListener() {
            public void onClick(View view) {
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                fileUri = Uri.fromFile(getOutputPhotoFile());
                i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
            }
        }
        );
    }


    private class mylocationlistener implements LocationListener 
    {
    @Override
    public void onLocationChanged(Location location) 
    {
        if (location != null) 
        {
        Log.d("LOCATION CHANGED", location.getLatitude() + "");
        Log.d("LOCATION CHANGED", location.getLongitude() + "");
        lat=location.getLatitude();
        lon=location.getLongitude();
        }
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) 
    {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) 
          {
            if (resultCode == RESULT_OK) 
            {
              Uri photoUri = null;
              if (data == null) 
              {
                // A known bug here! The image should have saved in fileUri
                Log.d("Image Code","Image Saved");
                  photoUri = fileUri;
              } 
              else 
              {
                photoUri = data.getData();
                Log.d("Image Code","Image Saved at"+data.getData());
              }
              drawLatLong(photoUri.getPath());
            } 
            else if (resultCode == RESULT_CANCELED) 
            {
            Log.d("Image Code", "Cancled");
            }
            else 
            {
              Log.d("Image Code", "Callout for image capture failed!");
            }
          }
    }


    /**
     * takes the PhotoURI and draws the Latitude and longitude over it
     * @param photoUri
     */
    private void drawLatLong(String photoUri) 
    {
          File imageFile = new File (photoUri);
          if (imageFile.exists())
          {
             Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

            // Resources res=getResources();
          // BitmapDrawable drawable = new BitmapDrawable(res,bitmap);

            try
             {

                 Canvas canvas = new Canvas(bitmap);

                    Paint paint = new Paint();
                    paint.setColor(Color.WHITE); // Text Color
                    paint.setStrokeWidth(12); // Text Size
                    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
                    // some more settings...

                    canvas.drawBitmap(bitmap, 0, 0, paint);
                    canvas.drawText("Testing...", 10, 10, paint);
-
                 FileOutputStream fOut = new FileOutputStream(photoUri);
                 Log.d("Path"," "+photoUri);
                 bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                 fOut.flush();
                 fOut.close();

                 System.out.println(photoUri);

                 //bitmap.recycle();
                // System.gc();

             }
             catch(Exception ex)
             {
                 Log.d("Canvas Part", "Exception caused in canvas drawing-->"+ex.toString());
             }
            showPhoto(bitmap);

        }

    }

    /**
     * Takes the Bitmap from the drawLatLong() method and displays it over the image view.
     * @param bitmapShow
     */
    private void showPhoto(Bitmap bitmapShow) 
    {     
        try
        {
         Resources res=getResources();
       BitmapDrawable drawable = new BitmapDrawable(res,bitmapShow);
           photoImage.setImageDrawable(drawable);
            photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
            photoImage.setImageBitmap(bitmapShow);            
        }
        catch(Exception ex)
        {
            Log.d("canvas","exception in canvas"+ex);
        }
        }   
}

清单文件

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.camera"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CAMERA"/>

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission 
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-feature android:name="android.hardware.camera" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.camera.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

位图需要是inMutable。使用以下代码设置它。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(),options);

由于画布已经具有此位图,因此不需要此部分代码。

//Not needed:
//canvas.drawBitmap(bitmap, 0, 0, paint);