android 些许记录

Android 计时器的使用

1
2
3
4
5
6
7
8
9
mTT = new TimerTask() {
@Override
public void run() {

Message msg = myHandler.obtainMessage();
msg.what = MESSAGE_SENDTO_SERVER;
myHandler.sendMessage(msg);
}
};
mTimer.scheduleAtFixedRate(mTT, 1, 4000);
  • TimerTask 后面加要在计时器中todo的事情
  • public void scheduleAtFixedRate(TimerTask task,
    Date firstTime,
    long period) 
    
  • task - task to be scheduled.
    firstTime - First time at which task is to be executed.
    period - time in milliseconds between successive task executions.

Android 摄像机预览图片上传服务器

图片处理

  • 在Camera.PreviewCallback方法中处理
  • 用YuvImage 获取图片和转化为jpg
  • 用Matrix 和 Bitmap 对图片进行高效处理
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    private Camera.PreviewCallback mPreviewCallback = new Camera.PreviewCallback() {

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {

    if (!mNeedToSend) {
    return;
    }
    mNeedToSend = false;

    Camera.Parameters parameters = camera.getParameters();

    // Log.d(TAG, "frame updated...........:" + data.length);
    Log.d(TAG, "Data sends to the server!");
    try {
    YuvImage yuvImage = new YuvImage(data,
    parameters.getPreviewFormat(),
    parameters.getPreviewSize().width,
    parameters.getPreviewSize().height, null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    yuvImage.compressToJpeg(
    new Rect(0, 0, parameters.getPreviewSize().width,
    parameters.getPreviewSize().height), 90,
    baos);
    yuvImage = null;

    byte cdata[] = baos.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(cdata, 0,
    cdata.length);
    baos.close();
    baos = null;

    Matrix matrix = new Matrix();
    matrix.postRotate(-90);
    matrix.postScale(-1, -1);
    Bitmap rBitmap = Bitmap
    .createBitmap(bitmap, 0, 0, bitmap.getWidth(),
    bitmap.getHeight(), matrix, true);
    bitmap.recycle();
    bitmap = null;

    int w = 252;
    int h = 336;
    float scaleWidth = ((float) w) / rBitmap.getWidth();
    float scaleHeight = ((float) h) / rBitmap.getHeight();

    Matrix matrix2 = new Matrix();
    matrix2.postScale(scaleWidth, scaleHeight);
    Bitmap rrrBitmap = Bitmap.createBitmap(rBitmap, 0, 0,
    rBitmap.getWidth(), rBitmap.getHeight(), matrix2, true);
    Log.d(TAG,
    "(" + rrrBitmap.getWidth() + ","
    + rrrBitmap.getHeight() + ")");
    rBitmap.recycle();
    rBitmap = null;

    if (null != rrrBitmap) {

    ByteArrayOutputStream rbaos = new ByteArrayOutputStream();
    //带缓冲区的输出流,能够提高文件的写入效率
    BufferedOutputStream bos = new BufferedOutputStream(rbaos);
    rrrBitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);

    bos.flush();
    bos.close();
    // Send Data to server.
    sendJpeg2Server(rbaos.toByteArray());
    rbaos.close();
    rrrBitmap.recycle();
    rrrBitmap = null;
    }

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    }

    }