gpt4 book ai didi

android - 如何在android中单击按钮从前到后和从后到前更改相机

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:10 25 4
gpt4 key购买 nike

我正在开发 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;
}

}

最佳答案

您可以使用 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();

关于android - 如何在android中单击按钮从前到后和从后到前更改相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16374666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com