How to use the system camera and camera

Source: Internet
Author: User

How to use the system camera and camera
1. Call the System camera to take a photo

Package com. android. myapplication; import android. content. intent; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. point; import android. graphics. drawable. bitmapDrawable; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. support. v7.app. appCompatActivity; import android. util. log; impor T android. view. display; import android. view. view; import android. view. window; import android. widget. imageView; import android. widget. linearLayout; import java. io. file; import java. text. simpleDateFormat; import java. util. date; public class MainActivity extends AppCompatActivity {private ImageView iv1; private File imgFile; private LinearLayout ll; @ Override protected void onCreate (Bundle savedInstanceStat E) {super. onCreate (savedInstanceState); supportRequestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); initView ();} private void initView () {iv1 = (ImageView) findViewById (R. id. iv1); ll = (LinearLayout) findViewById (R. id. ll);} // obtain the public void sys_capture (View view) {startActivityForResult (new Intent (MediaStore. ACTION_IMAGE_CAPTURE), 0);} // obtain the source image taken by the system. Public void orig_capture (View view) {Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // The image name String pic_name = "IMG _" + new SimpleDateFormat ("yyyyMMdd_hhmmssSSS "). format (new Date () + ". jpg "; // image save path String pic_dir = Environment. getExternalStorageDirectory (). getAbsolutePath () + File. separator + "MyPicture"; File file = new File (pic_dir); if (! File. exists () {file. mkdirs ();} // image File imgFile = new File (pic_dir + File. separator + pic_name); Uri imgFileUri = Uri. fromFile (imgFile); intent. putExtra (MediaStore. EXTRA_OUTPUT, imgFileUri); // specifies the position where the image is saved after the system takes a picture. startActivityForResult (intent, 1) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {if (RESULT_ OK = resultCode) {switch (requestCode) {case 0: {Bundle bundle = data. getExtras (); // get the container Bitmap bitmap = (Bitmap) bundle in the intent. get ("data"); // figure iv1.setImageBitmap (bitmap); break;} case 1: {Bitmap bitmap = getFitBitMap (); ll. setBackground (new BitmapDrawable (getResources (), bitmap); break ;}}}} /*** assume that the size of the image is x 50, and the size I need to display is 50x40 *. In this case, the image size must be halved to display the image in full. That is, the compression ratio of inSampleSize is 2 * ratio of source image to display image width: 100/50 = 2 * Ratio of the original image to the height of the display image: 50/40 = 1.25 *, the compression ratio is inSampleSize = the maximum aspect ratio is 2 * @ return */private Bitmap getFitBitMap () {// 1. obtain the image width and height BitmapFactory. options options = new BitmapFactory. options (); options. inJustDecodeBounds = true; // obtain the image attribute BitmapFactory only if the image is not encoded. decodeFile (imgFile. getAbsolutePath (), options); int imgWidth = options. outWidth; int imgHeight = options. outHeight; Log. e ("=", "" + imgWidth + "---" + imgHeight); // 2. get screen width and height Display defaultDisplay = getWindowManager (). getdefadisplay display (); Point outSize = new Point (); defaultDisplay. getSize (outSize); int screenWidth = outSize. x; int screenHeight = outSize. y; Log. e ("=", "" + screenWidth + "---" + screenHeight); // 3. calculate the compression ratio int widthRatio = (int) Math. ceil (imgWidth/screenWidth); // ceil indicates to take an integer up. That is, if the decimal part is not 0, 1 int heightRatio = (int) Math is added to the integer part. ceil (imgHeight/screenHeight); int fitInSampleSize = Math. max (widthRatio, heightRatio); // 4. compress the image options according to the compression ratio. inSampleSize = fitInSampleSize; options. inJustDecodeBounds = false; Bitmap fitBitmap = BitmapFactory. decodeFile (imgFile. getAbsolutePath (), options); return fitBitmap ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.