2012-03-22 14 views
1

写真を撮るときにgpsの位置を取得しようとしました。 LocationChangedメソッドが変数で実行されているときに位置リスナーの位置を保存し、写真を撮るときにその位置を表示したいと考えています。これは正しい方法ですか?私はアンドロイド2.3.6を使用します。 ここに私のコード:写真を撮った後にgpsの位置を取得する

package com.exercise; 



import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.OutputStream; 

import android.app.Activity; 
import android.content.ContentValues; 
import android.content.Context; 
import android.content.pm.ActivityInfo; 
import android.graphics.PixelFormat; 
import android.hardware.Camera; 
import android.hardware.Camera.AutoFocusCallback; 
import android.hardware.Camera.PictureCallback; 
import android.hardware.Camera.ShutterCallback; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore.Images.Media; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.Toast; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{ 

    Camera camera; 
    SurfaceView surfaceView; 
    SurfaceHolder surfaceHolder; 
    boolean previewing = false; 
    LayoutInflater controlInflater = null; 

    Button buttonTakePicture; 

    final int RESULT_SAVEIMAGE = 0; 

    public Location test; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

     LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     LocationListener mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

     getWindow().setFormat(PixelFormat.UNKNOWN); 
     surfaceView = (SurfaceView)findViewById(R.id.camerapreview); 
     surfaceHolder = surfaceView.getHolder(); 
     surfaceHolder.addCallback(this); 
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

     controlInflater = LayoutInflater.from(getBaseContext()); 
     View viewControl = controlInflater.inflate(R.layout.control, null); 
     LayoutParams layoutParamsControl 
      = new LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT); 
     this.addContentView(viewControl, layoutParamsControl); 

     buttonTakePicture = (Button)findViewById(R.id.takepicture); 
     buttonTakePicture.setOnClickListener(new Button.OnClickListener(){ 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       camera.takePicture(myShutterCallback, 
         myPictureCallback_RAW, myPictureCallback_JPG); 
      }}); 

     LinearLayout layoutBackground = (LinearLayout)findViewById(R.id.background); 
     layoutBackground.setOnClickListener(new LinearLayout.OnClickListener(){ 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       buttonTakePicture.setEnabled(false); 
       camera.autoFocus(myAutoFocusCallback); 
      }}); 
    } 

    public class MyLocationListener implements LocationListener{ 

     @Override 

     public void onLocationChanged(Location loc){ 
      loc.getLatitude(); 
      loc.getLongitude(); 
      String Text = "Lat = " + loc.getLatitude() + "|Long = " + loc.getLongitude(); 
      Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show(); 
      // final TextView tv = (TextView) findViewById(R.id.text); 
      // tv.setText(tv.getText()+ "||" + Text); 
      test = loc; 
     } 

     @Override 

     public void onProviderDisabled(String provider){ 
      Toast.makeText(getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 

     public void onProviderEnabled(String provider){ 
      Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); 
     } 

     @Override 

     public void onStatusChanged(String provider, int status, Bundle extras){ 
     } 

    } 

    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){ 

     @Override 
     public void onAutoFocus(boolean arg0, Camera arg1) { 
      // TODO Auto-generated method stub 
      buttonTakePicture.setEnabled(true); 
     }}; 

    ShutterCallback myShutterCallback = new ShutterCallback(){ 

     @Override 
     public void onShutter() { 
      // TODO Auto-generated method stub 

     }}; 

    PictureCallback myPictureCallback_RAW = new PictureCallback(){ 

     @Override 
     public void onPictureTaken(byte[] arg0, Camera arg1) { 
      // TODO Auto-generated method stub 

     }}; 

    PictureCallback myPictureCallback_JPG = new PictureCallback(){ 

     @Override 
     public void onPictureTaken(byte[] arg0, Camera arg1) { 
      // TODO Auto-generated method stub 
      /*Bitmap bitmapPicture 
       = BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */ 

      Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues()); 

      OutputStream imageFileOS; 
      try { 
       imageFileOS = getContentResolver().openOutputStream(uriTarget); 
       imageFileOS.write(arg0); 
       imageFileOS.flush(); 
       imageFileOS.close(); 

//    Toast.makeText(AndroidCamera.this, 
//      "Image saved: " + uriTarget.toString(), 
//      Toast.LENGTH_LONG).show(); 
//    test.getLatitude(); 
//    test.getLongitude(); 
//    String Text = "Lat = " + test.getLatitude() + "|Long = " + test.getLongitude(); 
//    Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show(); 
       if (test==null){ 
        Toast.makeText(AndroidCamera.this, "null", Toast.LENGTH_SHORT).show(); 
       } 
       else{ 
        test.getLatitude(); 
        String ausgabe = String.valueOf(test.getLatitude()); 
        Toast.makeText(AndroidCamera.this, ausgabe, Toast.LENGTH_SHORT).show(); 
       } 

      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      camera.startPreview(); 
     }}; 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
     // TODO Auto-generated method stub 
     if(previewing){ 
      camera.stopPreview(); 
      previewing = false; 
     } 

     if (camera != null){ 
      try { 
       camera.setPreviewDisplay(surfaceHolder); 
       camera.startPreview(); 
       previewing = true; 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
     camera = Camera.open(); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
     camera.stopPreview(); 
     camera.release(); 
     camera = null; 
     previewing = false; 
    } 
} 

答えて

1

はい、これは公正な方法です。ちなみに、あなたは代わりに.requestSingleUpdate()を使用することができます。これは、1つの更新のみが必要なためです。おそらく、GPS情報が到​​着するまで待つべきではありません(また、GPSからのコールドスタートとは対照的に、すばやく応答できる任意のプロバイダなど、gpsプロバイダとは異なるものを指定することもできます)。それがうまくいかない場合は、最後に知っている場所を常に照会することができます。しばらくの間更新が到着しない可能性があるので、(位置情報なしで)ゆっくりと画像データを保存し、場所が利用可能になったときに情報を更新するのがよいでしょう。

+0

写真の瞬間にどのようにポジションを得ることができますか、ポジションの検索をどのようにアクティブにすることができますか? – user1284946

+0

できません。 GPSが始動すると、ウォームアップしてその場所を修正するまでに時間がかかることがあります。まだ修正されていない場合は、開始する前にどこにいるかを知る方法がありません。代わりに、あなたが写真を撮るときに、あなたはアップデートを開始し、そして数秒後にあなたの場所を修正してほしい。場所を即座に取得する魔法の方法はありません。 –

関連する問題