Pages

Ads 468x60px

Saturday 23 November 2013

Deal With Camera in Android

Deal With Camera in Android
  Suppose you're implementing a new crowd-sourced weather conditions service that produces a global weather chart by joining together together pictures on the sky considered by devices running ones client iphone app. Integrating photos is a small a part of your request. You wish to take photographs with minimum fuss, certainly not reinvent the camera. Fortunately, most Android-powered devices curently have a minumum of one camera request installed. With this lesson, you learn how to make it have a picture for you personally.

Request Camera Permission

if an important function of your application is taking photos, then minimize its awareness on Search engines Play to be able to devices which may have a video camera. To advertise that the application is determined by having some sort of camera, put some sort of <uses-feature> tag in your manifest file: 

 

<manifest ... >
    <uses-feature android:name="android.hardware.camera" />
    ... 
    </manifest ... >
 
If your application uses, but does not require a camera in order to function, 
add android:required="false" to the tag. In doing so, 
Google Play will allow devices without a camera to download your application. 
It's then your responsibility to check for the availability of the camera at 
runtime by calling hasSystemFeature(PackageManager.FEATURE_CAMERA). 
If a camera is not available, you should then disable your camera features. 

Photo with The Camera App
 
The Android method for appointing activities to different provisions is to summon an Expectation that portrays 
what you need done. This process includes three pieces: The Expectation itself, a call to begin the outside
 Movement, and some code to handle the picture information when center comes back to your action. 

Here's a capacity that conjures a plan to catch a photograph. 
 
private void dispatchTakePictureIntent(int actionCode) {
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      startActivityForResult(takePictureIntent, actionCode);
} 


Congrats: with this code, your requisition has picked up the capacity to make an alternate Polaroid provision 
do its offering! Obviously, if no good provision is prepared to get the plan, then your application will fall down as
a messed up stage jump. Here is a capacity to check if an application can handle your aim:

 
public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}
 
Show Photo 

Provided that the basic deed of taking a photograph is not the summit of your 
application's aspiration, then you presumably need to get the picture back from 
the Polaroid provision and do something with it. 

The Android Polaroid requisition encodes the photograph in the return Goal conveyed
to onactivityresult() as a little Bitmap in the additional items, under the key 
"information". The accompanying code recovers this picture and showcases it in an 
Imageview.
 
private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    mImageView.setImageBitmap(mImageBitmap);
} 

Store Photo
 
 The Android Polaroid provision spares a full-measure photograph provided that you give it a document to spare into.
 You should furnish a way that incorporates the space volume, organizer, and record name. 

storageDir = new File(
    Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES
    ), 
    getAlbumName()
);           
 
storageDir = new File (
    Environment.getExternalStorageDirectory()
        + PICTURES_DIR
        + getAlbumName()
);
 
Set and Change File Name  
 
As demonstrated in the past area, the record area for a picture ought to be determined by the nature's turf. What you have to do yourself is pick an impact safe document naming plan. You might wish likewise to spare the way in a part variable for later utilization. Here's a case result:
 
 
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = 
        new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
    File image = File.createTempFile(
        imageFileName, 
        JPEG_FILE_SUFFIX, 
        getAlbumDir()
    );
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}
 
 

0 التعليقات:

Post a Comment

.

Education blog