Android:Android 相机预览上的叠加层 [英] Android: Overlay on Android Camera Preview
问题描述
我正在使用相机 API 并调用相机.
I'm using Camera API and invoking the camera.
我想在相机预览的顶部显示一个标题(用于品牌宣传).标题是 jpeg 图像.
I want to display a header (for branding) on the top of the camera preview. The header is a jpeg image.
有可能吗?任何帮助表示赞赏.
Is it possible? Any help appreciated.
提前致谢.
我的代码如下.
public class CameraActivity extends Activity {
@Override
protected void onPause() {
super.onPause();
}
private static final int CAMERA_PIC_REQUEST = 2500;
private Bitmap image2;
private Bitmap bm;
public static String imagepath;
public static int x=1;
private RdmsDbAdapter dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.header);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//caling new incident
if(IncidentFormActivity.incident_id == null || IncidentFormActivity.isDisable==true){
//DBAdapter instance created and connection opened.
dbHelper = new RdmsDbAdapter(CameraActivity.this);
dbHelper.open();
//setting up flags
NewIncidentHelper nih = new NewIncidentHelper();
nih.setUpNewIncident();
//setting up incident_id
String Date= IncidentIdGenerator.getDate();
String Time = IncidentIdGenerator.getTime();
IncidentFormActivity.incident_id = IncidentIdGenerator.now("ddMMyyyyHHmmss");
dbHelper.executeSQL("insert into incident values ('" + IncidentFormActivity.incident_id
+ "', ' ', ' ', ' ', ' ', '"+Date+"', '0','0','0','0','0','0','0','0','0','"+Time+"')");
dbHelper.close();
}
//calling camera
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
//back key on phone pressed
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Intent i= new Intent(CameraActivity.this, IncidentFormActivity.class);
startActivity(i);
this.finish();
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
//handle response came from camera when the picture is taken.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
final ImageView img = new ImageView(this);
img.setLayoutParams(new LayoutParams(100, 100));
image2 = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(image2);
String incident_ID = IncidentFormActivity.incident_id;
//l2.addView(img);
imagepath="/sdcard/RDMS/"+incident_ID+ x + ".png";
File file = new File(imagepath);
try {
bm = Bitmap.createScaledBitmap( image2,400, 300, true);
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bm.compress(CompressFormat.PNG, 90, ostream);
ostream.close();
//Initialising db class and inserting values
dbHelper = new RdmsDbAdapter(CameraActivity.this);
dbHelper.open();
dbHelper.executeSQL("insert into files values ('"+imagepath+"', '"+IncidentFormActivity.incident_id+"')");
dbHelper.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"yourfirst error message is "
+ e.toString(), 1000).show();
}
x++;
final AlertDialog.Builder alert = new AlertDialog.Builder(
CameraActivity.this);
alert.setTitle(getString(R.string.anotherimage));
alert.setCancelable(false);
//alert.setMessage("Play or Delete the Video selected");
//alert.setIcon(R.drawable.vid_red);
alert.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent sendingpage = new Intent(CameraActivity.this, CameraActivity.class);
startActivity(sendingpage);
}
});
alert.setNegativeButton(getString(R.string.no),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent callback = new Intent (CameraActivity.this, IncidentFormActivity.class);
startActivity(callback);
}
});
alert.show();
}
if(resultCode==RESULT_CANCELED)
{
AlertDialog.Builder builder = new AlertDialog.Builder(CameraActivity.this);
builder.setMessage(getString(R.string.areuexit)).setCancelable(
false).setPositiveButton(getString(R.string.yes),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i= new Intent(CameraActivity.this, IncidentFormActivity.class);
startActivity(i);
CameraActivity.this.finish();
}
}).setNegativeButton(getString(R.string.no),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Intent i= new Intent(CameraActivity.this, CameraActivity.class); startActivity(i);
CameraActivity.this.finish();
}
});
builder.show();
}
}
}
推荐答案
您可以使用 SurfaceView 并创建一个将打开相机的 CustomView,您可以相应地在 xml 中调整其大小.下面是一个伪代码.
You can use SurfaceView and create a CustomView that will open the camera and you can adjust its size in the xml accordingly. Below is a pseudo code.
创建一个扩展 SurfaceView 的类并在其中打开相机
CapturePreview.java
public class CapturePreview extends SurfaceView implements SurfaceHolder.Callback{
public static Bitmap mBitmap;
SurfaceHolder holder;
static Camera mCamera;
public CapturePreview(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 中包含使用 SurfaceView 创建的视图
main.xml
<?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.cam.CapturePreview
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.cam.CapturePreview>
</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>
现在您可以在任何 Acitivty
中使用这个 main.xml,它将使用 ImageView
打开相机.谢谢....
Now you can use this main.xml in any Acitivty
that will open a camera with a ImageView
.
Thanks....
这篇关于Android:Android 相机预览上的叠加层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!