gpt4 book ai didi

android - 在 cameraPreview 上叠加图像

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:54 24 4
gpt4 key购买 nike

简介

我有一个自定义相机应用程序,我在自定义 surfaceView 上显示相机预览。我需要实现的是,从图像资源创建一个叠加图像,它将显示在 cameraPreview 上,例如滤镜。

我一直在不同的主题中搜索此内容,但没有找到适用于我的应用程序的内容。

方法

我一直在尝试使用 onDraw() 方法和其他选项将图像设置为 bitamp,但我还没有实现显示图像。

这是我的代码:

代码

public class CameraActivity extends Activity implements PictureCallback {

private Button btnPhoto;
CameraPreview cameraPreview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

cameraPreview = (CameraPreview) findViewById(R.id.camera_preview);
btnPhoto = (Button)findViewById(R.id.buttonTakePhoto);

btnPhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
takePhoto();
}
});
}

public void takePhoto() {
cameraPreview.getCamera().takePicture(null, null, this);
}
//...
}

_

public class CameraPreview extends FrameLayout implements SurfaceHolder.Callback {

private SurfaceView surfaceView;
private Camera camera;

public CameraPreview(Context context, AttributeSet attrs) {
super(context, attrs);

createCamera();

surfaceView = new SurfaceView(context);
addView(surfaceView);

SurfaceHolder holder = surfaceView.getHolder();
holder.addCallback(this);

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setKeepScreenOn(true);
}

private void createCamera() {
//...
try {
this.camera = Camera.open(activeCameraId);
} catch (Exception e) {
return;
}
//...
}

public Camera getCamera() {
return camera;
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
//...
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//...
}

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

_

<com.uax.cameratakephoto.CameraPreview
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" />

<Button //THIS IS THE BUTTON TO TAKE THE PHOTO
android:id="@+id/buttonTakePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@android:drawable/ic_menu_camera" />

<ImageView //THIS IS A SMALL PREVIEW OF THE LAST PHOTO TAKEN
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

最佳答案

做这样的事情通过扩展 SurfaceView 创建相机预览类

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private String TAG = "CameraPreview";

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();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

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

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
mCamera.release();
}

@Override
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
StartPreview();
}

public void StartPreview() {
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}

然后在你的 Activity 中声明预览对象并初始化相机对象

private CameraPreview preview;

之后

   // Create our Preview view and set it as the content of ur activity.
preview = new CameraPreview(this, camera);

// Create Frame layout
FrameLayout previewLayout = new FrameLayout(this);

// Create camera layout params
LinearLayout.LayoutParams previewlayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, Gravity.LEFT);

// Add preview to previewLayout
previewLayout.addView(preview, 0);

Bitmap overlayBitmap = getBitmap();
if (overlayBitmap != null) {
Matrix matrix = new Matrix();
matrix.postRotate(180);

Bitmap rotatedBitmap = Bitmap.createBitmap(overlayBitmap, 0, 0, overlayBitmap.getWidth(),
overlayBitmap.getHeight(), matrix, true);

ImageView oImageView = new ImageView(this);
oImageView.setImageBitmap(rotatedBitmap);
previewLayout.addView(oImageView, 1);
}

// Add previewLayout to main layout
linearLayout.addView(previewLayout, previewlayoutParams);

就是这样。

关于android - 在 cameraPreview 上叠加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22454312/

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