Android:覆盖 Android 相机预览
我正在使用相机 API 并调用相机。
我想在相机预览的顶部显示标题(用于品牌)。标题是 jpeg 图像。
是否可以?任何帮助表示赞赏。
提前致谢。
我的代码如下。
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();
}
}
}
I'm using Camera API and invoking the camera.
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.
Thanks in advance.
My code goes as below.
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用SurfaceView并创建一个可以打开相机的自定义视图,并且可以相应地在XML中调整其大小。以下是伪代码。
创建一个
capturepreview.java
现在您必须包括在XML中使用SurfaceView创建的视图,例如 strong>
main.xml
现在您可以在任何
aciTivty
中使用此main.xml,该将使用imageView
打开相机。谢谢....
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.
Create a class that extends SurfaceView and open camera inside that
CapturePreview.java
Now you have to include the view that you created using SurfaceView in your xml like this
main.xml
Now you can use this main.xml in any
Acitivty
that will open a camera with aImageView
.Thanks....
您必须自己处理整个相机预览和拍照。查看位于
samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview
的示例。您可以在预览区域上拥有自己的布局并向其中添加图形。示例链接
https://developer.android.com/training/camera/cameradirect#java< /a>
You will have to handle the whole camera preview and taking picture yourself. Take a look at the Samples at
samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview
. You can have your own layout over the preview area and add your graphic to it.Sample link
https://developer.android.com/training/camera/cameradirect#java
您可以在
FrameLayout
的帮助下做到这一点,如下所示:此 相机项目将为您提供帮助。
You can do that with the help of
FrameLayout
like this:This camera project will help you.
恐怕你必须自己实现相机预览屏幕。理论上,可以通过修改其布局或创建覆盖窗口来将覆盖添加到其他应用程序。第一种方法是不可能实现的,第二种方法我认为可以实现,但这是一种黑客和容易出错的方法。
实现您自己的相机活动并不是一项非常困难的任务,但相当具有挑战性。我建议您看一下默认的相机应用程序。这是其源代码:https://github.com/android/platform_packages_apps_camera。
I'm afraid you have to implement camera preview screen yourself. In theory an overlay can be added to other application by modifying its layout or by creating an overlay window. The first way is impossible to implement and the second way can be implemented I think but it's kind of a hack and a error prone method.
Implementing you own camera activity is not a very difficult task but it's rather challenging. I would recommend you to take a look at the default Camera application. Here's its source code: https://github.com/android/platform_packages_apps_camera.
看看你的XML会是这样的:
对于java代码,你应该在你的活动中扩展SurfaceHolder.Callback,然后将相机附加到表面视图,如下所示,它是我的完整代码,请小心位图和画布,这是棘手的部分:
}
我想现在我也应该给你完整的 XML 部分。所以这里是:
共享按钮此时不起作用,但它会在单击捕获时保存整个框架布局。
希望这会有所帮助。
look your XML would be like this:
and for the java code you should extend SurfaceHolder.Callback in your activity and then attach the camera to the surface view like this,its my full code please be careful with bitmaps and canvas that is the tricky part :
}
and i think now i should give you the full XML part too. so here it is:
share button doesn't work at this point but it saves the whole frame layout on capture clicked.
hope it will be helpful.
替代解决方法是将活动 XML 文件与另一个包含标题图像的 XML 文件覆盖。为此:
overlay.xml
在其中插入一个
ImageView
,例如:然后在 Activity java 文件中,即
MotionDetector.java
文件,创建一个新方法
addView()
:最后从
onCreate()
调用addView()
方法来添加图像:然后最终结果将是图像覆盖在
>SurfaceView
。根据标题图像的质量,标题的渲染质量应看起来是原始的。希望有帮助。A workaround alternative is to overlay the Activity XML file with another XML file which contains the header image. To do so:
overlay.xml
Insert an
ImageView
inside it, something like:Then inside the Activity java file, ie
MotionDetector.java
file ,create a new method
addView()
:Finally invoke the
addView()
method fromonCreate()
to add the image:Then end result would be an image overlay above the
SurfaceView
. Subject to the quality of the header image, the rendered quality of the header shall seem original. Hope it helps.