YuvImage compressToJpeg - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for YuvImage compressToJpeg method.

This code example shows how to use the following methods:compressToJpeg, getHeight, getWidth
    
     
     
    public static File cropYuv(YuvImage origYuv, int targetSize, Context ctx) {
    	 
    	int w = origYuv.getWidth();
    	int h = origYuv.getHeight();
    	 
    	int left   = (int)(targetSize >= w ? 0 : (float)( (w-targetSize)/2 ) );
    	int right  = (int)(targetSize >= w ? w : (float)( (w-targetSize)/2 ) + targetSize);
    	int top    = (int)(targetSize >= h ? 0 : (float)( (h-targetSize)/2 ) );
    	int bottom = (int)(targetSize >= h ? h : (float)( (h-targetSize)/2 ) + targetSize);
    	 
    	File dir = ctx.getDir("snapshots", Context.MODE_PRIVATE);
        File of = new File(dir, "snapshot.jpg");
        try { 
            FileOutputStream fo = new FileOutputStream(of);
             
            try { 
                origYuv.compressToJpeg(new Rect(left, top, right, bottom), 80, fo);

6
CodeRank
This code example shows how to use the following methods:compressToJpeg, sleep
        setTorchMode(mTorchMode);
    } 
 
    private void uploadImage(byte[] imageData) {
        try { 
            YuvImage yuvImage = new YuvImage(
                    imageData, previewFormat, previewWidth, previewHeight, null);
            yuvImage.compressToJpeg(r, 20, out); // Tweak the quality here - 20
            // seems pretty decent for quality + size. 
            if (isLocalUrl) {
                try { 
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } 
                mParent.setRemoteEyesImage(out.toByteArray());
            } else { 
                PutMethod put = new PutMethod(putUrl);
                put.setRequestBody(new ByteArrayInputStream(out.toByteArray()));
                int result = put.execute(mHttpState, mConnection);
5
CodeRank
This code example shows how to use the following methods:compressToJpeg
		if (this.delegate == null) {
			return; 
		} 
		 
		try { 
            YuvImage yuvImage = new YuvImage(imageData, previewFormat, previewWidth, previewHeight, null);
            yuvImage.compressToJpeg(r, 20, out); // Tweak the quality here - 20 seems pretty decent for quality + size.
             
            delegate.newImageFromCameraPreviewFeed(this, out.toByteArray());
             
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } finally { 
            out.reset();
            if (mCamera != null) {
                mCamera.addCallbackBuffer(mCallbackBuffer);
            } 
            busyProcessing = false;
        } 
		 
5
CodeRank
This code example shows how to use the following methods:compressToJpeg
      camera.setPreviewCallback(new PreviewCallback() { 
        // Called for each frame previewed 
        public void onPreviewFrame(byte[] data, Camera camera) { 
         
        try{ 
	        		 YuvImage yuv_image = new YuvImage(data,param.getPreviewFormat() , size.width, size.height, null); // all bytes are in YUV format therefore to use the YUV helper functions we are putting in a YUV object 
	        		 Rect rect = new Rect(0, 0, size.width, size.height);  
	                 ByteArrayOutputStream output_stream = new ByteArrayOutputStream();  
	                 yuv_image.compressToJpeg(rect, 80, output_stream);// image has now been converted to the jpg format and bytes have been written to the output_stream object
	                  
	                  
	                 byte [] tmp=output_stream.toByteArray();//getting the byte array
	                 outStream.writeInt(tmp.length);// sending the size of the array 
	                 outStream.write(tmp);// writing the array to the socket output stream
	                 outStream.flush();
	                 System.gc();
	                  
					Log.d(TAG, "onPreviewFrame - wrote bytes: "+ data.length);
				        	 
	        		 
5
CodeRank
This code example shows how to use the following methods:compressToJpeg
	public CameraPreviewHolder(int width, int height) {
	} 
 
	@Override 
	public void onPreviewFrame(byte[] frame, Camera arg1) {
		YuvImage yuvimage = new YuvImage(frame,ImageFormat.NV21,arg1.getParameters().getPreviewSize().width,arg1.getParameters().getPreviewSize().height,null);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		yuvimage.compressToJpeg(new Rect(0,0,arg1.getParameters().getPreviewSize().width,arg1.getParameters().getPreviewSize().height), 80, baos);
		currentBuffer = baos.toByteArray();
	} 
	 
	public byte[] getCurrentBuffer() { 
		return currentBuffer;
	} 
 
} 
5
CodeRank
This code example shows how to use the following methods:compressToJpeg
        } 
    } 
     
    private byte[] decodeYUV(byte[] yuv, int width, int height) {
    	ByteArrayOutputStream out = new ByteArrayOutputStream();
    	YuvImage yuvImage = new YuvImage(yuv, ImageFormat.NV21, width, height, null);
    	yuvImage.compressToJpeg(new Rect(0, 0, width, height), 50, out);
    	byte[] imageBytes = out.toByteArray();
    	return imageBytes;
    } 
       
}   
5
CodeRank
This code example shows how to use the following methods:compressToJpeg
	public Bitmap getPic(int x, int y, int width, int height) {
		System.gc(); 
		Bitmap b = null;
		Size s = parameters.getPreviewSize();
 
		YuvImage yuvimage = new YuvImage(buffer, ImageFormat.NV21, s.width, s.height, null);
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		yuvimage.compressToJpeg(new Rect(x, y, width, height), 100, outStream); // make JPG
		b = BitmapFactory.decodeByteArray(outStream.toByteArray(), 0, outStream.size()); // decode JPG
		yuvimage = null;
		outStream = null;
		System.gc();
		return b;
	} 
	 
	private void updateBufferSize() { 
		buffer = null;
		System.gc();
		// prepare a buffer for copying preview data to 
		int h = camera.getParameters().getPreviewSize().height;
5
CodeRank
This code example shows how to use the following methods:compressToJpeg
	public Bitmap getPic(int x, int y, int width, int height) {
		System.gc();
		Bitmap b = null;
		Size s = mParameters.getPreviewSize();
 
		YuvImage yuvimage = new YuvImage(mBuffer, ImageFormat.NV21, s.width,
				s.height, null);
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		yuvimage.compressToJpeg(new Rect(x, y, width, height), 100, outStream); // make
																				// JPG 
		b = BitmapFactory.decodeByteArray(outStream.toByteArray(), 0,
				outStream.size()); // decode JPG
		if (b != null) {
			// Log.i(TAG, "getPic() WxH:" + b.getWidth() + "x" + b.getHeight()); 
		} else { 
			// Log.i(TAG, "getPic(): Bitmap is null.."); 
		} 
		yuvimage = null;
		outStream = null;
		System.gc();
4
CodeRank
This code example shows how to use the following methods:compressToJpeg
    public Bitmap getPic(int x, int y, int width, int height) {
        System.gc();
        Bitmap b = null;
        Size s = mParameters.getPreviewSize();
      
        YuvImage yuvimage = new YuvImage(mBuffer, ImageFormat.NV21, s.width, s.height, null);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        yuvimage.compressToJpeg(new Rect(x, y, width, height), 100, outStream); // make JPG
        b = BitmapFactory.decodeByteArray(outStream.toByteArray(), 0, outStream.size());
        if (b != null) {
            //Log.i(TAG, "getPic() WxH:" + b.getWidth() + "x" + b.getHeight()); 
        } else { 
            //Log.i(TAG, "getPic(): Bitmap is null.."); 
        } 
        yuvimage = null;
        outStream = null;
        System.gc();
        return b;
    } 
 
4
CodeRank
This code example shows how to use the following methods:compressToJpeg
		public void onPreviewFrame(byte[] data, Camera camera) { 
			Log.d(TAG, "onPreviewFrame"); 
 
			// make a bitmap from the byte array 
			ByteArrayOutputStream out = new ByteArrayOutputStream(); 
			YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, captureWidth, captureHeight, null); 
			yuvImage.compressToJpeg(new Rect(0, 0, captureWidth, captureHeight), 100, out); 
			byte[] imageBytes = out.toByteArray(); 
			Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); 
 
			// draw the frame onto the canvas 
			paintCanvas.drawBitmap(image, matrix, paint); 
 
			invalidate(); 
		} 
	}; 
	PreviewCallback previewCallback = new PreviewCallback() {
		public void onPreviewFrame(byte[] data, Camera camera) { 
		} 
	}; 
4
CodeRank

Related YuvImage compressToJpeg Questions & Answers:

compressToJpeg Method Overview
Compress a rectangle region in the YuvImage to a jpeg.
See Code Examples for other Android YuvImage Methods: