Android | Camera Activity


Two Files 1) Camera.java    2) camera.xml


package myapplication.example1.com.hackonlycom;


import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Camera  extends Activity implements View.OnClickListener {

    ImageButton ib;
    Button b;
    ImageView iv;
    Intent i;
    int cameraResults;
    final static int cameraData=0;
    Bitmap bmp;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        initialize();
        InputStream is= getResources().openRawResource(R.raw.oreo_logo);
       bmp=BitmapFactory.decodeStream(is);
    }
    private void initialize() {
        // TODO Auto-generated method stub        iv=(ImageView)findViewById(R.id.ivReturnedPic);
        ib=(ImageButton)findViewById(R.id.ibTakePic);
        b=(Button)findViewById(R.id.bSetwall);
        b.setOnClickListener(this);
        ib.setOnClickListener(this);





    }
    @SuppressWarnings("deprecation")
    @Override    public void onClick(View v) {
        // TODO Auto-generated method stub        switch(v.getId()){
            case R.id.bSetwall:
                try {
                    getApplicationContext().setWallpaper(bmp);
                } catch (IOException e) {
                    // TODO Auto-generated catch block                    e.printStackTrace();
                }

                break;
            case R.id.ibTakePic:
                i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i,cameraData);
                break;


        }
    }
    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode== RESULT_OK){
            Bundle extras =data.getExtras();
            bmp=(Bitmap) extras.get("data");
            iv.setImageBitmap(bmp);
        }
    }



}




2) camera.xml



xml version= "1.0" encoding ="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width= "match_parent"    android:layout_height= "match_parent"    android:orientation= "vertical" >

    <ImageView        android:id="@+id/ivReturnedPic"        android:layout_width="250dp"        android:layout_height="250dp"        android:layout_gravity="center"
        android:src="@drawable/me"        />

    <ImageButton        android:id="@+id/ibTakePic"        android:layout_width="125dp"        android:layout_height="wrap_content"        android:layout_gravity="center"
        android:src="@drawable/plus" />

    <Button        android:id="@+id/bSetwall"        android:layout_width="125dp"        android:layout_height="wrap_content"        android:layout_gravity="center"
        android:text="Set Wallpaper" />

</LinearLayout>
Reactions:

0 comments:

Post a Comment