2012-03-22 4 views
1

나는 사진 찍을 때 GPS 위치를 얻으려고했다. LocationChanged 메서드가 변수에서 실행될 때 location-listener의 위치를 ​​저장하고 사진을 찍을 때 표시하려고합니다. 이것이 올바른 방법일까요? 나는 android 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() 대신 하나의 업데이트 만 원할 수 있습니다. 또한 GPS 정보가 도착할 때까지 기다리지 말아야합니다 (GPS에서 가능한 춥긴 시작과 반대로, 더 빨리 응답 할 수있는 모든 제공 업체와 같이 GPS 공급자와 다른 것을 지정할 수도 있습니다). 작동하지 않으면 항상 마지막으로 알려진 위치를 쿼리 할 수 ​​있습니다. 잠시 동안 업데이트가 도착하지 않을 수 있으므로 위치 정보가없는 느린 위치에 이미지 데이터를 저장 한 다음 위치를 사용할 수있게되면 정보를 업데이트해야합니다.

+0

사진의 시점에서 어떻게 위치를 얻을 수 있습니까? 어떻게 위치 검색을 활성화 할 수 있습니까? – user1284946

+0

수 없습니다. GPS가 시작되면 GPS가 워밍업되고 위치가 수정되는 데 시간이 걸릴 수 있습니다. 아직 문제가 해결되지 않았다면 시작하기 전에 자신이 어디에 있는지 알 수있는 방법이 없습니다. 대신에 사진을 찍을 때 업데이트를 요청한 다음 몇 초/몇 분 후에 요청한 위치 정보를 얻으실 수 있습니다. 위치를 즉시 얻을 수있는 마법의 방법은 없습니다. –