将画布保存到图像错误

时间:2014-01-22 12:43:03

标签: android android-canvas

我的程序让用户拍照,并在此画画。 但是当我试图保存这个画布时,我不能。我试过这种方式

this.setDrawingCacheEnabled(true);  
this.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(this.getDrawingCache());   
this.setDrawingCacheEnabled(false);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

Bitmap bitmap = Bitmap.createBitmap(getBackground().getIntrinsicWidth(), getBackground().getIntrinsicHeight(), Config.ARGB_8888);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

首先,我的程序停止并说错误。

在第二种情况下,只保存背景,不保存用户的图纸。

2 个答案:

答案 0 :(得分:0)

您好我在这里添加了使用相机捕获图像的示例项目,并将文件保存到本地。请检查示例代码并告诉我们。感谢。

<强> Custom_CameraActivity.java

public class Custom_CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mCameraPreview;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mCamera = getCameraInstance();
    mCameraPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mCameraPreview);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCamera.takePicture(null, null, mPicture);
        }
    });
}

/**
 * Helper method to access the camera returns null if it cannot get the
 * camera or does not exist
 * 
 * @return
 */
private Camera getCameraInstance() {
    Camera camera = null;
    try {
        camera = Camera.open();
    } catch (Exception e) {
        // cannot get camera or does not exist
    }
    return camera;
}

PictureCallback mPicture = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        File pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
        }
    }
};

private static File getOutputMediaFile() {
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "MyCameraApp");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
  }
}

<强> CameraPreview.java

public class CameraPreview extends SurfaceView implements
    SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;

// Constructor that obtains context and camera
@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
    super(context);
    this.mCamera = camera;
    this.mSurfaceHolder = this.getHolder();
    this.mSurfaceHolder.addCallback(this);
    this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try {
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        // left blank for now
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    mCamera.stopPreview();
    mCamera.release();
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
        int width, int height) {
    // start preview with new settings
    try {
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (Exception e) {
        // intentionally left blank for a test
    }
  }
}

<强> main.xml中:

<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" />

<Button
    android:id="@+id/button_capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Capture" />

</LinearLayout>

在androidmanifest.xml文件中添加权限

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:0)

感谢您的帮助,我终于找到了解决方案,并将其放在其他人身上。 我做了那个功能:

private void captarpantalla(){

    View view = getWindow().getDecorView().findViewById(android.R.id.content);

    //añade extra el meno, mirar como quitar...

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    //int height = size.y;


    if (high ==0){

        int height = size.y;

        Bitmap bitmap2 = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888);  
        Canvas c2 = new Canvas(bitmap2);
        view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
        view.draw(c2);
        Boolean encontrado = false;
        height = height-1;

        while (encontrado ==false){

            if (bitmap2.getPixel(width/2, height)== Color.argb(000, 000, 000, 000)){
                height = height - 1;
            }else{
                encontrado = true;
                high = height;
            }

        }


    }


    Bitmap bitmap = Bitmap.createBitmap( width, high, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(bitmap);
    view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
    view.draw(c);


    File imageFile = new File(settings.getString("pizarraactual", ""));
    OutputStream fout = null;

    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fout);
        fout.flush();
        fout.close();

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



}

我计算得很高,因为如果你不这样做,当你拿走屏幕时,选择底部有一条黑线。

代码是我程序的一个功能,“settings.getString(”pizarraactual“,”“)”是保存图像的地方。