程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 圖片-android 利用自帶相機拍照 在現實出來

圖片-android 利用自帶相機拍照 在現實出來

編輯:編程解疑
android 利用自帶相機拍照 在現實出來

單獨可以 放在項目裡就不行 我也真的是無語了 就是在拍完照片 按完成的時候報錯的
上代碼
package com.android.workapp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Person_data_touxiang extends Activity {
private static final int PHOTO_CAPTURE = 1;
private static final int PHOTO_CAPTURE1 = 2;
private static String photoPath = "/sdcard/AnBo/";
private static String photoName = "";

private Button photo, sc_photo,sc_img;
private ImageView img_photo;
private Bitmap upbitmap;
//private String newName = "laoli.jpg";
//private String uploadFile = "/sdcard/AnBo/laol.jpg";
//private String actionUrl = "http://192.168.1.109:8080/Photo/photoServlet";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_person_data_touxiang);

    sc_img = (Button) findViewById(R.id.p_img);//本地
    sc_img.setOnClickListener(new sc_img());
    photo = (Button) findViewById(R.id.photo);//拍照
    sc_photo = (Button) findViewById(R.id.sc_photo);//上傳
    sc_photo.setOnClickListener(new sc_photo());
    img_photo = (ImageView) findViewById(R.id.imt_photo);
    // android.os.NetworkOnMainThreadException
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites()
            .detectNetwork().penaltyLog().build());
    StrictMode.setVmPolicy(
            new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build());

    photo.setOnClickListener(new photo());
}

// 本地
class sc_img implements View.OnClickListener {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, PHOTO_CAPTURE1);
    }

}

class sc_photo implements View.OnClickListener {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        dialog();
    }

}

// 拍照
class photo implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File file = new File(photoPath);
        if (!file.exists()) { // 檢查圖片存放的文件夾是否存在
            file.mkdir(); // 不存在的話 創建文件夾
        }
        photoName=photoPath +System.currentTimeMillis() +".jpg";
        File photo = new File(photoName);
        Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));
        imageUri = Uri.fromFile(photo);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // 這樣就將文件的存儲方式和uri指定到了Camera應用中
        startActivityForResult(intent, PHOTO_CAPTURE);

    }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    String sdStatus = Environment.getExternalStorageState();
    switch (requestCode) {
    case PHOTO_CAPTURE:
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
            Log.i("內存卡錯誤", "請檢查您的內存卡");
        } else {
            BitmapFactory.Options op = new BitmapFactory.Options();
            // 設置圖片的大小
            System.out.println("a" +photoName);
            Bitmap bitMap = BitmapFactory.decodeFile(photoName);
            int width = bitMap.getWidth();
            int height = bitMap.getHeight();
            // 設置想要的大小
            int newWidth = 480;
            int newHeight = 640;
            // 計算縮放比例
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // 取得想要縮放的matrix參數
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的圖片
            bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true);
            // canvas.drawBitmap(bitMap, 0, 0, paint)
            // 防止內存溢出
            op.inSampleSize = 1; // 這個數字越大,圖片大小越小.
            Bitmap pic = null;
            pic = BitmapFactory.decodeFile(photoName, op);
            img_photo.setImageBitmap(pic); // 這個ImageView是拍照完成後顯示圖片
            FileOutputStream b = null;
            ;
            try {
                b = new FileOutputStream(photoName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            if (pic != null) {
                pic.compress(Bitmap.CompressFormat.JPEG, 50, b);
            }
        }
        break;
    case PHOTO_CAPTURE1:
        if (data != null) {

            img_photo.setImageURI(data.getData());
            System.out.println(getAbsoluteImagePath(data.getData()));
            System.out.println("1231");
        }
        break;
    default:
        return;
    }
}

private String getAbsoluteImagePath(Uri data) {
    // TODO Auto-generated method stub
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(data, proj, // Which columns to return
            null, // WHERE clause; which rows to return (all rows)
            null, // WHERE clause selection arguments (none)
            null); // Order-by clause (ascending by name)

    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

protected void dialog() {
    AlertDialog.Builder builder = new Builder(Person_data_touxiang.this);
    builder.setMessage("確認上傳圖片嗎?");

    builder.setTitle("提示");

    builder.setPositiveButton("確認", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.setNegativeButton("取消", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

        }
    });
    builder.create().show();
}

/* 顯示Dialog的method */
private void showDialog(String mess) {
    new AlertDialog.Builder(Person_data_touxiang.this).setTitle("提示").setMessage(mess)
            .setNegativeButton("確定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            }).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
圖片說明

最佳回答:


manifest有聲明權限打開攝像頭沒?

ouchao0727
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved