我已经设置了一个 Android 相机应用程序,它允许我在我的主 Activity 的框架布局中预览相机。我想做的是(不使用相机特定功能;我希望它完全独立于硬件。)通过在像素周围显示一个框或模糊所有除了 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");
}