5

当我单击按钮时,表面和图像一起保存,我尝试了以下代码

camera.takePicture(shutterCallback, rawCallback, jpegCallback);
ShutterCallback shutterCallback = new ShutterCallback() 
{
    public void onShutter() 
    {
        Log.d(TAG, "onShutter'd");
    }
};

PictureCallback rawCallback = new PictureCallback() 
{
    public void onPictureTaken(byte[] data, Camera camera) 
    {
        Log.d(TAG, "onPictureTaken - raw");
    }
};

PictureCallback jpegCallback = new PictureCallback() 
{
    public void onPictureTaken(byte[] data, Camera camera) 
    {
        try 
        {
            File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"Camera");
            if (!root.exists()) 
            {
                root.mkdirs();
            }

            FileOutputStream f = new FileOutputStream(new File(root,System.currentTimeMillis()+".jpg"));
            int len1 = data.length;
            f.write(data,0, len1);
            f.close();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        } 
        finally 
        {
        }
    }
 };

通过此代码仅获取表面,是否有可能将表面与图像一起保存?如果有人知道请帮助我

4

1 回答 1

1

您尝试使用此示例。如

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

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class FingerpaintView extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "FingerpaintView";

    private Paint foregroundPaint;
    private Paint backgroundPaint;
    private int width, height;
    private int lastTouchX, lastTouchY;
    private Bitmap pictureBitmap;
    private Canvas pictureCanvas;
    private final Context context;

    public FingerpaintView(Context context) {
        super(context);
        this.context=context;
        init();
    }

    public FingerpaintView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        init();
    }

    private void init() {
        setFocusableInTouchMode(true);
        getHolder().addCallback(this);
        foregroundPaint = new Paint();
        foregroundPaint.setColor(Color.WHITE);
        foregroundPaint.setStrokeWidth(4);
        backgroundPaint = new Paint();
        backgroundPaint.setColor(Color.BLACK);
        lastTouchX = -1;
        lastTouchY = -1;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        if ((event.getAction() == MotionEvent.ACTION_DOWN) || (event.getAction() == MotionEvent.ACTION_MOVE)) {
            Log.d(TAG, "Touched " + x + "," + y);
            if ((lastTouchX != -1) && (lastTouchY != -1)) {
                pictureCanvas.drawLine(lastTouchX, lastTouchY, x, y, foregroundPaint);
                draw();
            }
            lastTouchX = x;
            lastTouchY = y;
        } else {
            lastTouchX = -1;
            lastTouchY = -1;
        }
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU){
            clear();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        this.width = width;
        this.height = height;
        if (pictureBitmap != null) {
            pictureBitmap.recycle();
        }
        pictureBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        pictureCanvas = new Canvas(pictureBitmap);
        clear();
        draw();
    }

    public void draw() {
        final Canvas c = getHolder().lockCanvas();
        if (c != null) {
            c.drawBitmap(pictureBitmap, 0, 0, null);
            getHolder().unlockCanvasAndPost(c);
        }
    }

    public void clear() {
        pictureCanvas.drawRect(0, 0, width, height, backgroundPaint);
        draw();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (pictureBitmap != null) {

            saveFile(pictureBitmap,"MyImage");


            pictureBitmap.recycle();
        }
        pictureBitmap = null;
    }

    private void saveFile(Bitmap bitmap, String name) {
        // String filename = String.valueOf(System.currentTimeMillis()) ;
        String extStorageDirectory;
        extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        OutputStream outStream = null;
           final File file = new File(extStorageDirectory, name);
           try {
            outStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();

            Toast.makeText(context, "Saved", Toast.LENGTH_LONG).show();

           } catch (final FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
           } catch (final IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
           }

          }

    }

我希望你会有所帮助。

于 2014-04-21T03:55:12.667 回答