如何在Android中设置相机预览画面?
我的SD卡里有一些相框 我的活动中有一个框架列表 现在,当用户单击帧时,相机预览将通过该帧显示 请看图片 如何存档此功能如何在Android中设置相机预览画面?,android,android-camera,Android,Android Camera,我的SD卡里有一些相框 我的活动中有一个框架列表 现在,当用户单击帧时,相机预览将通过该帧显示 请看图片 如何存档此功能 我对此进行了搜索,但没有找到合适的示例。您必须通过扩展SurfaceView类来创建自己的预览 将带有SurfaceView的FrameLayout作为子项,并根据您的需要进行定制 您可以使用SurfaceView创建一个CustomView来打开相机,并相应地在xml中调整其大小 创建一个扩展SurfaceView的类,并在该类中打开摄影机 CustomPreview.ja
我对此进行了搜索,但没有找到合适的示例。您必须通过扩展SurfaceView类来创建自己的预览 将带有SurfaceView的FrameLayout作为子项,并根据您的需要进行定制 您可以使用SurfaceView创建一个CustomView来打开相机,并相应地在xml中调整其大小 创建一个扩展SurfaceView的类,并在该类中打开摄影机 CustomPreview.java 现在可以在XML中添加此自定义视图
现在享受输出。您必须通过扩展SurfaceView类来创建自己的预览 将带有SurfaceView的FrameLayout作为子项,并根据您的需要进行定制 您可以使用SurfaceView创建一个CustomView来打开相机,并相应地在xml中调整其大小 创建一个扩展SurfaceView的类,并在该类中打开摄影机 CustomPreview.java 现在可以在XML中添加此自定义视图
现在享受输出。您可以使用framelayout覆盖预览并设置任何图像您可以使用framelayout覆盖预览并设置任何图像感谢重播。我将集成此解决方案并稍后返回。感谢您的重播。我将集成此解决方案,然后回来。
public class CustomPreview extends SurfaceView implements SurfaceHolder.Callback{
public static Bitmap mBitmap;
SurfaceHolder holder;
static Camera mCamera;
public CustomPreview(Context context, AttributeSet attrs) {
super(context, attrs);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.getSupportedPreviewSizes();
mCamera.setParameters(parameters);
mCamera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
/***
*
* Take a picture and and convert it from bytes[] to Bitmap.
*
*/
public static void takeAPicture(){
Camera.PictureCallback mPictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
BitmapFactory.Options options = new BitmapFactory.Options();
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
};
mCamera.takePicture(null, null, mPictureCallback);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/mySurfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.mjl.CustomPreview
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.mjl.CustomPreview>
</FrameLayout>
<LinearLayout
android:layout_below="@id/mySurfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center">
<ImageView android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
</LinearLayout>
</RelativeLayout>