任何人都可以为我提供良好的相机代码

时间:2013-10-23 09:34:13

标签: android android-camera

我是Android的新手,我有一个相机代码在三星的二重奏中工作得很好,但是如果我使用micromax,karbon ......等它不起作用,而且在某些手机中它会打开前置摄像头..等等

public static boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}  

//摄像头代码

public void openCamera() {

    if (Helper.checkCameraHardware(this)) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateFileName = sdf.format(new Date());

            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
            String curentDateandTime = sdf1.format(new Date());

            File sdImageMainDirectory = new File(Environment
                    .getExternalStorageDirectory().getPath()
                    + "/"
                    + Helper.IMG_FOLDER + "/" + dateFileName);
            if (!sdImageMainDirectory.exists()) {
                sdImageMainDirectory.mkdirs();
            }

            String PATH = Environment.getExternalStorageDirectory()
                    .getPath()
                    + "/"
                    + Helper.IMG_FOLDER
                    + "/"
                    + dateFileName + "/";
            // PATH = PATH OF DIRECTORY,image_PATH = full path of IMAGE

            image_PATH = PATH + curentDateandTime + ".jpg";

            System.out.println("image_PATH In open camera" + image_PATH);

            File file = new File(PATH, curentDateandTime + ".jpg");

            Uri outputFileUri = Uri.fromFile(file);

            Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
            i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(i, 1234);

        } catch (Exception e) {
            Helper.AlertBox(this,
                    "Error No: 001\nPlease contact Bluefrog technical person.\n"
                            + e.toString());
        }
    } else {
        Helper.AlertBox(this, "Image Not Captured.!");
        image_PATH = "";
    }
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // image_PATH = "";
    image_str = "";

    // super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1234) {
        if (resultCode == RESULT_OK) {
            // restorePreferences();

            Log.e("image_PATH in OnActivityResultSet", "File exist at "
                    + image_PATH);

            File file = new File(image_PATH);
            if (file.exists()) {

                Log.e("File exist condition :", "File exist at "
                        + image_PATH);

                try {

                    iv_MEPhoto.setImageBitmap(Helper.getImage(file
                            .getPath()));
                    iv_MEPhoto.setVisibility(View.VISIBLE);
                    photoTaken = true;
                    SENDING_IMAGE_PATH = image_PATH;

                    Log.e("File exist condition :", "File exist at "
                            + image_PATH);

                } catch (Exception e) {
                    Helper.AlertBox(this,
                            "Error No: 004\nPlease contact Bluefrog technical person.\n"
                                    + e.toString());
                    Log.e("Error reading file", e.toString());
                }

            } else {
                Helper.AlertBox(this,
                        "Error No: 005\nPlease contact Bluefrog technical person.");
            }
        } else {
            image_PATH = "";
            Helper.AlertBox(this, "Image Not Captured.");

        }
    }

}

1 个答案:

答案 0 :(得分:1)

宣布 -

 public static final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;   

然后在oncreate()方法中使用

光象=(的ImageButton)findViewById(R.id.photobutton);                     photoimage.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                         Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                    File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                    startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);

                    }
                });

然后在创建写

之外
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

         if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE&& resultCode == RESULT_OK) 
            {


               File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
               Bitmap bitmap1 = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1500, 800);
              // System.out.println("hight "+bitmap.getHeight() +"width "+bitmap.getWidth());
                 Matrix  matrix = new Matrix();

                //set image rotation value to 45 degrees in matrix.
                matrix.postRotate(0);

                //Create bitmap with new values.
                Bitmap bitmap = Bitmap.createBitmap( bitmap1, 0, 0,
                         bitmap1.getWidth(),  bitmap1.getHeight(), matrix, true);


        }
    }