Android: Saving photo with image on photo

2019-09-08 04:55发布

问题:

When I take a picture with my Android camera, there is an image (imageview3) on my screen that I want to save with my picture.

Here is my onPictureTaken method

public void onPictureTaken(byte[] data, Camera camera) {
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/Ker");
    imagesFolder.mkdirs();
    String fileName = "Ker_.jpg";
    output = new File(imagesFolder, fileName);
    ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3);
    view.setDrawingCacheEnabled(true);
    Bitmap b = view.getDrawingCache();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(output);
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
    b.compress(CompressFormat.JPEG, 95, fos);
    try {
        fos.write(data);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
        catch (IOException e) {
        e.printStackTrace();
    }
    camera.stopPreview();
}

When I open the folder, the picture saved is only the imageview3 with a black background. Why has the real camera view not been saved?

EDIT I'm trying something with canvas too:

output = new File(imagesFolder, fileName);
            ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3);
            view.setDrawingCacheEnabled(true);
            Bitmap b = view.getDrawingCache();   
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(output);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
              catch (IOException e) {
                    e.printStackTrace();
            }
            FileOutputStream fos2 = null;
            b.compress(CompressFormat.JPEG, 95, fos2);

            try {
                Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fos.getFD());
                Bitmap bitmap2 = BitmapFactory.decodeFileDescriptor(fos2.getFD());
                Canvas canvas = new Canvas(bitmap);
                canvas.drawBitmap(bitmap2, null, null);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Is that correct? How to save the canvas into a file on my sdcard (ie write data from canvas on fileoutputstream)

回答1:

You're appending the JPEG of the imageview and the JPEG from the camera into a single file.

Create a separate file output stream for each file, and write the data from Bitmap.compress() and the onPictureTaken data array into their own streams.

If you want the two images to be combined into a single image, you'll need to decode the data array into a Bitmap, and then use a Canvas to draw the ImageView bitmap and the camera-captured Bitmap onto the canvas in the arrangement you want, and then save that out as a single jpeg.

You can't simply concatenate two compressed JPEG bitstreams together; the file format doesn't work that way.