将图像添加到android中捕获的图像中

发布于 2024-12-01 21:12:02 字数 5160 浏览 0 评论 0原文

我正在使用以下代码捕获图像

  public class PictureDemo extends Activity {
  private SurfaceView preview=null;
  private SurfaceHolder previewHolder=null;
  private Camera camera=null;
  private boolean inPreview=false;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    save=(Button)findViewById(R.id.save);
    save.setOnClickListener(this);
    image=(ImageView)findViewById(R.id.image);
    image.setImageResource(R.drawable.sofa);
    preview=(SurfaceView)findViewById(R.id.preview);
    previewHolder=preview.getHolder();
    previewHolder.addCallback(surfaceCallback);
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }

  @Override
  public void onResume() {
  super.onResume();
  camera=CameraFinder.INSTANCE.open();
  }

  @Override
  public void onPause() {
    if (inPreview) {
    camera.stopPreview();
    }

    camera.release();
    camera=null;
    inPreview=false;

    super.onPause();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.options, menu);

    return(super.onCreateOptionsMenu(menu));
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.camera) {
      if (inPreview) {
        camera.takePicture(null, null, photoCallback);
        inPreview=false;
      }

      return(true);
    }

    return(super.onOptionsItemSelected(item));
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode==KeyEvent.KEYCODE_CAMERA ||
        keyCode==KeyEvent.KEYCODE_SEARCH) {

      return(true);
    }

    return(super.onKeyDown(keyCode, event));
  }

  private Camera.Size getBestPreviewSize(int width, int height,
                                         Camera.Parameters parameters) {
    Camera.Size result=null;

    for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
      if (size.width<=width && size.height<=height) {
        if (result==null) {
          result=size;
        }
        else {
          int resultArea=result.width*result.height;
          int newArea=size.width*size.height;

          if (newArea>resultArea) {
            result=size;
          }
        }
      }
    }

    return(result);
  }

  SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
    public void surfaceCreated(SurfaceHolder holder) {
      try {
        camera.setPreviewDisplay(previewHolder);
      }
      catch (Throwable t) {
        Log.e("PictureDemo-surfaceCallback",
              "Exception in setPreviewDisplay()", t);
        Toast
          .makeText(PictureDemo.this, t.getMessage(), Toast.LENGTH_LONG)
          .show();
      }
    }

    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width,
                               int height) {
      Camera.Parameters parameters=camera.getParameters();
      Camera.Size size=getBestPreviewSize(width, height,
                                          parameters);

      if (size!=null) {
        parameters.setPreviewSize(size.width, size.height);
        parameters.setPictureFormat(PixelFormat.JPEG);

        camera.setParameters(parameters);
        camera.startPreview();
        inPreview=true;
      }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
      // no-op
    }
  };

  Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
      new SavePhotoTask().execute(data);
      camera.startPreview();
      inPreview=true;
    }
  };

  class SavePhotoTask extends AsyncTask<byte[], String, String> {
    @Override
    protected String doInBackground(byte[]... jpeg) {
      File photo=new File(Environment.getExternalStorageDirectory(),
                          "photo.jpg");

      if (photo.exists()) {
        photo.delete();
      }

      try {
        FileOutputStream fos=new FileOutputStream(photo.getPath());

        fos.write(jpeg[0]);
        fos.close();
      }
      catch (java.io.IOException e) {
        Log.e("PictureDemo", "Exception in photoCallback", e);
      }

      return(null);
    }
  }
}

,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/image"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:scaleType="matrix"
  android:layout_centerInParent="true"
  />

  <Button
  android:id="@+id/save"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="save"
  android:layout_alignParentRight="true"
  />

</FrameLayout>

我可以使用此代码捕获图像,但我需要将提供给 ImageView 的图像添加到捕获的图像中。

如何做到这一点。

提前致谢。

I am capturing an image using the following code

  public class PictureDemo extends Activity {
  private SurfaceView preview=null;
  private SurfaceHolder previewHolder=null;
  private Camera camera=null;
  private boolean inPreview=false;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    save=(Button)findViewById(R.id.save);
    save.setOnClickListener(this);
    image=(ImageView)findViewById(R.id.image);
    image.setImageResource(R.drawable.sofa);
    preview=(SurfaceView)findViewById(R.id.preview);
    previewHolder=preview.getHolder();
    previewHolder.addCallback(surfaceCallback);
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }

  @Override
  public void onResume() {
  super.onResume();
  camera=CameraFinder.INSTANCE.open();
  }

  @Override
  public void onPause() {
    if (inPreview) {
    camera.stopPreview();
    }

    camera.release();
    camera=null;
    inPreview=false;

    super.onPause();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.options, menu);

    return(super.onCreateOptionsMenu(menu));
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.camera) {
      if (inPreview) {
        camera.takePicture(null, null, photoCallback);
        inPreview=false;
      }

      return(true);
    }

    return(super.onOptionsItemSelected(item));
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode==KeyEvent.KEYCODE_CAMERA ||
        keyCode==KeyEvent.KEYCODE_SEARCH) {

      return(true);
    }

    return(super.onKeyDown(keyCode, event));
  }

  private Camera.Size getBestPreviewSize(int width, int height,
                                         Camera.Parameters parameters) {
    Camera.Size result=null;

    for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
      if (size.width<=width && size.height<=height) {
        if (result==null) {
          result=size;
        }
        else {
          int resultArea=result.width*result.height;
          int newArea=size.width*size.height;

          if (newArea>resultArea) {
            result=size;
          }
        }
      }
    }

    return(result);
  }

  SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
    public void surfaceCreated(SurfaceHolder holder) {
      try {
        camera.setPreviewDisplay(previewHolder);
      }
      catch (Throwable t) {
        Log.e("PictureDemo-surfaceCallback",
              "Exception in setPreviewDisplay()", t);
        Toast
          .makeText(PictureDemo.this, t.getMessage(), Toast.LENGTH_LONG)
          .show();
      }
    }

    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width,
                               int height) {
      Camera.Parameters parameters=camera.getParameters();
      Camera.Size size=getBestPreviewSize(width, height,
                                          parameters);

      if (size!=null) {
        parameters.setPreviewSize(size.width, size.height);
        parameters.setPictureFormat(PixelFormat.JPEG);

        camera.setParameters(parameters);
        camera.startPreview();
        inPreview=true;
      }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
      // no-op
    }
  };

  Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
      new SavePhotoTask().execute(data);
      camera.startPreview();
      inPreview=true;
    }
  };

  class SavePhotoTask extends AsyncTask<byte[], String, String> {
    @Override
    protected String doInBackground(byte[]... jpeg) {
      File photo=new File(Environment.getExternalStorageDirectory(),
                          "photo.jpg");

      if (photo.exists()) {
        photo.delete();
      }

      try {
        FileOutputStream fos=new FileOutputStream(photo.getPath());

        fos.write(jpeg[0]);
        fos.close();
      }
      catch (java.io.IOException e) {
        Log.e("PictureDemo", "Exception in photoCallback", e);
      }

      return(null);
    }
  }
}

and the xml file is

<?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/image"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:scaleType="matrix"
  android:layout_centerInParent="true"
  />

  <Button
  android:id="@+id/save"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="save"
  android:layout_alignParentRight="true"
  />

</FrameLayout>

I can capture an image with this code but i need to add the image which was given to the ImageView to the captured image.

How to do that.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲歌长辞 2024-12-08 21:12:02

在你的类中声明一个位图

public class PictureDemo extends Activity {
  Bitmap bitmap;
  ...

我想你可以用这个获取你刚刚拍摄的图像:

public void onPictureTaken(byte[] data, Camera camera) {
  new SavePhotoTask().execute(data);
  camera.startPreview();
  inPreview=true;
  bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
}

然后将位图设置为你的ImageView,如下所示:

image.setImageBitmap(bitmap);

Declare a Bitmap in your class

public class PictureDemo extends Activity {
  Bitmap bitmap;
  ...

I think you can get the image you just took with this:

public void onPictureTaken(byte[] data, Camera camera) {
  new SavePhotoTask().execute(data);
  camera.startPreview();
  inPreview=true;
  bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
}

And then set the bitmap to your ImageView like this:

image.setImageBitmap(bitmap);
音盲 2024-12-08 21:12:02

尝试做这样的事情:

Bitmap bitmap = new Bitmap(define the size and other params you like).
Canvas canvas = new Canvas();
canvas.drawBitmap(your captured image);
canvas.drawBitmap(the overlay image);

// Now the bitmap will include both captured imaged and overlayed image

您可以查看此处此处

Trying doing something like this:

Bitmap bitmap = new Bitmap(define the size and other params you like).
Canvas canvas = new Canvas();
canvas.drawBitmap(your captured image);
canvas.drawBitmap(the overlay image);

// Now the bitmap will include both captured imaged and overlayed image

You can look in here and here

反目相谮 2024-12-08 21:12:02

您可以执行mFrameLayout.getDrawingCache()来获取整个布局的位图。应启用绘图缓存。我不知道它的默认状态。

You can do mFrameLayout.getDrawingCache() to get a bitmap of the whole layout. The drawing cache should be enabled. I don't know its default state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文