Make and Save the Photo

All your problems with Audio, Video and Images.

Make and Save the Photo

Postby andrew85q » Fri Jan 30, 2009 12:38 pm

I want to save image after I make the photo with the Camera of the
Emulator......


This is the Code :::: this program makes a photo when you push
space.....but don't save it !!!!!!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class AppCam extends Activity implements SurfaceHolder.Callback
  3.  
  4. {
  5.  
  6.    private static final String TAG = "CameraApiTest";
  7.  
  8.    Camera mCamera;
  9.  
  10.    boolean mPreviewRunning = false;
  11.  
  12.  
  13.  
  14.    public void onCreate(Bundle icicle)
  15.  
  16.    {
  17.  
  18.        super.onCreate(icicle);
  19.  
  20.  
  21.  
  22.        Log.e(TAG, "onCreate");
  23.  
  24.  
  25.  
  26.        getWindow().setFormat(PixelFormat.TRANSLUCENT);
  27.  
  28.  
  29.  
  30.        setContentView(R.layout.main);
  31.  
  32.        mSurfaceView = (SurfaceView)findViewById(R.id.surface);
  33.  
  34.  
  35.  
  36.        mSurfaceHolder = mSurfaceView.getHolder();
  37.  
  38.        mSurfaceHolder.addCallback(this);
  39.  
  40.        mSurfaceHolder.setType
  41.  
  42. (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  43.  
  44.    }
  45.  
  46.  
  47.  
  48.    public boolean onCreateOptionsMenu(android.view.Menu menu) {
  49.  
  50.        MenuItem item = menu.add(0, 0, 0, "Gallery");
  51.  
  52.        item.setOnMenuItemClickListener(new
  53.  
  54. MenuItem.OnMenuItemClickListener() {
  55.  
  56.            public boolean onMenuItemClick(MenuItem item) {
  57.  
  58.                Uri target = Uri.parse("content://media/external/
  59.  
  60. images/media");
  61.  
  62.                Intent intent = new Intent(Intent.ACTION_VIEW,
  63.  
  64. target);
  65.  
  66.                startActivity(intent);
  67.  
  68.                return true;
  69.  
  70.            }
  71.  
  72.        });
  73.  
  74.        return true;
  75.  
  76.    }
  77.  
  78.  
  79.  
  80.    @Override
  81.  
  82.    protected void onRestoreInstanceState(Bundle savedInstanceState)
  83.  
  84.    {
  85.  
  86.        super.onRestoreInstanceState(savedInstanceState);
  87.  
  88.    }
  89.  
  90.  
  91.  
  92.    Camera.PictureCallback mPictureCallback = new
  93.  
  94. Camera.PictureCallback() {
  95.  
  96.        public void onPictureTaken(byte[] data, Camera c) {
  97.  
  98.            Log.e(TAG, "PICTURE CALLBACK: data.length = " +
  99.  
  100. data.length);
  101.  
  102.            mCamera.startPreview();
  103.  
  104.        }
  105.  
  106.    };
  107.  
  108.  
  109.  
  110.    public boolean onKeyDown(int keyCode, KeyEvent event)
  111.  
  112.    {
  113.  
  114.        if (keyCode == KeyEvent.KEYCODE_BACK) {
  115.  
  116.            return super.onKeyDown(keyCode, event);
  117.  
  118.        }
  119.  
  120.  
  121.  
  122.        if (keyCode == KeyEvent.KEYCODE_SPACE) {
  123.  
  124.  
  125.  
  126.            mCamera.takePicture(null, null,mPictureCallback);
  127.  
  128.  
  129.  
  130.          
  131.  
  132.  
  133.  
  134.            return true;
  135.  
  136.        }
  137.  
  138.  
  139.  
  140.        return false;
  141.  
  142.    }
  143.  
  144.  
  145.  
  146.    protected void onResume()
  147.  
  148.    {
  149.  
  150.        Log.e(TAG, "onResume");
  151.  
  152.        super.onResume();
  153.  
  154.    }
  155.  
  156.  
  157.  
  158.    protected void onSaveInstanceState(Bundle outState)
  159.  
  160.    {
  161.  
  162.        super.onSaveInstanceState(outState);
  163.  
  164.    }
  165.  
  166.  
  167.  
  168.    protected void onStop()
  169.  
  170.    {
  171.  
  172.        Log.e(TAG, "onStop");
  173.  
  174.        super.onStop();
  175.  
  176.    }
  177.  
  178.  
  179.  
  180.    public void surfaceCreated(SurfaceHolder holder)
  181.  
  182.    {
  183.  
  184.        Log.e(TAG, "surfaceCreated");
  185.  
  186.        mCamera = Camera.open();
  187.  
  188.        //mCamera.startPreview();
  189.  
  190.    }
  191.  
  192.  
  193.  
  194.    public void surfaceChanged(SurfaceHolder holder, int format, int
  195.  
  196. w, int h)
  197.  
  198.    {
  199.  
  200.        Log.e(TAG, "surfaceChanged");
  201.  
  202.  
  203.  
  204.        // XXX stopPreview() will crash if preview is not running
  205.  
  206.        if (mPreviewRunning) {
  207.  
  208.            mCamera.stopPreview();
  209.  
  210.        }
  211.  
  212.  
  213.  
  214.        Camera.Parameters p = mCamera.getParameters();
  215.  
  216.        p.setPreviewSize(w, h);
  217.  
  218.        mCamera.setParameters(p);
  219.  
  220.        mCamera.setPreviewDisplay(holder);
  221.  
  222.        mCamera.startPreview();
  223.  
  224.        mPreviewRunning = true;
  225.  
  226.    }
  227.  
  228.  
  229.  
  230.    public void surfaceDestroyed(SurfaceHolder holder)
  231.  
  232.    {
  233.  
  234.        Log.e(TAG, "surfaceDestroyed");
  235.  
  236.        mCamera.stopPreview();
  237.  
  238.        mPreviewRunning = false;
  239.  
  240.        mCamera.release();
  241.  
  242.    }
  243.  
  244.  
  245.  
  246.    private SurfaceView mSurfaceView;
  247.  
  248.    private SurfaceHolder mSurfaceHolder;
  249.  
  250. }
Parsed in 0.051 seconds, using GeSHi 1.0.8.4
[syntax="java"]
andrew85q
Freshman
Freshman
 
Posts: 3
Joined: Fri Jan 30, 2009 12:31 pm

Top

Postby andmol » Tue Feb 03, 2009 4:11 pm

Hi andrew85q,

try to use the following code in your onPictureTaken method:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
  3.  
  4.  
  5.  
  6. File dataFile = new File(filepath);
  7.  
  8. try {
  9.  
  10.      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dataFile));
  11.  
  12.                        
  13.  
  14.      bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
  15.  
  16.      bos.flush();
  17.  
  18.      bos.close();
  19.  
  20. } catch (IOException ex) {
  21.  
  22. Log.e(TAG, ex.getMessage());
  23.  
  24. }
  25.  
  26.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Put it in an extra thread to make it faster...
andmol
Junior Developer
Junior Developer
 
Posts: 21
Joined: Tue Sep 30, 2008 5:34 pm

Postby andrew85q » Tue Feb 03, 2009 8:09 pm

thanks!!!!!!!!!!!!!!!!
andrew85q
Freshman
Freshman
 
Posts: 3
Joined: Fri Jan 30, 2009 12:31 pm

Location of image

Postby chetandl » Thu Mar 19, 2009 5:36 am

How to specify location for storing captured images in above code.

I want to store it for my application only.Not for public use.
chetandl
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue Mar 17, 2009 9:17 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 21 guests