gpt4 book ai didi

android - ImageView 在 Camera Surface View 后面移动

转载 作者:行者123 更新时间:2023-12-05 07:57:32 28 4
gpt4 key购买 nike

我试图将 ImageView 放置在相机表面 View 上,当应用程序启动时, ImageView 位于相机预览之上,但在拍摄照片并将其设置为 ImageView 后,相机预览嵌套在移动中的 RelativeLayout当调用 startpreview 再次启动相机时向前,因此相机预览最终位于 imageview 之上。在 xml 布局中的 imageview 之后放置了相同布局中的按钮,这些按钮不受影响。如果我在拍完一张照片后没有再次启动相机, ImageView 就会有它的图像并且是可见的。转储 View 层次结构验证相同。据我所知,这应该是相当简单的事情,所以我不确定我哪里出错了。

更新:我还尝试在相机启动后以编程方式添加一个带有位图的全新 ImageView,并且 ImageView 也向后移动。

XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:id="@+id/add_activity"
android:layout_height="fill_parent"
>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/camera_preview"
/>

<ImageView
android:layout_width="fill_parent"
android:id="@+id/beforeImageView"
android:src="@drawable/app_icon"
android:alpha="0.2"
android:layout_height="fill_parent" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="@+id/close_camera"
android:layout_gravity="bottom|left"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:gravity="bottom|left"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />

<Button
android:id="@+id/button_capture"
android:text="Before"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dp"
android:background="@color/purple"
android:textColor="@color/white"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add_progress_bar"
android:layout_gravity="center" />

</FrameLayout>

Java:

public class AddActivity extends Activity {

private Camera mCamera;
CameraPreview mPreview;
private RelativeLayout preview;
boolean useFrontCamera = true;
boolean tookBeforeImage = false;
boolean tookAfterImage = false;
Bitmap beforeImage;
ProgressBar progressBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_add);

//Hide Notification and Action Bars
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.hide();

progressBar = (ProgressBar) findViewById(R.id.add_progress_bar);
progressBar.setVisibility(View.INVISIBLE);

boolean cameraCheck = checkCameraHardware(getApplicationContext());

if (cameraCheck) {

StartCamera cameraLaunch = new StartCamera();
cameraLaunch.execute();

}

Button closeCameraButton = (Button) findViewById(R.id.close_camera);
closeCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
releaseCamera();
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.show();
finish();
}
});

Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null,null,mPicture);
}
});
}


private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
return false;
}
}

private void releaseCamera() {
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}

@Override
protected void onPause() {
super.onPause();
releaseCamera();
}

@Override
public void onBackPressed() {
releaseCamera();
super.onBackPressed();
}

@Override
protected void onResume() {
super.onResume();
//Hide Notification and Action Bars
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.hide();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_add, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
//return true;
}

return super.onOptionsItemSelected(item);
}

private class StartCamera extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}

@Override
protected String doInBackground(String... params) {
try {
if (useFrontCamera) {
mCamera = Camera.open();
mCamera.enableShutterSound(true);
} else {
//Flip Camera
}

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

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
mPreview = new CameraPreview(getApplicationContext(),mCamera);
preview = (RelativeLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview,0);
progressBar.setVisibility(View.INVISIBLE);

}
}



private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {

Display display = getWindowManager().getDefaultDisplay();
int rotation = 0;
switch (display.getRotation()) {
case Surface.ROTATION_0: // This is display orientation
rotation = 90;
break;
case Surface.ROTATION_90:
rotation = 0;
break;
case Surface.ROTATION_180:
rotation = 270;
break;
case Surface.ROTATION_270:
rotation = 180;
break;
}

Bitmap bitmap = ImageTools.toBitmap(data);
bitmap = ImageTools.rotate(bitmap, rotation);

if (!tookBeforeImage) {
beforeImage = bitmap;
ResetCamera cameraReset = new ResetCamera();
cameraReset.execute();

Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setText("After");
tookBeforeImage = true;

} else if (tookBeforeImage == true && tookAfterImage == false) {

} else {

}




}
};

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;

public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
this.setBackgroundColor(Color.TRANSPARENT);
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}



public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("Camera Error", "Error setting camera preview: " + e.getMessage());
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.

if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}

// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}

// set preview size and make any resize, rotate or
// reformatting changes here

// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();

} catch (Exception e){
Log.d("Camera Error", "Error starting camera preview: " + e.getMessage());
}
}
}

private static class ImageTools {
public static Bitmap toBitmap(byte[] data) {
return BitmapFactory.decodeByteArray(data, 0, data.length);
}

public static Bitmap rotate(Bitmap in, int angle) {
Matrix mat = new Matrix();
mat.postRotate(angle);
return Bitmap.createBitmap(in, 0, 0, in.getWidth(), in.getHeight(), mat, true);
}
}

private class ResetCamera extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}

@Override
protected String doInBackground(String... params) {
mCamera.startPreview();
return null;
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
ImageView beforeImageView = (ImageView) findViewById(R.id.beforeImageView);
beforeImageView.setImageBitmap(beforeImage);
beforeImageView.setImageAlpha(100);

//Trying to add programmatically still ends up behind Camera Preview
ImageView newBefore = (ImageView) new ImageView(getApplicationContext());
newBefore.setImageBitmap(beforeImage);
newBefore.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
preview.addView(newBefore,0);

progressBar.setVisibility(View.INVISIBLE);
}
}




}

最佳答案

这是由于试图加载太大的位图,它在日志中悄悄地抛出一条通知,因为它无法创建纹理。减小位图的大小解决了这个问题。

关于android - ImageView 在 Camera Surface View 后面移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26813852/

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