引言
我有一个自定义相机应用程序,我在自定义surfaceView上显示相机预览。我需要实现的是,从图像资源创建一个叠加图像,它将显示在cameraPreview上,例如过滤器。
我一直在搜索不同主题的内容,但我还没有找到适用于我的应用的内容。
APPROACH
我一直试图将图像设置为bitamp,使用onDraw()方法和其他选项,但我还没有实现显示图像。
这是我的代码:
CODE
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" />
答案 0 :(得分:2)
做这样的事情 通过扩展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());
}
}
}
然后在你的活动中 声明预览对象并初始化Camera Object
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);
那是它。
答案 1 :(得分:1)
我做了同样的事情,但在相机预览上使用TextView。诀窍是将其从XML布局中删除并将其附加到Camera的父级上。
答案根本不直观,但我发现了如何做到这一点。 与线性布局不同,声明顺序不会在相对布局中定义Z顺序。 通过声明两个视图,我能够在Camera Preview上覆盖textview 在XML中,并以编程方式将它们覆盖在我的Activity的onCreate方法上。
假设您有一个带有TextView的XML,其中有一个漂亮的透明背景,您希望覆盖在Camera Preview框架布局上,因为为什么不呢?,它看起来很酷!:
<RelativeLayout>
<TextView
android:id="@+id/txtnombotiga"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ola ke ase"
android:textColor="#000000"
android:textSize="16sp"
android:background="#55ffffff"
/>
<FrameLayout
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="240dp">
</FrameLayout>
</RelativeLayout>
如果这样,相机会覆盖你的文字,无论如何:( 要解决它,让我们进行编程,并且: 1)从其父布局中分离TextView 2)首先安装相机后,将其连接到相机的相框布局。
继承代码
的OnCreate(){ ... blaa blaaa ......
//Create camera instance and reference to our frame Layout
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview);
//Add camera to the Frame Layout
preview.addView(mPreview);
//Get reference to the TextView you want to overlay and type something funny
TextView txt=(TextView)findViewById(R.id.txtnombotiga);
txt.setText("ola keee aseee!");
//1) Step 1, here comes the magic! dettach the textView from its original Layout
((ViewGroup)txt.getParent()).removeView(txt);
//1) Step 2, voila! attach it to the View, order matters here so it will appear on
//top of the camera!
preview.addView(txt);
这是执行此操作的一般方法,如果您需要更多详细信息,请通知我。 我需要在工作中遵守最后期限,因此我会使用我想到的第一个解决方案,有时候不是最优雅或最有效的,如果您知道更好的方法,请与我们分享!