Take a picture and store

All your problems with Audio, Video and Images.

Take a picture and store

Postby akus85 » Sat Aug 27, 2011 9:23 am

I write this code to take a photo and store it but i obtain this distorted picture
Image

This is the source code but i don't know where is the error... :cry:


Photo2Activity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package it.Photo03;
  2.  
  3.  
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8.  
  9. import android.app.Activity;
  10. import android.graphics.Bitmap;
  11. import android.graphics.Bitmap.CompressFormat;
  12. import android.graphics.BitmapFactory;
  13. import android.graphics.PixelFormat;
  14. import android.hardware.Camera;
  15. import android.hardware.Camera.PictureCallback;
  16. import android.hardware.Camera.Size;
  17. import android.os.Bundle;
  18. import android.view.SurfaceHolder;
  19. import android.view.SurfaceView;
  20. import android.view.View;
  21. import android.view.Window;
  22. import android.view.WindowManager;
  23. import android.widget.Button;
  24. import android.view.View.OnClickListener;
  25.  
  26. public class Photo03Activity extends Activity implements SurfaceHolder.Callback{
  27.     /** Called when the activity is first created. */
  28.         private Button btn;
  29.         private SurfaceView mSurfaceView;
  30.         private SurfaceHolder mSurfaceHolder;
  31.         private Camera mCamera;
  32.         private final static String PHOTO_FILE_NAME = "/sdcard/DCIM/Camera/";
  33.        
  34.     @Override
  35.     public void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         getWindow().setFormat(PixelFormat.TRANSLUCENT);
  38.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  39.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  40.         setContentView(R.layout.main);
  41.         btn = (Button) findViewById(R.id.button1);
  42.         btn.setOnClickListener(new OnClickListener(){
  43.                         @Override
  44.                         public void onClick(View v) {
  45.                                 File f = new File(PHOTO_FILE_NAME,"photo.jpeg");
  46.                                 mCamera.takePicture(null, null, jpegCallback);
  47.                         }
  48.                
  49.         });
  50.                 mSurfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
  51.         mSurfaceHolder = mSurfaceView.getHolder();
  52.         mSurfaceHolder.addCallback(this);
  53.         mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  54.         mSurfaceHolder.setFixedSize(getWindow().getWindowManager()
  55.                 .getDefaultDisplay().getWidth(), getWindow().getWindowManager()
  56.                 .getDefaultDisplay().getHeight());
  57.     }
  58.    
  59.     PictureCallback jpegCallback = new PictureCallback() {
  60.                 public void onPictureTaken(byte[] _data, Camera _camera) {
  61.                         Bitmap bmp = BitmapFactory.decodeByteArray(_data, 0, _data.length);
  62.                         String filename = "photo.jpg";
  63.                         File pictureFile = new File(PHOTO_FILE_NAME + filename);
  64.                         try{
  65.                                 FileOutputStream pfos = new FileOutputStream(pictureFile);
  66.                                 bmp.compress(CompressFormat.JPEG, 50, pfos);
  67.                                 pfos.flush();
  68.                                 pfos.close();
  69.                         } catch (Exception e){}
  70.                        
  71.                         mCamera.stopPreview();
  72.                         mCamera.release();
  73.                         mCamera.startPreview();
  74.                        
  75.                 }
  76.         };
  77.  
  78.         @Override
  79.         public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  80.                 Camera.Parameters p = mCamera.getParameters();
  81.                 p.setPreviewSize(arg2, arg3);
  82.                 p.setPictureFormat(PixelFormat.JPEG);
  83.         p.setPictureSize(500, 500);
  84.         p.setJpegQuality(50);      
  85.         mCamera.setParameters(p);
  86.         mCamera.setDisplayOrientation(90);
  87.        
  88.         try {
  89.                 //lancio la preview
  90.                         mCamera.setPreviewDisplay(arg0);        
  91.                         mCamera.startPreview();
  92.                 } catch (IOException e) {}
  93.         }
  94.  
  95.         @Override
  96.         public void surfaceCreated(SurfaceHolder holder) {
  97.                 mCamera = Camera.open();                
  98.         }
  99.  
  100.         @Override
  101.         public void surfaceDestroyed(SurfaceHolder holder) {
  102.         mCamera.stopPreview();
  103.         mCamera.release();      
  104.         }
  105. }
  106.  
Parsed in 0.047 seconds, using GeSHi 1.0.8.4


main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.     <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent"
  3.                   xmlns:android="http://schemas.android.com/apk/res/android">
  4.                     <SurfaceView android:layout_height="fill_parent" android:id="@+id/surfaceView1"
  5.                                android:layout_width="fill_parent"></SurfaceView>
  6.                     <RelativeLayout android:layout_height="fill_parent" android:id="@+id/relativeLayout2" android:layout_width="wrap_content"
  7.                                    android:layout_alignRight="@id/surfaceView1">                                  
  8.                                      <Button android:layout_height="wrap_content" android:id="@+id/button1" android:layout_width="wrap_content"
  9.                                           android:text="Take" android:layout_alignParentBottom="true" android:layout_centerInParent="true"></Button>
  10.                    </RelativeLayout>
  11.     </RelativeLayout>
  12.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


AndroidManifest.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="it.Photo03"
  4.     android:versionCode="1"
  5.     android:versionName="1.0">
  6.     <uses-sdk android:minSdkVersion="8" />
  7.  
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  9.         <activity android:name=".Photo03Activity"
  10.                 android:label="@string/app_name">
  11.             <intent-filter>
  12.                 <action android:name="android.intent.action.MAIN" />
  13.                 <category android:name="android.intent.category.LAUNCHER" />
  14.             </intent-filter>
  15.         </activity>
  16.  
  17.     </application>
  18.     <uses-permission android:name="android.permission.CAMERA" />
  19.     <uses-feature android:name="android.hardware.camera"/>
  20.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  21. </manifest>
  22.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
Attachments
Photo03.zip
(49.49 KiB) Downloaded 15 times
akus85
Freshman
Freshman
 
Posts: 2
Joined: Wed Nov 24, 2010 11:21 pm

Top

Re: Take a picture and store

Postby vishal.trivedi » Wed Nov 23, 2011 11:02 am

hii

if you hv answr by any chance plz let me knw...
thnx
vishal.trivedi
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Sep 28, 2011 4:21 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 20 guests