Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

get the image from camera click in android

package edu.gvsu.cis.masl.camerademo;

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

public class MyCameraActivity extends Activity
{
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;
    private static final int MY_CAMERA_PERMISSION_CODE = 100;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
                {
                    requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
                }
                else
                {
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);
                } 
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_CAMERA_PERMISSION_CODE)
        {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
            else
            {
                Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {  
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
        {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }  
    } 
}
Comment

PREVIOUS NEXT
Code Example
Java :: Java Create a ConcurrentHashMap 
Java :: spannable string in android java for color 
Java :: java comments 
Java :: how to initialize array in java 
Java :: spring boot actuator 
Java :: how to print something in java 
Java :: uuid from any string java 
Java :: compare two character alphabetically java 
Java :: intent pick action video android 
Java :: sort an int array java 
Java :: how to close a jframe in netbeans 
Java :: java string replace character at position 
Java :: Difference between Public, Private and Protected modifier in Java? 
Java :: how to make a loop in java 
Java :: retrofit android 
Java :: binary to decimal conversion in java 
Java :: integer.tostring java 
Java :: list of integers in java 
Java :: jbutton open jframe java 
Java :: can interface have attributes java 
Java :: dequeue in java 
Java :: android iinput edit text password icon change 
Java :: random int in range java 
Java :: Maven Dependency for flyway 
Java :: linear search java 
Java :: reverse a string in java 
Java :: convert char array to set in java 
Java :: how to make priority queue of pairs in java 
Java :: multiplication table using while loop in java 
Java :: @override java example 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =