Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

camera - Android: Saving photo with image on photo

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...