2012-08-23 70 views
0

我已經設置了一個Android相機應用程序,該應用程序允許我在主要活動的框架佈局中預覽相機。我想做什麼(不使用攝像機特定的功能;我希望它完全獨立於硬件)在預覽的中心顯示150×150像素的線,可以通過在像素周圍顯示一個框或模糊所有的像素除150 x 150外。此外,如果不拍照,我想閱讀這些像素。最終我要做的效果是,當相機移動時,我會在150 x 150區域內顯示紅色,綠色和藍色濃度的變化。我知道如何做嵌套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循環用描述變量走過:

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可以很容易地從sur faceChange():

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

我在這裏使用很多變量的原因是爲了讓您清楚地看到我是如何解決這個問題的。

讓我知道它是怎麼回事。