如何在android中保存图像和表面视图?

时间:2022-12-01 11:58:46

Surface along with image when i click on button it's save only the surface for this i tried following code

当我点击按钮时表面和图像一起只保存表面我尝试下面的代码

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 
        {
        }
    }
 };

by this code getting only surface, is there any possible to save surface along with image ? if any one knows please help me

通过这个代码只获得表面,是否有可能与图像一起保存表面?如果有人知道请帮助我

1 个解决方案

#1


1  

You try using this example . Such as

您尝试使用此示例。如

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();
           }

          }

    }

I expected you will be helpful.

我希望你会有所帮助。

#1


1  

You try using this example . Such as

您尝试使用此示例。如

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();
           }

          }

    }

I expected you will be helpful.

我希望你会有所帮助。