Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:覆盖在Android相机预览上_Android_Header_Camera - Fatal编程技术网

Android:覆盖在Android相机预览上

Android:覆盖在Android相机预览上,android,header,camera,Android,Header,Camera,我正在使用摄像头API并调用摄像头 我想在相机预览的顶部显示标题(用于品牌)。标题是jpeg图像 可能吗?谢谢你的帮助 提前谢谢 我的代码如下 public class CameraActivity extends Activity { @Override protected void onPause() { super.onPause(); } private static final int CAMERA_PIC_REQUEST = 250

我正在使用摄像头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();



            }
        }
    }

你必须自己处理整个相机预览和拍照。查看
Samples/ApiDemos/src/com/example/android/api/graphics/CameraPreview
上的示例。您可以在预览区域上拥有自己的布局,并将图形添加到其中

示例链接

恐怕您必须自己实现摄像头预览屏幕。理论上,可以通过修改布局或创建覆盖窗口将覆盖添加到其他应用程序中。第一种方法是不可能实现的,第二种方法可以实现,但我认为这是一种黑客和容易出错的方法


实施您自己的相机活动不是一项非常困难的任务,但它相当具有挑战性。我建议您查看一下默认的照相机应用程序。以下是它的源代码:。

您可以通过以下方式借助
FrameLayout
完成此操作:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

  <android.view.SurfaceView
  android:id="@+id/surface"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />

<ImageView
    android:id = "@+id/header"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content" />

</FrameLayout>
 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >

<android.view.SurfaceView
 android:id="@+id/surface"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />

<ImageView
android:id = "@+id/header"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />

</FrameLayout>


将对您有所帮助。

您可以使用SurfaceView并创建一个自定义视图,该视图将打开摄像头,您可以相应地在xml中调整其大小。下面是一个伪代码

创建一个扩展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>

现在,您可以在任何
acitivity
中使用这个main.xml,该活动将使用
ImageView
打开相机。
谢谢……

看,您的XML是这样的:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

  <android.view.SurfaceView
  android:id="@+id/surface"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />

<ImageView
    android:id = "@+id/header"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content" />

</FrameLayout>
 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >

<android.view.SurfaceView
 android:id="@+id/surface"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />

<ImageView
android:id = "@+id/header"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />

</FrameLayout>
}

我想现在我也应该给你完整的XML部分。这就是:

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/containerImg"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android1:layout_gravity="bottom"
android:orientation="vertical"
android1:gravity="bottom" >

    <FrameLayout
    android:id="@+id/frameLayout1"
    android1:layout_width="wrap_content"
    android1:layout_height="wrap_content"
    android:gravity="center|top"
     >

 <SurfaceView
     android1:id="@+id/surfaceView1"
     android1:layout_width="100dip"
     android1:layout_height="150dip"
     android1:layout_gravity="center_horizontal" />

<ImageView
android1:id="@+id/imageView1"
android1:layout_width="fill_parent"
android1:layout_height="fill_parent"
android:contentDescription="@string/app_dress_desc"
android:gravity="center|bottom"
android1:scaleType="center"
android1:src="@drawable/dress" />

</FrameLayout>


<GridLayout
    android1:id="@+id/gridLayout1"
    android1:layout_width="match_parent"
    android1:layout_height="wrap_content"
    android1:layout_alignParentBottom="true"
    android1:layout_alignParentLeft="true"
    android1:columnCount="1"
    android1:gravity="center|bottom"
    android1:orientation="vertical" >

    <ImageButton
        android1:id="@+id/capturebtn"
        android1:layout_width="60dp"
        android1:layout_height="wrap_content"
        android1:layout_gravity="left"
        android1:src="@drawable/camera"
        android:contentDescription="@string/app_icon_camera_desc" />

    <ImageButton
        android1:id="@+id/savebtn"
        android1:layout_width="60dp"
        android1:layout_height="wrap_content"
        android1:layout_column="0"
        android1:layout_gravity="center_horizontal|top"
        android1:layout_row="0"
        android1:src="@drawable/save"
        android:contentDescription="@string/app_icon_save_desc" />

    <ImageButton
        android1:id="@+id/sharebtn"
        android1:layout_width="60dp"
        android1:layout_height="wrap_content"
        android1:layout_column="0"
        android1:layout_gravity="right|top"
        android1:layout_row="0"
        android1:src="@drawable/share"
        android:contentDescription="@string/app_icon_share_desc" />
</GridLayout>

“共享”按钮此时不起作用,但它会在单击捕获时保存整个帧布局。
希望这会有帮助

另一种解决方法是将活动XML文件与另一个包含标题图像的XML文件重叠。为此:

  • 在布局文件夹中创建新的布局文件。例如:
    overlay.xml
  • 在其中插入一个
    ImageView
    ,类似于:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <ImageView
        android:id="@+id/imageView1"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android" />
    
    
    </RelativeLayout>
    
  • 最后从
    onCreate()
    调用
    addView()
    方法来添加图像:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.header);
    addView();
    

  • 最终的结果将是
    SurfaceView
    上方的图像覆盖。根据标题图像的质量,标题的渲染质量应为原始质量。希望有帮助。

    标题是指摄像头应用程序顶部的图形还是类似水印的图形?是的,我想在顶部显示一个图形作为标题。谢谢。是的,我想在相机预览的顶部显示一张图片作为标题。没什么,我甚至不想改变拍摄的图像。谢谢。很好地分离了concernsHad,删除了
    android:layout_below=“@id/mySurfaceView”
    ,使代码正常工作。相机预览非常失真。现在解决方案是什么?请查看我从谷歌的Camera2Video中分出的示例代码,其中包含折旧问题的解决方案。链接怎么了?它似乎改变了方向。你能重新链接正确的链接吗?@PavanK更新了链接-@PavanK你的链接是Brokersory,但谷歌更新了他们的api。你可以在“不是一项很难的任务,但它很有挑战性”下搜索它。好吧,是哪一个?