2012-08-23 3 views
0

메인 액티비티의 프레임 레이아웃에서 카메라를 미리 볼 수있는 Android 카메라 애플리케이션을 설정했습니다. 카메라 고유의 기능을 사용하지 않고 하드웨어에서 완전히 독립적으로하고 싶습니다. 미리보기의 중심에있는 150 x 150 픽셀의 선은 픽셀 주위에 상자를 표시하거나 150 x 150 이외의 픽셀을 사용합니다. 또한 사진을 찍지 않고이 픽셀을 읽고 싶습니다. 궁극적으로 내가 할 효과는 카메라가 움직이면서 150x150 영역의 적색, 녹색 및 청색 농도 변화를 표시한다는 것입니다. 중첩 된 for 루프를 수행하여 픽셀에서 색상을 얻는 방법을 알고 있지만 픽셀을 읽기 시작하는 높이 및 두께에 대한 오프셋을 얻는 방법을 알아야합니다. 도움을 주셔서 감사합니다. 마이크카메라 미리보기에서 150 x 150 픽셀을 적극적으로 읽습니다.

public class MyActivity extends Activity { 
    final public String TAG = "MyActivity"; 
    static private Camera mCamera = null; 
    static public Camera getCamera() { return mCamera; } 
    private static boolean mBPreviewingCamera = false; 
    private PreviewSurface mPS; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     String lMTH = "onCreate "; 
     Log.i(TAG, lMTH + "Start"); 
     setContentView(R.layout.activity_color_temperature_estimator); 
     if (isThereACamera()) { 
      mPreviewCamera(true); 
      TextView lTVTemp = (TextView) findViewById(R.id.TV_TEMP); 
      lTVTemp.setText("Camera exists able to proceed"); 
      lTVTemp = null; 
     } else { 
      Toast.makeText(this, "No camera! Uunable to proceed", Toast.LENGTH_LONG).show(); 
      TextView lTVTemp = (TextView) findViewById(R.id.TV_TEMP); 
      lTVTemp.setText("No camera! Uunable to proceed"); 
      lTVTemp = null; 
     } 
    } 

    public void mPreviewCamera(boolean pBPreviewingCamera) { 
     String lMTH = "mPreviewCamera "; 
     try { 
      if (pBPreviewingCamera) { 
       Log.i(TAG, lMTH + "Open Camera"); 
       mCamera = Camera.open(); 
       mPS = new PreviewSurface(this); 
       FrameLayout lFVPreview = (FrameLayout) findViewById(R.id.FL_Preview); 
       lFVPreview.addView(mPS); 
       lFVPreview = null; 

       mBPreviewingCamera = true; 
       Button lBTNTemp = (Button) findViewById(R.id.BTN_CLOSE); 
       lBTNTemp.setText(R.string.BTN_CLOSE); 
       lBTNTemp = null; 
      } else { 
       Log.i(TAG, lMTH + "Open Camera"); 
       mCloseCamera(); 
       Button lBTNTemp = (Button) findViewById(R.id.BTN_CLOSE); 
       lBTNTemp.setText(R.string.BTN_OPEN); 
       lBTNTemp = null; 
      } 

     } catch (Exception e) { 
      Log.i(TAG, lMTH + e.getMessage()); 
      mCloseCamera(); 
      e.printStackTrace(); 
     } 
    } 


    public void mCloseCamera() { 
     String lMTH = "mCloseCamera "; 
     Log.i(TAG, lMTH + "Turn Camera Off"); 
     if (mCamera != null) { 
      mCamera.stopPreview(); 
      mCamera.release(); 
     } 
     mCamera = null; 
     mBPreviewingCamera = false; 
    } 


    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     String lMTH = "onPause "; 
     Log.i(TAG, lMTH + "Turn Camera Off"); 
     mPreviewCamera(false); 
    } 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     String lMTH = "onDestroy "; 
     Log.i(TAG, lMTH + "Turn Camera Off"); 
     mPreviewCamera(false); 
    } 

    /** Check if this phone has a camera */ 
    private boolean isThereACamera() { 
     if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ 
      // There is a camera 
      return true; 
     } else { 
      // There isn't a camera 
      return false; 
     } 
    } 

    public void mBTN(View view) { 
     mPreviewCamera(!mBPreviewingCamera); 
    } 
} 




public class PreviewSurface extends SurfaceView implements SurfaceHolder.Callback { 
    final static public String TAG = "PreviewSurface"; 
    private SurfaceHolder mSH; 

    /** 
    * @param context 
    */ 
    public PreviewSurface(Context pContext) { 
     super(pContext); 
     // TODO Auto-generated constructor stub 
     String lMTH = "PreviewSurface(Context pContext) "; 
     Log.i(TAG, lMTH + "Constructor"); 

     pContext = null; 
     mSH = getHolder(); 
     mSH.addCallback(this); 
     mSH.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    } 


    /* (non-Javadoc) 
    * @see android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder, int, int, int) 
    */ 
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { 
     String lMTH = "surfaceChanged "; 
     // TODO Auto-generated method stub 
     Log.i(TAG, lMTH + "arg1 = " + arg1 + " arg2 " + arg2 + " arg3 " + arg3); 
     if (arg0.getSurface() != null) { 
      // stop old preview if it exists 
      try { 
       Log.i(TAG, lMTH + "Stop Preview "); 
       MyActivity.getCamera().stopPreview(); 
      } catch (Exception e) { 
       Log.i(TAG, lMTH + "Stoping preview exception " + e.getMessage()); 

      } 

      // start new preview 
      try { 
       Log.i(TAG, lMTH + "Set Preview "); 
       MyActivity.getCamera().setPreviewDisplay(arg0); 
       Log.i(TAG, lMTH + "Start Preview "); 
       MyActivity.getCamera().startPreview(); 

      } catch (Exception e){ 
       Log.i(TAG, lMTH + "Starting camera preview exception " + e.getMessage()); 
      } 
      } 

      // stop preview before making changes 

    } 

    /* (non-Javadoc) 
    * @see android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder) 
    */ 
    public void surfaceCreated(SurfaceHolder arg0) { 
     String lMTH = "surfaceCreated "; 
     // TODO Auto-generated method stub 
     try { 
      Log.i(TAG, lMTH + "Set Preview "); 
      MyActivity.getCamera().setPreviewDisplay(arg0); 
      Log.i(TAG, lMTH + "Start Preview "); 
      MyActivity.getCamera().startPreview(); 
     } catch (Exception e) { 
      Log.i(TAG, lMTH + "Exception " + e.getMessage()); 
      e.printStackTrace(); 
     } 

    } 

    /* (non-Javadoc) 
    * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder) 
    */ 
    public void surfaceDestroyed(SurfaceHolder arg0) { 
     String lMTH = "surfaceDestroyed "; 
     // TODO Auto-generated method stub 
     Log.i(TAG, lMTH + "Activity will handle destroy"); 

    } 

답변

1

쉬운 방법 "나는로부터 픽셀을 읽기 시작하는 신장과 체중 오프셋하는 방법을 알고 있어야합니다"에 대한 오프하려면 당신의 중첩 된 for-loop에는 설명 변수가 포함되어 있습니다.

int xWidth = 150; // The length you want to concentrate on 
int yHeight = 150; 
int xCenter = previewWidth/2; 
int yCenter = previewHeight/2; 
int xStart = xCenter - xWidth/2; 
int xEnd = xCenter + xWidth/2; 
int yStart = yCenter - yHeight/2; 
int yEnd = yCenter + yHeight/2; 

for(int y = yStart; y < yEnd; y++) { 
    for(int x = xStart; x < xEnd; x++) { 
     // read pixels 
    } 
} 

previewWidth 및 previewHeight는 쉽게 확인할 수 있습니다. faceChange() : 당신은 내가이 문제를 걸었다 방법을 명확하게 볼 수 있도록

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
    previewWidth = w; 
    previewHeight = h; 
    ... 
} 

이유는 내가 여기에 많은 변수를 사용입니다.

어떻게 진행되는지 알려주세요.

관련 문제