如何在android中点击按钮,从前到后再回到前面

时间:2013-05-04 13:12:45

标签: android camera

我正在开发Android平板电脑应用程序。我想要从前到后更换相机,然后在按钮上返回前面。我怎样才能做到这一点?我尝试了几个例子,但没有得到适当的回应。

我也在添加代码。

public class PhotoPreview extends Activity implements SurfaceHolder.Callback {

private Camera camera;
private ImageButton cameraClick;
private ImageButton cameraSwap;
SurfaceView surfaceView;
private SurfaceHolder mHolder;
boolean previewing = false;
String path = "";
LayoutInflater controlInflater = null;
Bitmap bmp;
Button cameraCancel;
private SharedPreferences myPrefs;
private int camId;

/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    System.out.println("Photo preview Called $$$$$$$$$$$$$$$$$$ ");

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.photo_preview);

    myPrefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
    camId = myPrefs.getInt("camId",1);

    System.out.println("CAM ID $$$$$$$$$$$$$$$$$$ "+camId);

    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView) findViewById(R.id.camerapreview);

    mHolder = surfaceView.getHolder();
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mHolder.addCallback(this);


    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    LayoutParams layoutParamsControl = new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    this.addContentView(viewControl, layoutParamsControl);
    cameraClick = (ImageButton) findViewById(R.id.cameraClick);
    cameraClick.setOnClickListener(cameraClickListener);
    cameraCancel = (Button) findViewById(R.id.cameraCancel);
    cameraCancel.setOnClickListener(cameraCancelClickListener);
    cameraSwap = (ImageButton) findViewById(R.id.cameraSwap);
    cameraSwap.setOnClickListener(swapCameraClickListener);

}

/*
 * This is click event of Photo capture button
 */
private OnClickListener cameraClickListener = new OnClickListener() {
    @Override
    public void onClick(final View v) {
        if (camera != null) {
            camera.takePicture(shutterCallback, rawCallback, jpegCallback);
        }

    }
};

/*
 * This is click event of camera cancel button
 */
private OnClickListener cameraCancelClickListener = new OnClickListener() {
    @Override
    public void onClick(final View v) {

        Intent intent = new Intent(PhotoPreview.this,
                MainScreenActivity.class);
        startActivity(intent);

    }
};

/*
 * This is click event of camera cancel button
 */
private OnClickListener swapCameraClickListener = new OnClickListener() {
    @Override
    public void onClick(final View v) {

        if (camId == 0) {
            SharedPreferences.Editor prefsEditor = myPrefs.edit();
            prefsEditor.putInt("camId", 1);
            prefsEditor.commit();

        } else {
            SharedPreferences.Editor prefsEditor = myPrefs.edit();
            prefsEditor.putInt("camId", 0);
            prefsEditor.commit();
        }
        System.out.println("CAM ID ^^^^^^^^^^^^^^^^^^^^ "+camId);
        Intent intent = new Intent(PhotoPreview.this, PhotoPreview.class);
        startActivity(intent);

    }
};

// Handles when shutter open
ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {

    }
};

/** Handles data for raw picture */
PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {

    }
};

/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/Easy_Measurement_images");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-" + n + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists())
            file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        Intent intent = new Intent(PhotoPreview.this,
                VerticalAdjustmentActivity.class);
        startActivity(intent);
    }
};

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

    // Set camera preview size,orientation,rotation using parameters
    if (camera != null) {
        Camera.Parameters parameters = camera.getParameters();
        parameters.set("orientation", "portrait");
        camera.setParameters(parameters);
        camera.startPreview();
    }

}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    System.out.println("CAM ID %%%%%%%%%%%%%%$$$$$$$$$$$$$$$$$$ "+camId);
    camera = Camera.open(camId);

    if (camera != null) {
        try {
            camera.setPreviewDisplay(holder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

    System.out.println("Surface destroyed ***************");
    if (camera != null) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }

}

}

1 个答案:

答案 0 :(得分:0)

您可以使用Camera.open(int i)打开相机。你一次只能使用一个摄像头。释放一个,然后打开另一个。例如

public void onClick(View v) {

camera.stopPreview();

camera.release();


if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
else {
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}

camera = Camera.open(currentCameraId);

setCameraDisplayOrientation(CameraActivity.this, currentCameraId, camera);
try {
    camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
    e.printStackTrace();
}
camera.startPreview();

}