Wednesday, March 2, 2011

Android: Capture the best moments

I was expecting to write a new post on lambu's birthday...but it didn't happened..So here is another android application through which you can capture an image an view it as and when you capture it as a thumbnail....

My Activity

package com.apiit.TryCamera;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class TryCamera extends Activity implements SurfaceHolder.Callback{
private Camera camera;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SurfaceView surface=(SurfaceView)findViewById(R.id.surface);
final SurfaceHolder holder=surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(150, 150);
Button tke=(Button)findViewById(R.id.take_btn);
tke.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
takePicture();
}

});
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {

try{
camera = camera.open();
camera.setPreviewDisplay(holder);
camera.startPreview();
}
catch(IOException e)
{

}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
}

private void takePicture(){
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
// TODO Do something when the shutter closes.
}
};
PictureCallback rawCallback = new PictureCallback() {

public void onPictureTaken(byte[] data, Camera camera) {
// TODO Do something with the image RAW data.

}
};
PictureCallback jpegCallback = new PictureCallback() {

public void onPictureTaken(byte[] data, Camera camera) {
// Save the image JPEG data to the SD card
FileOutputStream outStream=null;

FileInputStream fstream = null;
Bitmap bmp;
try {
File root=Environment.getExternalStorageDirectory();
File jpgfile=new File(root,"vinsol.jpg");
outStream=new FileOutputStream(jpgfile);
outStream.write(data);
outStream.flush();
outStream.close();
fstream = new FileInputStream(Environment.getExternalStorageDirectory() + "/" + "vinsol.jpg");
bmp = BitmapFactory.decodeStream(fstream);
ImageView iv=(ImageView)findViewById(R.id.image);
iv.setImageBitmap(bmp);

} catch (FileNotFoundException e) {
Log.d("CAMERAIO", e.getMessage());
} catch (IOException e) {
Log.d("CAMERAIO", e.getMessage());
}
camera.startPreview();
}
};
}





Layout


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="300dip"
android:id="@+id/surface"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:layout_width="70dip"
android:layout_height="wrap_content"
android:text="Take Picture"
android:id="@+id/take_btn"/>
<ImageView
android:layout_marginTop="10dip"
android:layout_marginLeft="-5dip"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:id="@+id/image"/>

</LinearLayout>
</LinearLayout>





Important Points

You must be able to see a <SurfaceView> tag in my layout..
When you need to update the content of the view rapidly you need to use the SurfaceView..
as is the case of the camera (Updated frequently)

                                                                     A) Initial Screen

B) Captured Image Thumbnail


As I could not run the application on a device...the screenshot is not showing the proper preview...


Enjoy...Suggestions required.. 
 




Friday, February 18, 2011

A Tribute!!!

Paying a tribute to Pratik Dehane (B.M.S). You are in our hearts brother and we miss you often.
I still remember the charm and sharpness you had but GOD had some different plans for you..
Miss you always brother


Thursday, February 17, 2011

Android Animation

Hello Fellows!!!
First of all, as per the previous post I will be showing the way I did all that stuff.

Note: You must have android-sdk installed and it should be updated and run successfully in eclipse.
          Google and even I prefer eclipse GYNAMEDE to develop Android Applications

In eclipse New--->Android Project--->Name your project with a proper package name.
Now you will be having a activity class generated just copy and the paste the code mentioned below:

My Activity

public class DeveloperAnimate extends Activity {
    /** Called when the activity is first created. */
View me,me1,me2,me3=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        me=(View)findViewById(R.id.View01);
        me1=(View)findViewById(R.id.View02);
        me2=(View)findViewById(R.id.View03);
        me3=(View)findViewById(R.id.View04);
        Animation a = AnimationUtils.loadAnimation(this, R.anim.try1);
a.reset();
Animation b=AnimationUtils.loadAnimation(this, R.anim.try2);
b.reset();
Animation c=AnimationUtils.loadAnimation(this, R.anim.try3);
c.reset();
Animation d=AnimationUtils.loadAnimation(this, R.anim.try4);
d.reset();
me=(View)findViewById(R.id.View01);
   me1=(View)findViewById(R.id.View02);
   me2=(View)findViewById(R.id.View03);
   me3=(View)findViewById(R.id.View04);
   me.clearAnimation();
   me1.clearAnimation();
   me2.clearAnimation();
   me3.clearAnimation();
   me.startAnimation(a);
   me1.startAnimation(b);
   me2.startAnimation(c);
   me3.startAnimation(d);
   final int display=6000;
   Thread splash=new Thread()
   {
      int wait=0;
      public void run()
      {
      try{
      super.run();
      while(wait<display)
      {
      sleep(100);
      wait+=100;
      }
        }catch(Exception e)
        {
       
        }finally
        {
        startActivity(new Intent(DeveloperAnimate.this,MenuScreen.class));
        finish();
        }
        }
       
       };
       splash.start();
   
      }
} 


Now the layout i.e. main.xml



<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<ImageView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/image"
android:src="@drawable/tryfinal"
    />
</LinearLayout>   




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<View 
android:id="@+id/View02" 
android:layout_width="2dip"     
android:layout_height="400dip" 
android:background="#2B497B"
android:visibility="invisible"
android:paddingLeft="250dip"
android:layout_marginLeft="230dip">
  </View>
</LinearLayout>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<View 
android:id="@+id/View01" 
android:layout_width="2dip"     
android:layout_height="325dip" 
android:visibility="invisible"
android:background="#2B497B"
android:paddingLeft="50dip"
android:layout_marginLeft="85dip">
  </View>
 </LinearLayout>


 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<View 
android:id="@+id/View03" 
android:layout_width="300dip"     
android:layout_height="3dip" 
android:visibility="invisible"
android:background="#2B497B"
android:paddingLeft="50dip"
android:layout_marginTop="90dip">
  </View>
 </LinearLayout>


  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<View 
android:id="@+id/View04" 
android:layout_width="300dip"     
android:layout_height="3dip" 
android:visibility="invisible"
android:background="#2B497B"
android:paddingLeft="50dip"
android:layout_marginTop="195dip"
>
  </View>
 </LinearLayout>
</FrameLayout>


Now the animation files i.e. try1.xml,try2.xml,try3.xml,try4.xml i.e. 4 lines which I have animated I will be posting one of them because others are quite similar.
Note: These are the animation files. Therefore there is a different way of creating the animation XML
New-->Android XML-->select Animation from the appeared dialog box 



<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/accelerate_interpolator">
 <translate 
    android:fromXDelta="-600" 
    android:toXDelta="50" 
    android:duration="900" />
       <alpha    
xmlns:android="http://schemas.android.com/apk/res/android"    
android:fromAlpha="1.0"    
android:toAlpha="0.0"    
android:duration="3000" /> 
</set>


Define the Animation i.e. four files and run the project you will see the result.
Again need suggestions guys....

Wednesday, February 16, 2011

Secret Revealed

So, day to day before yesterday I was thinking regarding how should my application look like as it is a Game i.e. my FYP (Tic-Roid). It is an Bluetooth based multiplayer game for Android devices.
Previously I was thinking to name it as "Tic-Tac-Troid", Thanx to Suresh Sir as he suggested me this name "Tic-Roid". Though I was in favour of naming it as "Tic-Roid", superstitious Suresh Sir was another reason for being in favour of his suggestion.
Seriously, things are going well after renaming my application. I am also posting some screenshots of my application as I need reviews.

Animated Screen

Splash Screen

So guys kindly provide your reviews regarding the look and feel of the Splash screen. Though I have developed some of my application I will be sharing it in future posts. That's it for this post. :)

Tuesday, February 15, 2011

Greetings Fellows......
With a time span of around 100 days remaining at APIIT, a thought appeared in my mind
i.e. to start a blog with a purpose which you all will come to know after going through the blog.
Frankly speaking, someone ideally inspired me to write this blog.

At present this blog is about something which I have been working on since three months i.e. "Android".
Ahhhhh!!! I am speechless. The features I see while working on it are awesome.

Multimedia being my graduation specialization has made me hear about Flash, 3Ds MAX, Maya etc. and some of among them I have worked on even. Three days before while working on Android I was visiting some of the blogs "Android & Amir" & "SaiGeetha" etc. and once again Android surprised me.
I will be sharing it with you all as soon as possible.....