我已经设置了一个Android相机应用程序,允许我在主Activity中的Frame Layout中预览相机。我想做什么(不使用相机专用功能;我希望它完全独立于硬件。)在预览中心的150 X 150像素线上显示一个框围绕像素或模糊所有的除了150 x 150之外的像素。另外,没有拍照,我想阅读这些像素。最终我想要的效果是,当相机移动时,我会在150 x 150区域内显示红色,绿色和蓝色浓度的变化。我知道如何做嵌套for循环并从像素中获取颜色,但我需要知道如何获得高度和重量的偏移量来开始读取像素。 在此先感谢您的所有帮助! 麦克
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");
}
答案 0 :(得分: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
}
}
可以从surfaceChange()中轻松获取previewWidth和previewHeight:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
previewWidth = w;
previewHeight = h;
...
}
我在这里使用很多变量的原因是你可以清楚地看到我是如何解决这个问题的。
让我知道它是怎么回事。