1

我创建了一个相机应用程序,它在相机预览中具有图像。例如,这是一顶帽子的图像,我想拍摄一个人的照片,帽子图像在他的头上。这就是我所做的:

        mFrameLayout = new FrameLayout(this);

        mPreview = new Preview(this);
        mFrameLayout.addView(mPreview);

        mImage = new ImageView(this);

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100, 100);
        layoutParams.leftMargin = 200;
        layoutParams.topMargin = 100;
        mImage.setLayoutParams(layoutParams);
        mImage.setImageResource(R.drawable.gorro);
        mFrameLayout.addView(mImage);

        mFrameLayout.setOnClickListener(this);

        setContentView(mFrameLayout);

预习班:

class Preview extends ViewGroup implements SurfaceHolder.Callback {
    private final String TAG = "Preview";

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;
    Size mPreviewSize;
    List<Size> mSupportedPreviewSizes;
    Camera mCamera;

    Preview(Context context) {
        super(context);

        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);
    }

并且这个类实现了所有的表面方法。当我触摸相机上的任何地方时,就会拍摄照片。这是代码:

public void onClick(View arg0) {
        Toast.makeText(this, "Taken!", Toast.LENGTH_LONG).show();
        mCamera.takePicture(null, mPictureCallback, mPictureCallback);
    }

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] imageData, Camera c) {
            if (imageData != null) {
                String root = Environment.getExternalStorageDirectory()
                        .toString();
                File myDir = new File(root + "/saved_images2");
                myDir.mkdirs();
                Random generator = new Random();
                int n = 10000;
                n = generator.nextInt(n);
                String fname = "Image-" + n + ".jpg";
                File file = new File(myDir, fname);
                if (file.exists())
                    file.delete();
                try {
                    FileOutputStream out = new FileOutputStream(file);
                    out.write(imageData);
                    out.flush();
                    out.close();
                    Log.d("teste", "onPictureTaken - wrote bytes: "
                            + imageData.length);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };

问题是当我用 .png 图像拍照时,没有它就保存了照片。

拍照时如何保存 .png 图像?

4

1 回答 1

0

您必须在 PictureCallBack 中将您的 imageData 转换为位图,然后使用带有方法 drawBitmap 的 Canvas 将您的帽子位图绘制到从相机捕获的图像上。最后,您可以将结果位图保存到文件中。所以:

Bitmap cameraBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
Canvas canv = new Canvas(cameraBitmap );
canv.drawBitmap(.... your hat image);
于 2013-03-19T15:50:44.637 回答