2014年12月24日 星期三

Android Camera AutoFocus list delete picture 拍照 自動對焦 列出照片 刪除照片

拍照功能參考在 Android 裡使用 Camera 照相
並加入自動對焦與列出照片並可刪除該照片檔案

1.AndroidManifest.xml加入權限


    

    
    

    
        
            
                

                
            
        
        
        
        
        
    


2.建立java檔與layout檔

TakeAPhotoActivity.java 拍照
package tw.android;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class TakeAPhotoActivity extends Activity {

 private static final String TAG = "TakeAPhotoActivity";
 private SurfaceView sv;
 // private ImageView iv;
 private Button takePhotoBtn, hidePhotoBtn, autoFocus,picList;
 // 相機 callback
 private CameraCallback cc;
 // 快門 callback
 private ShCallback sc;
 // 處理 raw data callback
 private RawCallback rc;
 // 處理 jpg callback
 private JpgCallback jc;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.take_a_photo_activity);

  this.sv = (SurfaceView) this.findViewById(R.id.sv);
  this.takePhotoBtn = (Button) this.findViewById(R.id.takePhotoBtn);
  this.hidePhotoBtn = (Button) this.findViewById(R.id.hidePhotoBtn);
  this.autoFocus = (Button) this.findViewById(R.id.autoFocus);
  this.picList= (Button) this.findViewById(R.id.picList);
  
  this.cc = new CameraCallback();
  this.sc = new ShCallback();
  this.rc = new RawCallback();
  this.jc = new JpgCallback(this);

  Log.d(TAG, "設定預覽視窗...");
  SurfaceHolder sh = this.sv.getHolder();
  sh.addCallback(this.cc);
  sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

  Log.d(TAG, "設定拍照頁面...");
  this.hidePhotoBtn.setVisibility(View.GONE);

  // 按鈕外按自動對焦
  autoFocus.setOnClickListener(new Button.OnClickListener() {

   @Override
   public void onClick(View v) {
    cc.getCarema().autoFocus(new AutoFocusCallback() {
     @Override
     public void onAutoFocus(boolean success, Camera camera) {

     }
    });
   }
  });
  //前往照片列表
  picList.setOnClickListener(new Button.OnClickListener() {

   @Override
   public void onClick(View v) {
    goToPicList();
   }
  });
  
 }

 public void takePhoto(View v) {
  Log.d(TAG, "拍照...");
  // 需要三個 callback:快門、處理 raw data、處理 jpg
  // 拍照時自動對焦
  this.cc.getCarema().autoFocus(new AutoFocusCallback() {
   @Override
   public void onAutoFocus(boolean success, Camera camera) {
    if (success) {
     camera.takePicture(sc, rc, jc);
    }
   }
  });
 }

 public void hidePhoto(View v) {
  Log.d(TAG, "設定拍照頁面...");
  this.takePhotoBtn.setVisibility(View.VISIBLE);// 顯示拍照按鈕
  this.hidePhotoBtn.setVisibility(View.GONE); // 隱藏重拍按鈕

  Log.d(TAG, "回到拍照功能,需重新啟動預覽...");
  this.cc.getCarema().startPreview();
 }

 public void showPhoto(String picPath) {
  Log.d(TAG, "取得照片路徑:" + picPath);
  Log.d(TAG, "設定照片頁面...");
  this.takePhotoBtn.setVisibility(View.GONE);
  this.hidePhotoBtn.setVisibility(View.VISIBLE);

 }

 //前往照片列表
  public void goToPicList(){
   Intent intent=new Intent(this,PhotoList.class);
   startActivity(intent);
  }

}

CameraCallback.java
package tw.android;

import java.io.IOException;

import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;


public class CameraCallback implements Callback {

    private static final String TAG = "CameraCallback";
    private Camera carema;

    public Camera getCarema() {
        return this.carema;
    }

    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "啟動相機...");
        this.carema = Camera.open();
        try {
            Log.d(TAG, "設定預覽視窗");
            this.carema.setPreviewDisplay(holder);
        }
        catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        Log.d(TAG, "開始預覽...");
        this.carema.setDisplayOrientation(90); //相機旋轉90度
        this.carema.startPreview();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(TAG, "停止預覽...");
        this.carema.stopPreview();
        Log.d(TAG, "釋放相機資源...");
        this.carema.release();
        this.carema = null;
    }

 
}

JpgCallback.java
package tw.android;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Environment;
import android.util.Log;

public class JpgCallback implements PictureCallback {

    private static final String TAG = "JpgCallback";
    private String picPath;
    private TakeAPhotoActivity act;
    int number;
    public JpgCallback(TakeAPhotoActivity act) {
        super();
        this.act = act;
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.d(TAG, "處理 JPG 資料,輸出 jpg 檔...");
        /*-- 2015-09-11 相機旋轉90度後,照片需要跟著旋轉90度 --*/
        Bitmap srcBmp, dstBmp;
        srcBmp= BitmapFactory.decodeByteArray(data, 0, data.length);
        Matrix matrix=new Matrix();
        matrix.reset();
        matrix.postRotate(90f);
        dstBmp= Bitmap.createBitmap(srcBmp, 0, 0, srcBmp.getWidth(), srcBmp.getHeight(), matrix, true);
        /*-- 2015-09-11 --*/
        FileOutputStream os = null;
        try {
            File pic = this.createPicFile();
            os = new FileOutputStream(pic);
            dstBmp.compress(Bitmap.CompressFormat.JPEG, 100, os); //2015-09-11 Bitmap 轉回 byte[]
            os.write(data);
        }
        catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        finally {
            if (os != null) {
                try {
                    os.close();
                }
                catch (IOException e) {
                }
            }
        }
        Log.d(TAG, "輸出 JPG 完成");
        // 顯示照片
        this.act.showPhoto(this.picPath);
    }

    @SuppressLint("DefaultLocale")
 private File createPicFile() {
        File sdDir = Environment.getExternalStorageDirectory();
        Log.d(TAG, "sdDir" + sdDir);
        File picDir = new File(sdDir, "takePic"); //建立放置照片資料夾
        if (!picDir.exists()) {
            picDir.mkdir();
        }
        long ctm =System.currentTimeMillis(); //照片名
        
        /* SharedPreferences */
        try {
   SharedPreferences preferencesGet = this.act
     .getSharedPreferences("takePic",
       android.content.Context.MODE_PRIVATE);
   number=preferencesGet.getInt("number", 0); //照片數量
   
  } catch (Exception e) {

  }
        
  SharedPreferences preferencesSave = this.act
    .getSharedPreferences("takePic",
      android.content.Context.MODE_PRIVATE);
  SharedPreferences.Editor editor = preferencesSave.edit();
  
  Log.d(TAG,number+"");
  number++; //照片數量+1
  editor.putInt("number", number);
  editor.putLong(Integer.toString(number), ctm); //儲存照片名
  editor.commit();
  
  
        String fileName = String.format("%d.jpg", ctm);
        File pic = new File(picDir, fileName);
        this.picPath = pic.getAbsolutePath();
        Log.d(TAG, "照片路徑:" + this.picPath);
        return pic;
    }
}
RawCallback.java
package tw.android;

import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.util.Log;

public class RawCallback implements PictureCallback {

    private static final String TAG = "RawCallback";

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.d(TAG, "處理 Raw data...");
    }

}

ShCallback.java
package tw.android;

import android.hardware.Camera.ShutterCallback;
import android.util.Log;

public class ShCallback implements ShutterCallback {

    private static final String TAG = "ShCallback";

    @Override
    public void onShutter() {
        Log.d(TAG, "啟動快門...");
    }
}

PhotoList.java 列出照片
package tw.android;

import java.io.File;

import android.app.Activity;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;

public class PhotoList extends Activity {

 private Button[] detelPic;
 private String[] dataName;

private int number;

 private int id;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.photo_list);
  
  setTable();
 }


 // 排版
 public void setTable() {

  try {
   SharedPreferences preferencesGet = getApplicationContext()
     .getSharedPreferences("takePic",
       android.content.Context.MODE_PRIVATE);
   number=preferencesGet.getInt("number", 0);//取出照片數量
   dataName = new String[number];
   Log.i("number", number+"");
   for(int i = 0; i < number; i++){ 
    dataName[i]=String.valueOf(preferencesGet.getLong(Integer.toString(i+1), 0)); //放入照片名稱
    Log.i("dataName[i]", dataName[i]+"");
   }

  } catch (Exception e) {

  }
  
  
  TableLayout t1 = (TableLayout) findViewById(R.id.tableSet);
  t1.removeAllViews();
  TableRow.LayoutParams tP = new TableRow.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);

  tP.setMargins(0, 0, 0, 20);

  detelPic = new Button[number];
  for (int i = 0; i < number; i++) { // 列
   TableRow row = new TableRow(this);
   ImageView iv = new ImageView(this);
   String picPath = "/storage/emulated/0/takePic/" + dataName[i]
     + ".jpg";
   Uri uri = Uri.fromFile(new File(picPath));
   iv.setLayoutParams(tP);
   iv.setImageURI(uri);
   row.addView(iv, 0);

   // 刪除button
   detelPic[i] = new Button(this);
   detelPic[i].setText("刪除");
   detelPic[i].setId(i);
   detelPic[i].setOnClickListener(dp); // 動作
   row.addView(detelPic[i], 1);

   t1.addView(row);
  }

 }

 private OnClickListener dp = new OnClickListener() {
  public void onClick(View v) {

   id = v.getId();
   // 刪除照片
   File file = new File("/storage/emulated/0/takePic/" + dataName[id]
     + ".jpg");
   file.delete();

   
   setTable();//重整
  }
 };

}

take_a_photo_activity.xml //拍照layout


    
  
    
 
    

    

    


photo_list.xml //照相列表 layout


    

        
        
    


檔案下載:
https://github.com/terryyamg/AndroidCamera
參考來源:
1.http://cw1057.blogspot.tw/2011/12/android-camera_09.html
2.http://stackoverflow.com/questions/8058122/where-to-put-autofocus-in-the-class-android-camera
3.http://stackoverflow.com/questions/5486529/delete-file-from-internal-storage
4.http://stackoverflow.com/questions/10660598/android-camera-preview-orientation-in-portrait-mode 
(2015-09-11 update)
5. http://rincliu.com/blog/2013/11/18/camera/
6.http://bingtian.iteye.com/blog/642128

2014年12月8日 星期一

Android NFC tag writer 寫入 訊息

NFC感應TAG,將訊息寫入TAG中
1.AndroidManifest.xml
加入權限
與最低版本
android:minsdkversion="17" android:targetsdkversion="17"
2.activity_main.xml



    

        

        
    

    

        

        
    

    


3.MainActivity.java
import java.io.IOException;
import nl.paulus.nfctagwriter.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Activity to write NFC tags with own mimetype and ID Based on the excellent
 * tutorial by Jesse Chen
 * http://www.jessechen.net/blog/how-to-nfc-on-the-android-platform/
 */
public class MainActivity extends Activity {

 boolean mWriteMode = false;
 private NfcAdapter mNfcAdapter;
 private PendingIntent mNfcPendingIntent;

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

  ((Button) findViewById(R.id.button))
    .setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
      mNfcAdapter = NfcAdapter
        .getDefaultAdapter(MainActivity.this);
      mNfcPendingIntent = PendingIntent.getActivity(
        MainActivity.this,
        0,
        new Intent(MainActivity.this,
          MainActivity.class)
          .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
        0);
      //開啟寫入
      enableTagWriteMode();

      new AlertDialog.Builder(MainActivity.this)
        .setTitle("感應tag寫入")
        .setOnCancelListener(
          new DialogInterface.OnCancelListener() {
           @Override
           public void onCancel(
             DialogInterface dialog) {
            //關閉寫入
            disableTagWriteMode();
           }

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

 private void enableTagWriteMode() {
  mWriteMode = true;
  IntentFilter tagDetected = new IntentFilter(
    NfcAdapter.ACTION_TAG_DISCOVERED);
  IntentFilter[] mWriteTagFilters = new IntentFilter[] { tagDetected };
  mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent,
    mWriteTagFilters, null);
 }

 private void disableTagWriteMode() {
  mWriteMode = false;
  mNfcAdapter.disableForegroundDispatch(this);
 }

 @SuppressLint("NewApi")
 @Override
 protected void onNewIntent(Intent intent) {
  // Tag writing mode
  if (mWriteMode
    && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
   Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
   NdefRecord record = NdefRecord.createMime( //需要Android 4.1(API Level 16)
     ((TextView) findViewById(R.id.mime)).getText().toString(),
     ((TextView) findViewById(R.id.value)).getText().toString()
       .getBytes());
   NdefMessage message = new NdefMessage(new NdefRecord[] { record });
   if (writeTag(message, detectedTag)) {
    Toast.makeText(this, "成功寫入!",
      Toast.LENGTH_LONG).show();
   }
  }
  
  //讀取寫入訊息 看是否有寫入
  
        try {
         Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
      NdefMessage[] msgs = new NdefMessage[rawMsgs.length];
      for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }

            NdefMessage msg = msgs[0];
         String msgstring = new String(msg.getRecords()[0].getPayload(), "UTF-8");
         Toast.makeText(getApplicationContext(), msgstring, Toast.LENGTH_LONG).show();
   
        } catch (Exception e) {
            e.printStackTrace();
        }
 }

 /* 檢查訊息 */
 public boolean writeTag(NdefMessage message, Tag tag) {
  int size = message.toByteArray().length;
  try {
   Ndef ndef = Ndef.get(tag);
   if (ndef != null) {
    ndef.connect();
    if (!ndef.isWritable()) {
     Toast.makeText(getApplicationContext(),
       "Error: tag not writable", Toast.LENGTH_SHORT)
       .show();
     return false;
    }
    if (ndef.getMaxSize() < size) {
     Toast.makeText(getApplicationContext(),
       "Error: tag too small", Toast.LENGTH_SHORT).show();
     return false;
    }
    ndef.writeNdefMessage(message);
    return true;
   } else {
    NdefFormatable format = NdefFormatable.get(tag);
    if (format != null) {
     try {
      format.connect();
      format.format(message);
      return true;
     } catch (IOException e) {
      return false;
     }
    } else {
     return false;
    }
   }
  } catch (Exception e) {
   return false;
  }
 }
} 
參考來源:
https://github.com/balloob/Android-NFC-Tag-Writer 
http://stackoverflow.com/questions/7917567/strange-character-on-android-ndef-record-payload