Search This Blog

Tuesday, 13 March 2012

chapter 8: Images and media Composite


Images and media
Composite

this Code to captured and Save the Image So, How shoud i have to Implement Such things.



takePhotoBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) { // <5>
            ImageCaptureCallback iccb = null;
            try {
                String filename = timeStampFormat.format(new Date());
                ContentValues values = new ContentValues();
                values.put(Media.TITLE, filename);
                values.put(Media.DESCRIPTION, "Image capture by camera");
                Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
                iccb = new ImageCaptureCallback( getContentResolver().openOutputStream(uri));
                // to put the Image on captured image.
                Canvas canvas = new Canvas();
                Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
                        R.drawable.icon);
                canvas.drawColor(Color.argb(160, 21, 140, 21));
                canvas.drawBitmap(kangoo, 130, 10, null);
            } catch(Exception ex ){
                ex.printStackTrace();
                Log.e(getClass().getSimpleName(), ex.getMessage(), ex);
            }
          camera.takePicture(mShutterCallback, mPictureCallbackRaw, iccb);
        }
      });
Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera c) {
        Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
            System.out.println("\n\n\n\nThe Data in mPictureCallbackRaw is :"+data);
        //camera.startPreview(); // Added StastPreview();
    }
};
Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera c) {
        Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
        System.out.println("\n\n\n\nThe Data in mPictureCallbackJPEG is :"+data);
        camera.startPreview();
    }
};
Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
    public void onShutter() {
        Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
    }
};
public class ImageCaptureCallback implements PictureCallback  {
private OutputStream filoutputStream;
public ImageCaptureCallback(OutputStream filoutputStream) {
    this.filoutputStream = filoutputStream;
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
    try {
        Log.v(getClass().getSimpleName(), "onPictureTaken=" + data + " length = " + data.length);
        filoutputStream.write(data);
        filoutputStream.flush();
        filoutputStream.close();
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}
Android implements several image compositing algorithms. Here's an easy way to use them. I don't have eclipse on this computer, so the following code is untested (but should work or at least be close to working).
img1 and img2 are both bitmaps, one of which you captured from the camera.
1) Create a new, empty bitmap. I'm assuming that img1 and img2 are the same size. If not, you can resize them or make this new bitmap have the size of the largest one or something.
Bitmap compositeImage = Bitmap.createBitmap(img1.getWidth(),
  img1.getWidth(), img1.getContig());
2) Create a canvas for drawing on the bitmap
Canvas canvas = new Canvas(compositeImage);
3) draw the first image onto the new bitmap
Paint paint = new Paint();
canvas.drawBitmap(img1, 0.0f, 0.0f, paint);

public Bitmap combineImages(Bitmap c, Bitmap s) {
        Bitmap cs = null;
        int width, height = 0;
        if (c.getWidth() > s.getWidth()) {
            width = c.getWidth();
            height = c.getHeight();
        } else {
            width = s.getWidth() + s.getWidth();
            height = c.getHeight();
        }
        cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas comboImage = new Canvas(cs);
        comboImage.drawBitmap(c, 0, 0, null);
        comboImage.drawBitmap(s, 100, 300, null);
        /******
         * 
         *   Write file to SDCard
         * 
         * ****/
        String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
        OutputStream os = null;
        try {
            os = new FileOutputStream(Environment.getExternalStorageDirectory()
                    + "/"+tmpImg);
            cs.compress(CompressFormat.PNG, 100, os);
        } catch (IOException e) {
            Log.e("combineImages", "problem combining images", e);
        }
        return cs;
    }

No comments:

Post a Comment